Module: Typr

Included in:
Space
Defined in:
lib/grid.rb,
lib/line.rb,
lib/text.rb,
lib/space.rb,
lib/stack.rb,
lib/browser.rb,
lib/terminal.rb

Defined Under Namespace

Classes: Browser, Grid, Line, Space, Stack, Text

Constant Summary collapse

MIMETYPES =
eval File.read( __dir__ + "/../share/mimetypes" )
INPUT =

alias :show :print

(IO.new IO.sysopen "/dev/tty", "r")
OUTPUT =
(IO.new IO.sysopen "/dev/stdin", "w")
KEY_ESCAPE =
"\e"
KEY_RETURN =
CARRIAGE_RETURN
KEY_TAB =
"\t"
KEY_ENTER =
CARRIAGE_RETURN
KEY_PAGEDOWN =
KEY_NPAGE
KEY_PAGEUP =
KEY_PPAGE
KEY_INSERT =
KEY_IC
KEY_DELETE =
KEY_DC
MODES =
%i[ reset bold faint italic underline slow fast invert ]
COLORS =
%i[ black red green yellow blue magenta cyan white ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear(mode = :screen) ⇒ Object

def enable attr; mode attr end def disable attr=nil; mode :reset end def refresh; end



138
139
140
# File 'lib/terminal.rb', line 138

def self.clear mode=:screen 
  $>.print( { screen: CLEAR_SCREEN, line: DELETE_LINE }[mode]  )
end

.columnObject



167
# File 'lib/terminal.rb', line 167

def self.column; position.last end

.exitObject



176
177
178
179
180
181
182
183
# File 'lib/terminal.rb', line 176

def self.exit; 
	# background
extend self
	
	$>.print CURSOR_NORMAL; 
	$>.print ORIG_COLORS; color; clear 
	
end

.heightObject



165
# File 'lib/terminal.rb', line 165

def self.height; size.first end

.init(default = [ :white, :black ]) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/terminal.rb', line 168

def self.init default=[ :white, :black  ] 
  $color = $default = default
  # include Typr
  # extend self
  # Object.undef_method :read
  print CURSOR_INVISIBLE
  system "tput smkx"
end

.positionObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/terminal.rb', line 149

def self.position
  result = ''
  $stdin.raw do |stdin|
    $stdout << USER7 #"\e[6n"
    $stdout.flush
    until (char = stdin.getc) == 'R'
      result << char if char
    end
  end
  result[/[\d;]+/].split(?;).map &:to_i
  # m = res.match /(?<row>\d+);(?<column>\d+)/
  # { row: Integer(m[:row]), column: Integer(m[:column]) }
end

.read(obj, prompt = '') ⇒ Object



142
143
144
145
146
147
# File 'lib/terminal.rb', line 142

def self.read obj, prompt=''
  case obj
    when :key then INPUT.raw{ |tty| tty.sysread 6 } rescue return
    when :line then line=Readline.readline prompt; print CURSOR_INVISIBLE;line
  end
end

.rowObject



166
# File 'lib/terminal.rb', line 166

def self.row; position.first end

.sizeObject



163
# File 'lib/terminal.rb', line 163

def self.size; IO.console.winsize end

.widthObject



164
# File 'lib/terminal.rb', line 164

def self.width; size.last-1 end

Instance Method Details

#background(c = ) ⇒ Object



123
# File 'lib/terminal.rb', line 123

def background c=$default[1]; $color[1]=c; show color_code(c,true) end

#color(c = $default) ⇒ Object

def color c=$default c = [c] unless c.is_a? Array and c.count == 2 show color_code(c) end



128
129
130
131
132
# File 'lib/terminal.rb', line 128

def color c=$default
  c = [c] unless c.is_a? Array and c.count == 2
  foreground c[0] if c[0] 
  background c[1] if c[1] 
end

#color_code(color, bg = false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/terminal.rb', line 104

def color_code color, bg=false
  color = color.to_sym if color.is_a? String
  case color
    when Symbol; #color = color.to_s
      if id = COLORS.index(color); "\e[#{ id + (bg ? 40 : 30) }m"
      elsif color[/^gr[ae]y\d{,2}$/]
        "\e[%i;5;%im" % [bg ? 48 : 38, 232 + (color[/\d+/].to_f/100*23).to_i]
      end
    when Integer; "\e[%i;5;%im" % [ bg ? 48 : 38, color]
    when Array; case color.count
      when 3; "\e[%i;2;%i;%i;%im" % [ bg ? 48 : 38, *color ] 
      when 2; color_code( color[0] ) +  color_code( color[1], true )
    end
  end
end

#fade(str, side = :left, num = 3) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/terminal.rb', line 69

def fade str, side=:left, num=3
  isleft = side == :left
  # chars = (side == ? 0..num : (-num-1..-2).to_a.reverse)
  chars = str[ isleft ? 0..num : -num..-1 ]
  chars = chars.chars.map.with_index do |char,id|
    id = chars.size-id unless isleft
    color_code( "grey#{(80/chars.size)*id+10}".to_sym ) + char
  end.join
  # chars.each.with_index do |pos,id|
  #   str.insert pos, color_code( "grey#{(80/chars.size)*id+10}".to_sym )
    # str.insert pos, color_code( "grey#{(100/id+3)*10}".to_sym )
  # end 
  return ( isleft ? chars + color_code($color[0]) + str[num+1..-1] :
    str[0..-num-1] + chars )

    #+ color_code( $color[1] )
  # return str
  # length.times{|i| 
  #   color [ ("grey"+((i+3)*10).to_s ).to_sym, @colors[:default][1] ]
  #   show @directory[-space+i] }    
  # color @colors[:default]
  # show @directory[-space+6..-1] 
end

#foreground(c = ) ⇒ Object



122
# File 'lib/terminal.rb', line 122

def foreground c=$default[0]; $color[0]=c; show color_code(c) end

#get_backgroundObject



120
# File 'lib/terminal.rb', line 120

def get_background; $color[1] end

#get_foregroundObject



121
# File 'lib/terminal.rb', line 121

def get_foreground; $color[0] end

#mode(name) ⇒ Object



101
# File 'lib/terminal.rb', line 101

def mode name; show mode_code(name) end

#mode_code(name) ⇒ Object



102
# File 'lib/terminal.rb', line 102

def mode_code name; if id = MODES.index(name.to_s) then "\e[#{ id }m" end end

#move(x = 0, y = 0) ⇒ Object

$>.print "\e[%iJ" % %w[down line screen].index(mode.to_s)



98
# File 'lib/terminal.rb', line 98

def move x=0,y=0; show move_code( x, y ) end

#move_code(x = 0, y = 0) ⇒ Object



99
# File 'lib/terminal.rb', line 99

def move_code x=0,y=0; "\e[%i;%if" % [ y+1, x+1 ] end

#prepare(str, max, align = :left, side = :right, fade = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/terminal.rb', line 37

def prepare str, max, align=:left, side=:right, fade=false
  stop = width = real_size( str )
  # unless str[0..max-1].ascii_only? and width == str.size
  unless str.ascii_only? and width == str.size
    stop = width = 0
    str.each_char do |c|
      if c == ?\e 
        width -= str[stop..-1][/^\x1b\[[^m]+m|/].size-1
        stop += 1
      elsif (cwidth = (c.ascii_only? ? 1 : Unicode::DisplayWidth.of(c))) +
        width >= max
        break
      else width += cwidth; stop += 1 end
    end 
  end
  # return 
  if ( space = ( max - width ) ) > 0 #.clamp 0, max
    str = [ str[0..stop-1], " " * space ]
    str.reverse! if align == :right
    str.join
  else #case align
    # when :left; str = str[0..max-1]
    # when :right; 
    str = str[side == :left ? -max..-1 : 0..max-1]
    # end
    # str = fade str, align, str.length / 8 + 1 if fade
    str = fade str, side, fade if fade
    return str
  end
  # return str.join
end

#printables(str) ⇒ Object

def printables str; str.dump.gsub(/\e[.+m|"/, '') end



93
# File 'lib/terminal.rb', line 93

def printables str; str.gsub(/\x1b\[[^m]+m|/,'') end

#real_size(str) ⇒ Object

def real_size str; printables(str).size end



95
# File 'lib/terminal.rb', line 95

def real_size str; Unicode::DisplayWidth.of( printables(str) ) end

#show(str) ⇒ Object



134
# File 'lib/terminal.rb', line 134

def show str; $>.print str end