Module: ES
- Defined in:
- lib/kaki/utils/es.rb
Constant Summary collapse
- S =
"\e["
- Col =
%i(black red green yellow blue magenta cyan white ex_c ex_b reset style code)
- Cmd1 =
%i(CUU CUD CUF CUB CNL CPL CHA CHT CBT ECH DCH IL DL SU SD REP DSR)
- Do1 =
%w(A B C D E F G I Z X P L M S T b n)
- Cmd2 =
%i(DECSC DECRC RIS DECALN IND NEL HTS VTS PLD PLU RI DSC SOS ST)
- Do2 =
%w(7 8 c #8 D E H J K L M P X /)
Class Method Summary collapse
- .clear ⇒ Object
- .clear_below ⇒ Object
- .cmd(given) ⇒ Object
- .color(col, opt = nil) ⇒ Object
- .console_size ⇒ Object
- .csi(*args) ⇒ Object
- .cursor(x, y = nil) ⇒ Object
- .cursor_position ⇒ Object
- .cursor_r(x, y) ⇒ Object
- .down ⇒ Object
- .esc(str) ⇒ Object
- .home ⇒ Object
- .pop ⇒ Object
- .push ⇒ Object
- .reset ⇒ Object
- .safe_scroll(n) ⇒ Object
- .scroll(rn = nil) ⇒ Object
- .scroll_up(n) ⇒ Object
- .top ⇒ Object
- .up ⇒ Object
Class Method Details
.clear ⇒ Object
75 |
# File 'lib/kaki/utils/es.rb', line 75 def clear() ES.csi(:ED, 2) + ES.csi(:CUP, 1, 1) end |
.cmd(given) ⇒ Object
66 67 68 69 |
# File 'lib/kaki/utils/es.rb', line 66 def cmd(given) cm = Cmd2.zip(Do2).to_h[given] cm ? "\e" + cm : raise("#{given} is undefined command.") end |
.color(col, opt = nil) ⇒ Object
11 12 13 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 |
# File 'lib/kaki/utils/es.rb', line 11 def color(col, opt = nil) ch = Col.map.with_index {|c, i| [c, i + 30]}.to_h[col] case ch when 38 return S + "38;5;#{opt.to_i}m" when 39 return S + "48;5;#{opt.to_i}m" when 40 return S + "39;40m" when 41 n = case opt when :bold then 1 when :italic then 3 when :blink then 5 when :reverse then 7 when :blink_stop then 25 when :underline then 4 when :bold_stop then 22 when :underline_stop then 24 else opt end return S + "#{n}m" when 42 return S + "#{opt}m" end raise "Undefind color name: #{col}" unless ch m = case opt when :background then 10 when :bright then 60 when :bright_background then 70 else 0 end S + (ch + m).to_s + "m" end |
.console_size ⇒ Object
106 107 108 |
# File 'lib/kaki/utils/es.rb', line 106 def console_size [`tput cols`, `tput lines`].map(&:to_i) end |
.csi(*args) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kaki/utils/es.rb', line 46 def csi(*args) cm = Cmd1.zip(Do1).to_h if (a = cm[args[0]]) S + args[1].to_s + a else case args[0] when :CUP then S + "#{args[1]};#{args[2]}H" when :ED then S + (args[1] ? args[1].to_s : "") + "J" when :EL then S + (args[1] ? args[1].to_s : "") + "K" when :TBC then S + (args[1] ? args[1].to_s : "") + "g" when :DECSTBM S + (args[1] ? "#{args[1]};#{args[2]}" : "") + "r" when :DECTCEM S + "?25" + args[1] else raise "#{args[0]} is undefined CSI." end end end |
.cursor(x, y = nil) ⇒ Object
84 85 86 |
# File 'lib/kaki/utils/es.rb', line 84 def cursor(x, y = nil) y ? ES.csi(:CUP, y, x) : ES.csi(:CHA, x) end |
.cursor_position ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/kaki/utils/es.rb', line 110 def cursor_position puts "\x1B[6n" res = "" STDIN.raw do |io| until (c = io.getc) == 'R' res << c if c end end m = /(\d+);(\d+)/.match(res) [m[2], m[1]].map(&:to_i) end |
.cursor_r(x, y) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/kaki/utils/es.rb', line 88 def cursor_r(x, y) st = "" st += if x > 0 ES.csi(:CUF, x) elsif x < 0 ES.csi(:CUB, -x) else "" end st += if y > 0 ES.csi(:CUD, y) elsif y < 0 ES.csi(:CUU, -y) else "" end end |
.esc(str) ⇒ Object
71 72 73 |
# File 'lib/kaki/utils/es.rb', line 71 def esc(str) "\e" + str end |
.safe_scroll(n) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/kaki/utils/es.rb', line 130 def safe_scroll(n) return "" if n <= 0 str = "" y = ES.cursor_position[1] if (h = ES.console_size[1]) < y + n str = ES.scroll_up(n - 1) y = h - n end str + ES.cursor(1, y) end |
.scroll(rn = nil) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/kaki/utils/es.rb', line 122 def scroll(rn = nil) return case rn when Range then ES.csi(:DECSTBM, rn.first, rn.max) when 0 then ES.csi(:DECSTBM) else ES.push + ES.csi(:DECSTBM) + ES.pop end end |
.scroll_up(n) ⇒ Object
141 |
# File 'lib/kaki/utils/es.rb', line 141 def scroll_up(n) "\n" * n end |