pepin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 570d718040558cf7d64387744e53377dc2af975f
4
+ data.tar.gz: 2d523c01a2cfa6683d6eeb9897244481413e598a
5
+ SHA512:
6
+ metadata.gz: 7506252df04c06ade066876f27ac80871fa54bd376551889c3d92c96abe40f9f45d7d23d5e32e070e279193fad5e5a51d85db55c5d5abc224a567b68cbef1f19
7
+ data.tar.gz: ca4fbede7b90605ba47b62e496f83a31d82d0781bed65535271a99ff32b2a6d53da18b024ca5c2f0dc4b36d71c85c90bc6493ef8495653da918159e24f0d68bd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pepin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hika Hibariya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Pepin
2
+
3
+ Pepin is a Percol and Peco like, interactive filtering tool for CLI written in Ruby.
4
+ `Pepin.search` Launches interactive window for filtering, and returns the filtered item.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'pepin'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install pepin
21
+
22
+ ## Usage
23
+
24
+ The most simple example is below.
25
+
26
+ ```ruby
27
+ #!/usr/bin/env ruby
28
+
29
+ require 'pepin'
30
+
31
+ list = %w(Alpha Bravo Charlie Delta Echo)
32
+ item = Pepin.search(list) # Launches interactive window and returns selected item
33
+
34
+ puts %(You selected "#{item}" from #{list.inspect}.)
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hibariya/pepin. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pepin"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/pepin ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pepin'
4
+
5
+ list = $stdin.read.split($/)
6
+
7
+ # FIXME Pepin responds to SIGWINCH but this command can't handle window size
8
+ $stdin.reopen '/dev/tty'
9
+
10
+ puts Pepin.search(list)
data/lib/pepin.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'pepin/version'
2
+
3
+ module Pepin
4
+ autoload :InteractiveSearch, 'pepin/interactive_search'
5
+ autoload :GrepFilter, 'pepin/grep_filter'
6
+ autoload :Query, 'pepin/query'
7
+ autoload :CursesView, 'pepin/curses_view'
8
+
9
+ class << self
10
+ def search(list, view: nil, filter: nil)
11
+ InteractiveSearch.new(list, view: view, filter: filter).search
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,141 @@
1
+ require 'curses'
2
+ require 'forwardable'
3
+
4
+ module Pepin
5
+ class CursesView
6
+ class PromptView
7
+ extend Forwardable
8
+
9
+ attr_reader :cursor
10
+
11
+ def_delegators :@window, :resize
12
+
13
+ def initialize(height, width, y, x, query:, prompt:)
14
+ @query, @prompt_string = query, prompt
15
+ @window = Curses::Window.new(height, width, y, x)
16
+ @cursor = cursor_range.first
17
+
18
+ @query.add_observer -> { render }, :call
19
+ end
20
+
21
+ def input_char
22
+ @window.getch
23
+ end
24
+
25
+ def move_cursor(value)
26
+ self.cursor = cursor + value
27
+ end
28
+
29
+ def move_cursor_to_first
30
+ self.cursor = cursor_range.first
31
+ end
32
+
33
+ def move_cursor_to_last
34
+ self.cursor = cursor_range.last
35
+ end
36
+
37
+ def render
38
+ @window.setpos 0, 0
39
+ @window.addstr @prompt_string + @query.to_s
40
+ @window.clrtoeol
41
+ end
42
+
43
+ private
44
+
45
+ def cursor_range
46
+ 0..@query.length
47
+ end
48
+
49
+ def cursor=(pos)
50
+ return false unless cursor_range.include?(pos)
51
+
52
+ @cursor = pos
53
+
54
+ @window.setpos 0, @prompt_string.length + pos
55
+ end
56
+ end
57
+
58
+ class ListView
59
+ extend Forwardable
60
+
61
+ def_delegators :@window, :resize
62
+
63
+ def initialize(height, width, y, x, query:)
64
+ @query = query
65
+ @window = Curses::Window.new(height, width, y, x)
66
+
67
+ @query.add_observer -> { render }, :call
68
+ end
69
+
70
+ def render
71
+ @window.clear
72
+
73
+ @query.search.each.with_index 1 do |item, n|
74
+ selected = n == 1
75
+ matched_offset = @query.pattern ? @query.pattern.match(item).offset(0) : [-1, -1]
76
+
77
+ @window.setpos n, 0
78
+ @window.attron Curses::A_UNDERLINE if selected
79
+ item.each_char.with_index do |ch, i|
80
+ @window.attron Curses::A_BOLD if i == matched_offset[0]
81
+ @window.attroff Curses::A_BOLD if i == matched_offset[1]
82
+ @window.addch ch
83
+ end
84
+
85
+ @window.addstr ' ' * (@window.maxx - @window.curx)
86
+ @window.attroff Curses::A_UNDERLINE if selected
87
+ @window.attroff Curses::A_BOLD
88
+ end
89
+
90
+ @window.refresh
91
+ end
92
+ end
93
+
94
+ extend Forwardable
95
+
96
+ def_delegators :@prompt_view, :cursor, :input_char, :move_cursor, :move_cursor_to_first, :move_cursor_to_last
97
+
98
+ def initialize(query, prompt: '> ')
99
+ @query = query
100
+ @prompt_string = prompt
101
+
102
+ launch
103
+ end
104
+
105
+ def launch
106
+ @window = Curses.init_screen
107
+
108
+ @window.keypad true
109
+ Curses.raw
110
+ Curses.noecho
111
+
112
+ @prompt_view = PromptView.new(1, Curses.cols, 0, 0, query: @query, prompt: @prompt_string)
113
+ @list_view = ListView.new( Curses.lines - 1, Curses.cols, 0, 0, query: @query)
114
+
115
+ render
116
+ end
117
+
118
+ def render
119
+ @prompt_view.render
120
+ @list_view.render
121
+ end
122
+
123
+ def refresh_winsize
124
+ Curses.clear
125
+
126
+ lines = Curses.lines
127
+ cols = Curses.cols
128
+
129
+ Curses.resize lines, cols
130
+ @window.resize lines, cols
131
+ @prompt_view.resize 1, cols
132
+ @list_view.resize lines - 1, cols
133
+
134
+ render
135
+ end
136
+
137
+ def shutdown
138
+ Curses.close_screen if @window
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,11 @@
1
+ module Pepin
2
+ class GrepFilter
3
+ def pattern(query)
4
+ query.empty? ? nil : Regexp.new(Regexp.quote(query), 'i')
5
+ end
6
+
7
+ def select(list, query)
8
+ query.empty? ? list : list.grep(pattern(query))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,108 @@
1
+ module Pepin
2
+ class InteractiveSearch
3
+ attr_reader :query, :view
4
+
5
+ def initialize(list, view: nil, filter: nil)
6
+ @query = Query.new(list, filter: filter || GrepFilter)
7
+ @view_klass = view || CursesView
8
+ end
9
+
10
+ def search
11
+ @view = @view_klass.new(query)
12
+
13
+ search_interactive
14
+ ensure
15
+ @view.shutdown if @view
16
+ end
17
+
18
+ private
19
+
20
+ def search_interactive
21
+ while ch = view.input_char
22
+ handler =
23
+ case ch
24
+ when String then :handle_string
25
+ when Curses::KEY_ENTER, 10 then :handle_enter
26
+ when Curses::KEY_CTRL_A then :handle_ctrl_a
27
+ when Curses::KEY_CTRL_W then :handle_ctrl_w
28
+ when Curses::KEY_CTRL_E then :handle_ctrl_e
29
+ when Curses::KEY_CTRL_K then :handle_ctrl_k
30
+ when Curses::KEY_BACKSPACE, Curses::KEY_CTRL_H, 127 then :handle_backspace
31
+ when Curses::KEY_CTRL_C then :handle_ctrl_c
32
+ when Curses::KEY_CTRL_D then :handle_ctrl_d
33
+ when Curses::KEY_CTRL_F, Curses::KEY_RIGHT then :handle_cursor_right
34
+ when Curses::KEY_CTRL_B, Curses::KEY_LEFT then :handle_cursor_left
35
+ when Curses::KEY_RESIZE then :handle_resize
36
+ else :handle_unknown
37
+ end
38
+
39
+ send handler, ch
40
+ end
41
+
42
+ result
43
+ rescue StopIteration
44
+ result
45
+ end
46
+
47
+ def result
48
+ query.search.first
49
+ end
50
+
51
+ def handle_string(ch)
52
+ query.add_char ch
53
+ view.move_cursor 1
54
+ end
55
+
56
+ def handle_enter(*)
57
+ raise StopIteration
58
+ end
59
+
60
+ def handle_ctrl_a(*)
61
+ view.move_cursor_to_first
62
+ end
63
+
64
+ def handle_ctrl_w(*)
65
+ len = query.delete_behind_word_from(view.cursor)
66
+
67
+ view.move_cursor -len
68
+ end
69
+
70
+ def handle_ctrl_e(*)
71
+ view.move_cursor_to_last
72
+ end
73
+
74
+ def handle_ctrl_k(*)
75
+ query.delete_ahead_from view.cursor
76
+ end
77
+
78
+ def handle_backspace(*)
79
+ query.delete_behind_char_from view.cursor
80
+
81
+ view.move_cursor -1
82
+ end
83
+
84
+ def handle_ctrl_c(*)
85
+ exit
86
+ end
87
+
88
+ def handle_ctrl_d(*)
89
+ exit if query.empty?
90
+ end
91
+
92
+ def handle_cursor_right(*)
93
+ view.move_cursor 1
94
+ end
95
+
96
+ def handle_cursor_left(*)
97
+ view.move_cursor -1
98
+ end
99
+
100
+ def handle_resize(*)
101
+ view.refresh_winsize
102
+ end
103
+
104
+ def handle_unknown(*)
105
+ # Intentionally NOP
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,70 @@
1
+ require 'forwardable'
2
+ require 'observer'
3
+
4
+ module Pepin
5
+ class Query
6
+ extend Forwardable
7
+ include Observable
8
+
9
+ def_delegators :@string, :length, :empty?, :to_s
10
+
11
+ def initialize(list, filter: GrepFilter)
12
+ @list = list
13
+ @string = ''
14
+ @filter = filter.new
15
+ end
16
+
17
+ def pattern
18
+ @filter.pattern(@string.strip)
19
+ end
20
+
21
+ def search
22
+ @filter.select(@list, @string.strip)
23
+ end
24
+
25
+ def clear
26
+ @string.clear
27
+
28
+ notify_change
29
+ end
30
+
31
+ def add_char(ch)
32
+ @string << ch
33
+
34
+ notify_change
35
+ end
36
+
37
+ def delete_behind_word_from(pos)
38
+ string_was = @string
39
+ @string = @string[0..pos.pred].split(/\b/)[0...-1].join + @string[pos..-1]
40
+
41
+ notify_change
42
+
43
+ string_was.length - @string.length
44
+ end
45
+
46
+ def delete_behind_char_from(pos)
47
+ @string = @string[0...pos.pred] + @string[pos..-1]
48
+
49
+ notify_change
50
+
51
+ 1
52
+ end
53
+
54
+ def delete_ahead_from(pos)
55
+ string_was = @string
56
+ @string = @string.to_s[0...pos]
57
+
58
+ notify_change
59
+
60
+ string_was.length - @string.length
61
+ end
62
+
63
+ private
64
+
65
+ def notify_change
66
+ changed
67
+ notify_observers
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Pepin
2
+ VERSION = '0.1.0'
3
+ end
data/pepin.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pepin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pepin'
8
+ spec.version = Pepin::VERSION
9
+ spec.authors = ['hibariya']
10
+ spec.email = ['hibariya@gmail.com']
11
+
12
+ spec.summary = %q{Interactive filtering tool for CLI written in Ruby.}
13
+ spec.homepage = 'https://github.com/hibariya/pepin'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'curses', '~> 1.0.1'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.10'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pepin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hibariya
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - hibariya@gmail.com
58
+ executables:
59
+ - pepin
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/pepin
73
+ - lib/pepin.rb
74
+ - lib/pepin/curses_view.rb
75
+ - lib/pepin/grep_filter.rb
76
+ - lib/pepin/interactive_search.rb
77
+ - lib/pepin/query.rb
78
+ - lib/pepin/version.rb
79
+ - pepin.gemspec
80
+ homepage: https://github.com/hibariya/pepin
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Interactive filtering tool for CLI written in Ruby.
104
+ test_files: []