Class: Ansi::Selector::Impl
- Inherits:
-
Object
- Object
- Ansi::Selector::Impl
- Defined in:
- lib/ansi/selector/impl.rb
Direct Known Subclasses
Constant Summary collapse
- CODES =
{ standout_mode: `tput rev`, exit_standout_mode: `tput rmso`, cursor_up: `tput cuu1`, cursor_down: `tput cud1`, carriage_return_key: `tput cr` }
Instance Method Summary collapse
- #all_options_fit?(columns) ⇒ Boolean
-
#initialize(options, formatter, preselected) ⇒ Impl
constructor
A new instance of Impl.
- #select ⇒ Object
- #terminal_columns ⇒ Fixnum
- #terminal_lines ⇒ Fixnum
Constructor Details
#initialize(options, formatter, preselected) ⇒ Impl
Returns a new instance of Impl.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ansi/selector/impl.rb', line 14 def initialize(, formatter, preselected) @options = @formatter = formatter @highlighted_line_index = 0 @cursor_line_index = 0 @columns = terminal_columns # Converts options to column-aligned strings. formatted = @options.map(&@formatter).map(&method(:Array)).map { |line| line.map(&method(:strip_ansi_colors)) } @formatted ||= formatted.map do |f| f.map.with_index do |part, column| width = formatted.map { |fm| fm[column] }.compact.map(&:size).max part.to_s.ljust(width) end.join(' ') end Signal.trap('SIGWINCH', proc do columns = terminal_columns if columns < @columns && !(columns) # When a buffer gets narrower, the text that doesn't fit into the screen anymore # starts jumping around in a way that makes it hard to predict. In such case we # clear everything and re-print options at the beginning of the buffer to # simplify things. tty.print(`printf '\e[2J'`) tty.print(`tput cup 0 0`) else print_line(0) end @columns = columns end) end |
Instance Method Details
#all_options_fit?(columns) ⇒ Boolean
48 49 50 |
# File 'lib/ansi/selector/impl.rb', line 48 def (columns) option_indices.map(&method(:final_text_for_line)).map(&:size).all? { |size| size < columns } end |
#select ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ansi/selector/impl.rb', line 62 def select answer = ask_to_choose # We need to reprint here in order to render the lines that are behind the buffer. range(@highlighted_line_index, @options.size - 1).each(&method(:print_line)) go_to_line(@options.size) answer ensure tty.close end |
#terminal_columns ⇒ Fixnum
53 54 55 |
# File 'lib/ansi/selector/impl.rb', line 53 def terminal_columns `tput cols`.to_i end |
#terminal_lines ⇒ Fixnum
58 59 60 |
# File 'lib/ansi/selector/impl.rb', line 58 def terminal_lines `tput lines`.to_i end |