pecorb 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +9 -1
- data/lib/pecorb/console.rb +56 -0
- data/lib/pecorb/version.rb +1 -1
- data/lib/pecorb.rb +54 -53
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc95ef88893133f15ebad8a99f97f2c1dc2ad1c1
|
4
|
+
data.tar.gz: 51edf909a9588acaae62f4d7caffef90749b507d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46f6e081725eff2335c66f39419aed3633fb3129ae1621bba1ac1c6091e1429cbab228ab93fb173659cfa384b0ff254a24dccc3f26be5312d8cfa0920f7eb1b0
|
7
|
+
data.tar.gz: e61814a55c82e153708fd76ffb43605874f05201da8cd46ff64303dab70f84dada79f9fcd88d10742c085a8d9e578aed47b974042695794c8b3a7f412a95ca13
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -65,9 +65,17 @@ tag for the version, push git commits and tags, and push the `.gem` file to
|
|
65
65
|
Bug reports and pull requests are welcome on GitHub at
|
66
66
|
https://github.com/stevenocchipinti/pecorb.
|
67
67
|
|
68
|
-
|
69
68
|
## License
|
70
69
|
|
71
70
|
The gem is available as open source under the terms of the
|
72
71
|
[MIT License](http://opensource.org/licenses/MIT).
|
73
72
|
|
73
|
+
## Kudos
|
74
|
+
|
75
|
+
Inspiration and education came from these resources:
|
76
|
+
- [inquirer.js](https://github.com/SBoudrias/Inquirer.js/)
|
77
|
+
- [inquirer.rb](https://github.com/arlimus/inquirer.rb)
|
78
|
+
- [peco](https://github.com/peco/peco)
|
79
|
+
- [percol](https://github.com/mooz/percol)
|
80
|
+
- [Random Access Terminal - Blog Post](http://graysoftinc.com/terminal-tricks/random-access-terminal)
|
81
|
+
- [ANSI Escape Codes - Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "io/console"
|
2
|
+
|
3
|
+
module Console
|
4
|
+
CSI = "\e["
|
5
|
+
UP = "#{CSI}A"
|
6
|
+
DOWN = "#{CSI}B"
|
7
|
+
RIGHT = "#{CSI}C"
|
8
|
+
LEFT = "#{CSI}D"
|
9
|
+
|
10
|
+
def read_char
|
11
|
+
$stdin.echo = false
|
12
|
+
$stdin.raw!
|
13
|
+
input = $stdin.getc.chr
|
14
|
+
if input == "\e" then
|
15
|
+
input << $stdin.read_nonblock(3) rescue nil
|
16
|
+
input << $stdin.read_nonblock(2) rescue nil
|
17
|
+
end
|
18
|
+
ensure
|
19
|
+
$stdin.echo = true
|
20
|
+
$stdin.cooked!
|
21
|
+
return input
|
22
|
+
end
|
23
|
+
|
24
|
+
def print(val) $stderr.print val end
|
25
|
+
def puts(val) $stderr.puts val end
|
26
|
+
def output(val) $stdout.puts val end
|
27
|
+
|
28
|
+
def up(n=1) $stderr.print "#{CSI}#{n}A" end
|
29
|
+
def down(n=1) $stderr.print "#{CSI}#{n}B" end
|
30
|
+
def right(n=1) $stderr.print "#{CSI}#{n}C" end
|
31
|
+
def left(n=1) $stderr.print "#{CSI}#{n}D" end
|
32
|
+
|
33
|
+
def backspace(n=1) $stderr.print "\b"*n end
|
34
|
+
def carriage_return() $stderr.print "\r" end
|
35
|
+
def clear_to_eol() $stderr.print "#{CSI}K" end
|
36
|
+
def clear_screen() $stderr.print "#{CSI}H#{CSI}J" end
|
37
|
+
|
38
|
+
def black() $stderr.print "#{CSI}#{CSI}30m" end
|
39
|
+
def red() $stderr.print "#{CSI}#{CSI}31m" end
|
40
|
+
def green() $stderr.print "#{CSI}#{CSI}32m" end
|
41
|
+
def yellow() $stderr.print "#{CSI}#{CSI}33m" end
|
42
|
+
def blue() $stderr.print "#{CSI}#{CSI}34m" end
|
43
|
+
def magenta() $stderr.print "#{CSI}#{CSI}35m" end
|
44
|
+
def cyan() $stderr.print "#{CSI}#{CSI}36m" end
|
45
|
+
def white() $stderr.print "#{CSI}#{CSI}37m" end
|
46
|
+
def reset_color() $stderr.print "#{CSI}#{CSI}0m" end
|
47
|
+
|
48
|
+
def save_pos
|
49
|
+
$stderr.print "#{CSI}s"
|
50
|
+
if block_given?
|
51
|
+
yield
|
52
|
+
load_pos
|
53
|
+
end
|
54
|
+
end
|
55
|
+
def load_pos() $stderr.print "#{CSI}u" end
|
56
|
+
end
|
data/lib/pecorb/version.rb
CHANGED
data/lib/pecorb.rb
CHANGED
@@ -1,14 +1,9 @@
|
|
1
1
|
require_relative "pecorb/version"
|
2
|
-
|
2
|
+
require_relative "pecorb/console"
|
3
3
|
|
4
4
|
module Pecorb
|
5
5
|
extend self
|
6
|
-
|
7
|
-
KILL_LINE_CHAR = "\x1B[K"
|
8
|
-
CURSOR_UP_CHAR = "\x1B[A"
|
9
|
-
CSI = "\e["
|
10
|
-
SELECTED_COLOR = "#{CSI}\e[36m"
|
11
|
-
RESET_COLOR = "#{CSI}\e[0m"
|
6
|
+
extend Console
|
12
7
|
|
13
8
|
@input = ""
|
14
9
|
@cursor = 0
|
@@ -21,9 +16,7 @@ module Pecorb
|
|
21
16
|
@displayed_items = @items = items
|
22
17
|
@prompt = prompt
|
23
18
|
|
24
|
-
|
25
|
-
print_items(items)
|
26
|
-
move_cursor_from_end_to_start
|
19
|
+
print_menu
|
27
20
|
|
28
21
|
while c = read_char
|
29
22
|
case c
|
@@ -32,85 +25,86 @@ module Pecorb
|
|
32
25
|
when /[]/
|
33
26
|
@input = ""
|
34
27
|
break
|
28
|
+
when /[]/
|
29
|
+
clear_screen
|
30
|
+
print_menu
|
35
31
|
when "" # Backspace key
|
36
32
|
next if @input.empty? || @cursor <= 0
|
37
33
|
@input.slice!(@cursor-1)
|
38
34
|
replace_input(@input)
|
39
35
|
replace_items { filter_items(@items, @input) }
|
40
|
-
|
36
|
+
backspace
|
41
37
|
@cursor -= 1
|
42
|
-
when
|
38
|
+
when Console::LEFT
|
43
39
|
next unless @cursor > 0
|
44
|
-
|
40
|
+
print c
|
45
41
|
@cursor -= 1
|
46
|
-
when
|
42
|
+
when Console::RIGHT
|
47
43
|
next unless @cursor < @input.length
|
48
|
-
|
44
|
+
print c
|
49
45
|
@cursor += 1
|
50
|
-
when
|
51
|
-
@selected
|
46
|
+
when Console::UP
|
47
|
+
@selected = (@selected - 1) % @displayed_items.size
|
52
48
|
replace_items { filter_items(@items, @input) }
|
53
|
-
when
|
54
|
-
@selected
|
49
|
+
when Console::DOWN
|
50
|
+
@selected = (@selected + 1) % @displayed_items.size
|
55
51
|
replace_items { filter_items(@items, @input) }
|
56
52
|
else
|
57
53
|
@input.insert(@cursor, c)
|
58
54
|
replace_input(@input)
|
59
55
|
replace_items { filter_items(@items, @input) }
|
60
|
-
|
56
|
+
print c
|
61
57
|
@cursor += 1
|
62
58
|
end
|
63
59
|
end
|
64
60
|
|
65
61
|
move_cursor_from_start_to_end
|
66
|
-
|
62
|
+
output @displayed_items[@selected]
|
67
63
|
@displayed_items[@selected]
|
68
64
|
end
|
69
65
|
|
70
66
|
private
|
71
67
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
68
|
+
def print_menu
|
69
|
+
puts @prompt
|
70
|
+
print_items(@displayed_items)
|
71
|
+
move_cursor_from_end_to_start
|
72
|
+
print @input
|
75
73
|
end
|
76
74
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
75
|
+
def move_cursor_from_end_to_start
|
76
|
+
up(@displayed_items.size + 1)
|
77
|
+
right(@prompt.size)
|
80
78
|
end
|
81
79
|
|
82
|
-
def
|
83
|
-
|
84
|
-
|
85
|
-
input = $stdin.getc.chr
|
86
|
-
if input == "\e" then
|
87
|
-
input << $stdin.read_nonblock(3) rescue nil
|
88
|
-
input << $stdin.read_nonblock(2) rescue nil
|
89
|
-
end
|
90
|
-
ensure
|
91
|
-
$stdin.echo = true
|
92
|
-
$stdin.cooked!
|
93
|
-
return input
|
80
|
+
def move_cursor_from_start_to_end
|
81
|
+
down(@displayed_items.size + 1)
|
82
|
+
carriage_return
|
94
83
|
end
|
95
84
|
|
96
85
|
def replace_input(str)
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
86
|
+
save_pos do
|
87
|
+
backspace(@cursor)
|
88
|
+
print @input
|
89
|
+
clear_to_eol
|
90
|
+
end
|
101
91
|
end
|
102
92
|
|
103
93
|
def replace_items
|
104
94
|
return unless block_given?
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
95
|
+
list_size = @displayed_items.size
|
96
|
+
save_pos do
|
97
|
+
down
|
98
|
+
carriage_return
|
99
|
+
clear_to_eol
|
100
|
+
if list_size > 0
|
101
|
+
(list_size - 1).times { down; clear_to_eol}
|
102
|
+
(list_size - 1).times { up }
|
103
|
+
end
|
104
|
+
@displayed_items = yield
|
105
|
+
@selected = limit_max @selected, list_size - 1
|
106
|
+
print_items @displayed_items
|
107
|
+
end
|
114
108
|
end
|
115
109
|
|
116
110
|
def limit_max(n, max)
|
@@ -119,7 +113,14 @@ module Pecorb
|
|
119
113
|
|
120
114
|
def print_items(items)
|
121
115
|
items.each_with_index do |item, i|
|
122
|
-
|
116
|
+
if @selected == i
|
117
|
+
cyan
|
118
|
+
print "‣ "
|
119
|
+
else
|
120
|
+
print " "
|
121
|
+
end
|
122
|
+
puts item
|
123
|
+
reset_color
|
123
124
|
end
|
124
125
|
end
|
125
126
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pecorb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Occhipinti
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- bin/pecorb
|
70
70
|
- lib/pecorb.rb
|
71
|
+
- lib/pecorb/console.rb
|
71
72
|
- lib/pecorb/version.rb
|
72
73
|
- pecorb.gemspec
|
73
74
|
- pecorb/.gitignore
|