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 =
(IO.new IO.sysopen("/dev/tty", "r")) rescue $stdin
- KEY_ESCAPE =
"\e"
- KEY_RETURN =
CARRIAGE_RETURN
- KEY_TAB =
"\t"
- KEY_PAGEDOWN =
KEY_NPAGE
- KEY_PAGEUP =
KEY_PPAGE
- MODES =
%i[ reset bold 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
110
111
112
|
# File 'lib/terminal.rb', line 110
def self.clear mode=:screen
$>.print( { screen: CLEAR_SCREEN, line: DELETE_LINE }[mode] )
end
|
.column ⇒ Object
141
|
# File 'lib/terminal.rb', line 141
def self.column; position.last end
|
.exit ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/terminal.rb', line 152
def self.exit;
extend self
if $stdin.tty?
$>.print CURSOR_NORMAL;
$>.print ORIG_COLORS; color; clear
end
end
|
.height ⇒ Object
139
|
# File 'lib/terminal.rb', line 139
def self.height; size.first - 1 end
|
.init(default = [ :white, :black ]) ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/terminal.rb', line 145
def self.init default=[ :white, :black ]
$color = $default = default
if $stdin.tty?
print CURSOR_INVISIBLE
system "tput smkx"
end
end
|
.on_resize(&block) ⇒ Object
142
143
144
|
# File 'lib/terminal.rb', line 142
def self.on_resize &block
trap(:WINCH, &block)
end
|
.position ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/terminal.rb', line 124
def self.position
return [0, 0] unless $stdin.tty?
result = ''
$stdin.raw do |stdin|
$stdout << USER7
$stdout.flush
until (char = stdin.getc) == 'R'
result << char if char
end
end
result[/[\d;]+/].split(?;).map &:to_i
end
|
.read(obj, prompt = '') ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/terminal.rb', line 114
def self.read obj, prompt=''
case obj
when :key
return INPUT.raw{ |tty| tty.sysread 6 } rescue nil unless INPUT.tty?
INPUT.raw{ |tty| tty.sysread 6 } rescue return
when :line
$stdin.tty? ? (line=Readline.readline prompt; print CURSOR_INVISIBLE; line) : $stdin.gets&.chomp
end
end
|
.row ⇒ Object
140
|
# File 'lib/terminal.rb', line 140
def self.row; position.first end
|
.size ⇒ Object
137
|
# File 'lib/terminal.rb', line 137
def self.size; IO.console&.winsize || [24, 80] end
|
.width ⇒ Object
138
|
# File 'lib/terminal.rb', line 138
def self.width; size.last-1 end
|
Instance Method Details
#background(c = ) ⇒ Object
102
|
# File 'lib/terminal.rb', line 102
def background c=$default[1]; $color[1]=c; show color_code(c,true) end
|
#coerce_type(value) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/terminal.rb', line 63
def coerce_type value
s = value.to_s
return nil if s.empty? or /\A(?:nil|NULL)\z/i.match(s)
case value
when /^-?[\d]+$/ then s.to_i
when /^-?\d*[\.\,]\d+$/ then s.to_f
when /\byes\z/i then true
when /\bno(ot)?\z/i then false
when /true\z/i then true
when /false\z/i then false
else value
end
end
|
#color(c = $default) ⇒ Object
103
104
105
106
107
|
# File 'lib/terminal.rb', line 103
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/terminal.rb', line 83
def color_code color, bg=false
color = color.to_sym if color.is_a? String
case color
when Symbol;
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
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/terminal.rb', line 49
def fade str, side=:left, num=3
isleft = side == :left
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
return ( isleft ? chars + color_code($color[0]) + str[num+1..-1] :
str[0..-num-1] + chars )
end
|
#foreground(c = ) ⇒ Object
101
|
# File 'lib/terminal.rb', line 101
def foreground c=$default[0]; $color[0]=c; show color_code(c) end
|
#get_background ⇒ Object
99
|
# File 'lib/terminal.rb', line 99
def get_background; $color[1] end
|
#get_foreground ⇒ Object
100
|
# File 'lib/terminal.rb', line 100
def get_foreground; $color[0] end
|
#mode(name) ⇒ Object
80
|
# File 'lib/terminal.rb', line 80
def mode name; show mode_code(name) end
|
#mode_code(name) ⇒ Object
81
|
# File 'lib/terminal.rb', line 81
def mode_code name; if id = MODES.index(name.to_s) then "\e[#{ id }m" end end
|
#move(x = 0, y = 0) ⇒ Object
77
|
# File 'lib/terminal.rb', line 77
def move x=0,y=0; show move_code( x, y ) end
|
#move_code(x = 0, y = 0) ⇒ Object
78
|
# File 'lib/terminal.rb', line 78
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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/terminal.rb', line 24
def prepare str, max, align=:left, side=:right, fade=false
stop = width = real_size( str )
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
if ( space = ( max - width ) ) > 0
str = [ str[0..stop-1], " " * space ]
str.reverse! if align == :right
str.join
else
str = str[side == :left ? -max..-1 : 0..max-1]
str = fade str, side, fade if fade
return str
end
end
|
#printables(str) ⇒ Object
60
|
# File 'lib/terminal.rb', line 60
def printables str; str.gsub(/\x1b\[[^m]+m|/,'') end
|
#real_size(str) ⇒ Object
61
|
# File 'lib/terminal.rb', line 61
def real_size str; Unicode::DisplayWidth.of( printables(str) ) end
|
#show(str) ⇒ Object
109
|
# File 'lib/terminal.rb', line 109
def show str; $>.print str end
|