module ES

Constants

Cmd1
Cmd2
Col
Do1
Do2
S

Public Class Methods

clear() click to toggle source
# File lib/kaki/utils/es.rb, line 75
def clear() ES.csi(:ED, 2) + ES.csi(:CUP, 1, 1) end
clear_below() click to toggle source
# File lib/kaki/utils/es.rb, line 142
def clear_below() ES.csi(:ED) end
cmd(given) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/kaki/utils/es.rb, line 106
def console_size
  [`tput cols`, `tput lines`].map(&:to_i)
end
csi(*args) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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
down() click to toggle source
# File lib/kaki/utils/es.rb, line 76
def down()  ES.cmd(:NEL) end
esc(str) click to toggle source
# File lib/kaki/utils/es.rb, line 71
def esc(str)
  "\e" + str
end
home() click to toggle source
# File lib/kaki/utils/es.rb, line 80
def home()  ES.cursor(0, 0) end
pop() click to toggle source
# File lib/kaki/utils/es.rb, line 82
def pop()   ES.cmd(:DECRC) end
push() click to toggle source
# File lib/kaki/utils/es.rb, line 81
def push()  ES.cmd(:DECSC) end
reset() click to toggle source
# File lib/kaki/utils/es.rb, line 78
def reset() S + "0m" end
safe_scroll(n) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/kaki/utils/es.rb, line 141
def scroll_up(n)  "\n" * n end
top() click to toggle source
# File lib/kaki/utils/es.rb, line 79
def top()   ES.csi(:CHA, 1) end
up() click to toggle source
# File lib/kaki/utils/es.rb, line 77
def up()    ES.cmd(:RI) end

Private Instance Methods

clear() click to toggle source
# File lib/kaki/utils/es.rb, line 75
def clear() ES.csi(:ED, 2) + ES.csi(:CUP, 1, 1) end
clear_below() click to toggle source
# File lib/kaki/utils/es.rb, line 142
def clear_below() ES.csi(:ED) end
cmd(given) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/kaki/utils/es.rb, line 106
def console_size
  [`tput cols`, `tput lines`].map(&:to_i)
end
csi(*args) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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
down() click to toggle source
# File lib/kaki/utils/es.rb, line 76
def down()  ES.cmd(:NEL) end
esc(str) click to toggle source
# File lib/kaki/utils/es.rb, line 71
def esc(str)
  "\e" + str
end
home() click to toggle source
# File lib/kaki/utils/es.rb, line 80
def home()  ES.cursor(0, 0) end
pop() click to toggle source
# File lib/kaki/utils/es.rb, line 82
def pop()   ES.cmd(:DECRC) end
push() click to toggle source
# File lib/kaki/utils/es.rb, line 81
def push()  ES.cmd(:DECSC) end
reset() click to toggle source
# File lib/kaki/utils/es.rb, line 78
def reset() S + "0m" end
safe_scroll(n) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/kaki/utils/es.rb, line 141
def scroll_up(n)  "\n" * n end
top() click to toggle source
# File lib/kaki/utils/es.rb, line 79
def top()   ES.csi(:CHA, 1) end
up() click to toggle source
# File lib/kaki/utils/es.rb, line 77
def up()    ES.cmd(:RI) end