https://github.com/agilecreativity/code_lister
List/filter files based on simple concept like 'find' command in Linux/Unix based system
https://github.com/agilecreativity/code_lister
Last synced: 10 months ago
JSON representation
List/filter files based on simple concept like 'find' command in Linux/Unix based system
- Host: GitHub
- URL: https://github.com/agilecreativity/code_lister
- Owner: agilecreativity
- License: mit
- Created: 2014-04-12T21:47:09.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2017-12-31T01:47:25.000Z (over 8 years ago)
- Last Synced: 2025-07-27T12:58:10.830Z (12 months ago)
- Language: Ruby
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## code_lister
[](http://badge.fury.io/rb/code_lister)
[](https://gemnasium.com/agilecreativity/code_lister)
[](https://codeclimate.com/github/agilecreativity/code_lister)
Find/filter files based on simple criteria like `extension`, include/exclude some words in the name.
It provides the functionality similar to subset of `find | grep` command in Linux/Unix system.
Initially this was part of my internal project. I extracted this out as a gem so
that I can re-use it in other project.
Note: starting from version `0.1.0` this gem will follow the [Semantic Versioning] convention.
### NOTES
- Starting from version 0.2.0 only ruby 2.1.0+ are supported (use refinement instead of monkeypatching)
- Use version 0.1.x if you need to use with ruby 1.9.3
### Installation
Add this line to your application's Gemfile:
gem 'code_lister'
And then execute:
$ bundle
Or install it yourself as:
$ gem install code_lister
### Usage
#### Using as CLI interface
List all pdf and epub files in the `/opts/downloads/` directory recursively
```sh
$code_lister --base-dir /opt/downloads/ --exts pdf epub --recursive
```
Usage/Synopsis:
```
Usage:
code_lister
Options:
-b, [--base-dir=BASE_DIR] # Base directory
# Default: . (current directory)
-e, [--exts=one two three] # List of extensions to search for
-f, [--non-exts=one two three] # List of files without extension to search for
-n, [--inc-words=one two three] # List of words in the filename to be included with the result if any
-x, [--exc-words=one two three] # List of words in the filename to be excluded from the result if any
-i, [--ignore-case], [--no-ignore-case] # Ignore the case in the input filename
# Default: --ignore-case
-r, [--recursive], [--no-recursive] # Search for files recursively
# Default: --recursive
-v, [--version], [--no-version] # Display version information
List files by extensions, patterns, and simple criteria
```
Example Usage:
- Find all java and ruby files in a given directory
```ruby
# find all files that ends with '*.java' or '*.rb' in a given directory
code_lister -b spec/fixtures/ -e rb java
```
Output:
```
./demo1.xxx.rb
./demo1.yyy.rb
./demo2.xxx.rb
./demo2.yyy.rb
./java/demo3.xxx.java
./java/demo3.yyy.java
./java/demo4.xxx.java
./java/demo4.yyy.java
```
- Find all java java and ruby files but include only the files that contain the word `xxx`
```sh
$code_lister -b spec/fixtures/ -e rb java -n xxx
```
Output:
```
./demo1.xxx.rb
./demo2.xxx.rb
./java/demo3.xxx.java
./java/demo4.xxx.java
```
- Same as previous step, but filter out result that contain the word `demo3` or `demo4`
```ruby
$code_lister -b spec/fixtures/ -e rb java -n xxx -x demo3 demo4
```
Output:
```
./demo1.xxx.rb
./demo2.xxx.rb
```
#### Using as ruby library
This is probably the proper way to utilize the library as the CLI only serve to
show the result in the console.
Example of how you might use the library in your own project.
```ruby
require 'code_lister'
include CodeLister
# To search for everything that ends in '*.java' and '*.rb" recursively
file_list = CodeLister.files base_dir: "spec/fixtures",
exts: %w(rb java),
recursive: true
puts file_list
# To filter out the result you may do so with the `CodeLister.filter` method
new_list = CodeLister.filter(file_list, inc_words: %w(some list of word),
exc_words: %w(other text to excluded),
ignore_case: false)
```
### Development/Testing
```sh
git clone https://github.com/agilecreativity/code_lister.git
cd code_lister
bundle
rake -T
# Play around with it using Pry
rake pry
```
From inside `Pry`
```ruby
include CodeLister
# To search for everything that ends in '*.java' and '*.rb" recursively
list1 = CodeLister.files(base_dir: "spec/fixtures", exts: %w(rb java), recursive: true)
puts list1
# To filter out the result list
list2 = CodeLister.filter(list1, inc_words: %w(final complete), exc_words: %w(demo test))
```
## Contributing
1. Fork it (http://github.com/agilecreativity/code_lister/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
[Semantic Versioning]: http://semver.org