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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19e38304d5db7cc98da0f3660a476fe82f0cf272
4
- data.tar.gz: cf6cc1aec755961576535947fa183bf1333c8c77
3
+ metadata.gz: dc95ef88893133f15ebad8a99f97f2c1dc2ad1c1
4
+ data.tar.gz: 51edf909a9588acaae62f4d7caffef90749b507d
5
5
  SHA512:
6
- metadata.gz: 4508647eb8ed11ae58f10c380f8f4dd4297c7b00c3f9cb058d842924820cb2d64a21228834fbc00a4ac64450893bdc5d495fe5774b9905553c802739fb44bc27
7
- data.tar.gz: 227b25c6d687dfc75ed4285e639aa3755ed1f1ff2b25526668fef5c59b13ff6274ea5c92ad95497b08160061e5d1e92cb0f6616b6e335aa30f53cab7edb9145b
6
+ metadata.gz: 46f6e081725eff2335c66f39419aed3633fb3129ae1621bba1ac1c6091e1429cbab228ab93fb173659cfa384b0ff254a24dccc3f26be5312d8cfa0920f7eb1b0
7
+ data.tar.gz: e61814a55c82e153708fd76ffb43605874f05201da8cd46ff64303dab70f84dada79f9fcd88d10742c085a8d9e578aed47b974042695794c8b3a7f412a95ca13
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pecorb (1.0.2)
4
+ pecorb (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
@@ -1,3 +1,3 @@
1
1
  module Pecorb
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/pecorb.rb CHANGED
@@ -1,14 +1,9 @@
1
1
  require_relative "pecorb/version"
2
- require "io/console"
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
- $stderr.puts prompt
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
- $stderr.print "\b"
36
+ backspace
41
37
  @cursor -= 1
42
- when "#{CSI}D" # Left arrow key
38
+ when Console::LEFT
43
39
  next unless @cursor > 0
44
- $stderr.print c
40
+ print c
45
41
  @cursor -= 1
46
- when "#{CSI}C" # Right arrow key
42
+ when Console::RIGHT
47
43
  next unless @cursor < @input.length
48
- $stderr.print c
44
+ print c
49
45
  @cursor += 1
50
- when "#{CSI}A" # Up arrow key
51
- @selected -= 1 if @selected > 0
46
+ when Console::UP
47
+ @selected = (@selected - 1) % @displayed_items.size
52
48
  replace_items { filter_items(@items, @input) }
53
- when "#{CSI}B" # Down arrow key
54
- @selected += 1 if @selected < @displayed_items.size - 1
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
- $stderr.print c
56
+ print c
61
57
  @cursor += 1
62
58
  end
63
59
  end
64
60
 
65
61
  move_cursor_from_start_to_end
66
- $stdout.puts @displayed_items[@selected]
62
+ output @displayed_items[@selected]
67
63
  @displayed_items[@selected]
68
64
  end
69
65
 
70
66
  private
71
67
 
72
- def move_cursor_from_end_to_start
73
- # Move up x times and right y times
74
- $stderr.print "#{CSI}#{@displayed_items.size+1}A#{CSI}#{@prompt.length}C"
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 move_cursor_from_start_to_end
78
- # Move down x times and then to the start of the line
79
- $stderr.print "#{CSI}#{@displayed_items.size+1}B\r"
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 read_char
83
- $stdin.echo = false
84
- $stdin.raw!
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
- $stderr.print "#{CSI}s"
98
- $stderr.print "\b"*@cursor
99
- $stderr.print "#{@input}#{CSI}K"
100
- $stderr.print "#{CSI}u"
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
- $stderr.print "#{CSI}s"
106
- $stderr.print "#{CSI}\r"
107
- $stderr.print "#{CSI}B#{CSI}K" * @displayed_items.size
108
- $stderr.print "#{CSI}A" * (@displayed_items.size - 1) if @displayed_items.size > 0
109
- new_items = yield
110
- @displayed_items = new_items
111
- @selected = limit_max @selected, @displayed_items.size - 1
112
- print_items new_items
113
- $stderr.print "#{CSI}u"
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
- $stderr.puts "#{@selected == i ? "#{SELECTED_COLOR}‣" : " "} #{item}#{RESET_COLOR}"
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.2
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