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")
- 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
-
#background(c = ) ⇒ Object
-
#color(c = $default) ⇒ Object
def color c=$default c = [c] unless c.is_a? Array and c.count == 2 show color_code(c) end.
-
#color_code(color, bg = false) ⇒ Object
-
#fade(str, side = :left, num = 3) ⇒ Object
-
#foreground(c = ) ⇒ Object
-
#get_background ⇒ Object
-
#get_foreground ⇒ Object
-
#mode(name) ⇒ Object
-
#mode_code(name) ⇒ Object
-
#move(x = 0, y = 0) ⇒ Object
$>.print "\e[%iJ" % %w[down line screen].index(mode.to_s).
-
#move_code(x = 0, y = 0) ⇒ Object
-
#prepare(str, max, align = :left, side = :right, fade = false) ⇒ Object
-
#printables(str) ⇒ Object
def printables str; str.dump.gsub(/\e[.+m|"/, '') end.
-
#real_size(str) ⇒ Object
def real_size str; printables(str).size end.
-
#show(str) ⇒ Object
Class Method Details
.clear(mode = :screen) ⇒ Object
def enable attr; mode attr end
def disable attr=nil; mode :reset end
def refresh; end
137
138
139
|
# File 'lib/terminal.rb', line 137
def self.clear mode=:screen
$>.print( { screen: CLEAR_SCREEN, line: DELETE_LINE }[mode] )
end
|
.column ⇒ Object
166
|
# File 'lib/terminal.rb', line 166
def self.column; position.last end
|
.exit ⇒ Object
175
176
177
178
179
180
181
182
|
# File 'lib/terminal.rb', line 175
def self.exit;
extend self
$>.print CURSOR_NORMAL;
$>.print ORIG_COLORS; color; clear
end
|
.height ⇒ Object
164
|
# File 'lib/terminal.rb', line 164
def self.height; size.first end
|
.init(default = [ :white, :black ]) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/terminal.rb', line 167
def self.init default=[ :white, :black ]
$color = $default = default
print CURSOR_INVISIBLE
system "tput smkx"
end
|
.position ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/terminal.rb', line 148
def self.position
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
141
142
143
144
145
146
|
# File 'lib/terminal.rb', line 141
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
|
.row ⇒ Object
165
|
# File 'lib/terminal.rb', line 165
def self.row; position.first end
|
.size ⇒ Object
162
|
# File 'lib/terminal.rb', line 162
def self.size; IO.console.winsize end
|
.width ⇒ Object
163
|
# File 'lib/terminal.rb', line 163
def self.width; size.last-1 end
|
Instance Method Details
#background(c = ) ⇒ Object
122
|
# File 'lib/terminal.rb', line 122
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
127
128
129
130
131
|
# File 'lib/terminal.rb', line 127
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/terminal.rb', line 103
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/terminal.rb', line 68
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
121
|
# File 'lib/terminal.rb', line 121
def foreground c=$default[0]; $color[0]=c; show color_code(c) end
|
#get_background ⇒ Object
119
|
# File 'lib/terminal.rb', line 119
def get_background; $color[1] end
|
#get_foreground ⇒ Object
120
|
# File 'lib/terminal.rb', line 120
def get_foreground; $color[0] end
|
#mode(name) ⇒ Object
100
|
# File 'lib/terminal.rb', line 100
def mode name; show mode_code(name) end
|
#mode_code(name) ⇒ Object
101
|
# File 'lib/terminal.rb', line 101
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)
97
|
# File 'lib/terminal.rb', line 97
def move x=0,y=0; show move_code( x, y ) end
|
#move_code(x = 0, y = 0) ⇒ Object
98
|
# File 'lib/terminal.rb', line 98
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
36
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
|
# File 'lib/terminal.rb', line 36
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
def printables str; str.dump.gsub(/\e[.+m|"/, '') end
92
|
# File 'lib/terminal.rb', line 92
def printables str; str.gsub(/\x1b\[[^m]+m|/,'') end
|
#real_size(str) ⇒ Object
def real_size str; printables(str).size end
94
|
# File 'lib/terminal.rb', line 94
def real_size str; Unicode::DisplayWidth.of( printables(str) ) end
|
#show(str) ⇒ Object
133
|
# File 'lib/terminal.rb', line 133
def show str; $>.print str end
|