Class: Typr::Space
Overview
Base layout class for all TUI widgets. Defines a viewport rectangle and border drawing.
Boundaries accept integers (absolute), floats (0..1 fraction of terminal), Procs, or negative ints (offset from opposite edge: -1 = last row/column).
Constant Summary
Constants included from Typr
COLORS, COLOR_MAP, DEFAULT_KEYS, ERASE_LINE, INPUT, KEY_ESCAPE, KEY_PAGEDOWN, KEY_PAGEUP, KEY_RETURN, KEY_TAB, MIMETYPES, MODES, MOUSE_OFF, MOUSE_ON, ORIG_COLORS, TERMINFO
Instance Attribute Summary collapse
-
#borders ⇒ Object
Returns the value of attribute borders.
-
#bottom ⇒ Object
writeonly
Sets the attribute bottom.
-
#colors ⇒ Object
Returns the value of attribute colors.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#left ⇒ Object
writeonly
Sets the attribute left.
-
#margin ⇒ Object
Returns the value of attribute margin.
-
#right ⇒ Object
writeonly
Sets the attribute right.
-
#top ⇒ Object
writeonly
Sets the attribute top.
Instance Method Summary collapse
-
#border=(border) ⇒ Object
Set border style: :light/:heavy/:double/:round, a char repeated across the box, or nil for none.
-
#draw_border(x, y, char) ⇒ Object
Draw a border char at screen (x, y); skips nil and off-screen positions.
-
#height ⇒ Object
Viewport height in cells (bottom - top + 1).
-
#initialize(args = {}) ⇒ Space
constructor
Build a widget from boundary (left/top/right/bottom), border, margin, colors, interval, and keymap.
-
#show ⇒ Object
Draw the widget's border box (no-op when no borders are set).
-
#start ⇒ Object
Redraw every
intervalseconds from a background thread. -
#stop ⇒ Object
Stop the background refresh thread.
-
#symbolize(obj) ⇒ Object
Deep-convert String keys to Symbols so string-keyed config (e.g. YAML) is honored.
-
#width ⇒ Object
Viewport width in cells (right - left + 1).
Methods included from Typr
#background, clear, #clip, #coerce_type, #color, #color_code, column, decode_mouse, #draw, exit, #fade, #foreground, #get_background, #get_foreground, height, init, #mode, #mode_code, #move, #move_code, on_resize, position, #prepare, #printables, read_key, read_line, #real_size, row, size, text_width, width, word_next, word_prev
Constructor Details
#initialize(args = {}) ⇒ Space
Build a widget from boundary (left/top/right/bottom), border, margin, colors, interval, and keymap.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/space.rb', line 92 def initialize args={} @max, @keymap, @colors, @margin = 0, {}, {}, ' ' @left, @top, @interval, @borders = 0, 0, 1, {} args.each{ |name, value| instance_variable_set ?@ + name.to_s, value } @colors = symbolize( @colors ) if @colors.is_a? Hash @keymap = { exit: KEY_ESCAPE }.merge @keymap @colors = { default: [:white, :black], border: [:white, :black] }.merge(@colors) borders = @borders.dup self.border = @border @borders.merge! borders end |
Instance Attribute Details
#borders ⇒ Object
Returns the value of attribute borders.
42 43 44 |
# File 'lib/space.rb', line 42 def borders @borders end |
#bottom=(value) ⇒ Object (writeonly)
Sets the attribute bottom
41 42 43 |
# File 'lib/space.rb', line 41 def bottom=(value) @bottom = value end |
#colors ⇒ Object
Returns the value of attribute colors.
42 43 44 |
# File 'lib/space.rb', line 42 def colors @colors end |
#interval ⇒ Object
Returns the value of attribute interval.
42 43 44 |
# File 'lib/space.rb', line 42 def interval @interval end |
#left=(value) ⇒ Object (writeonly)
Sets the attribute left
41 42 43 |
# File 'lib/space.rb', line 41 def left=(value) @left = value end |
#margin ⇒ Object
Returns the value of attribute margin.
42 43 44 |
# File 'lib/space.rb', line 42 def margin @margin end |
#right=(value) ⇒ Object (writeonly)
Sets the attribute right
41 42 43 |
# File 'lib/space.rb', line 41 def right=(value) @right = value end |
#top=(value) ⇒ Object (writeonly)
Sets the attribute top
41 42 43 |
# File 'lib/space.rb', line 41 def top=(value) @top = value end |
Instance Method Details
#border=(border) ⇒ Object
Set border style: :light/:heavy/:double/:round, a char repeated across the box, or nil for none.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/space.rb', line 78 def border=(border) chars = case border when Symbol { light: '──││┌┐└┘', heavy: '━━┃┃┏┓┗┛', double: '══║║╔╗╚╝', round: '──││╭╮╰╯' }[border] when String; if border.size == 1 then border * 8 else border.chars end when nil; [nil] * 8 end @borders = %w[top bottom left right top_left top_right bottom_left bottom_right ].map.with_index{ |part,id| [part.to_sym, chars[id]] }.to_h if chars end |
#draw_border(x, y, char) ⇒ Object
Draw a border char at screen (x, y); skips nil and off-screen positions.
55 56 57 58 |
# File 'lib/space.rb', line 55 def draw_border x,y,char draw move_code(x, y) + char unless not char or x < 0 or x > Typr.width or y < 0 or y > Typr.height end |
#height ⇒ Object
Viewport height in cells (bottom - top + 1).
47 |
# File 'lib/space.rb', line 47 def height; bottom - top + 1 end |
#show ⇒ Object
Draw the widget's border box (no-op when no borders are set).
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/space.rb', line 61 def show return if @borders.empty? color @colors[:border] draw_border(left - 1, top - 1, @borders[:top_left]) draw_border(left, top - 1, @borders[:top] * width) if @borders[:top] draw_border(right + 1, top - 1, @borders[:top_right]) (height+1).times do |pos| draw_border(left - 1, top + pos, @borders[:left] ) draw_border(right + 1, top + pos, @borders[:right] ) end draw_border(left - 1, bottom + 1, @borders[:bottom_left] ) draw_border(left, bottom + 1, @borders[:bottom] * width ) if @borders[:bottom] draw_border(right + 1, bottom + 1, @borders[:bottom_right]) end |
#start ⇒ Object
Redraw every interval seconds from a background thread.
50 |
# File 'lib/space.rb', line 50 def start; @updater = Thread.new{ loop{show; sleep @interval }} end |
#stop ⇒ Object
Stop the background refresh thread.
52 |
# File 'lib/space.rb', line 52 def stop; @updater.terminate end |
#symbolize(obj) ⇒ Object
Deep-convert String keys to Symbols so string-keyed config (e.g. YAML) is honored.
33 34 35 36 37 38 39 |
# File 'lib/space.rb', line 33 def symbolize obj case obj when Hash obj.each_with_object({}) { |(k, v), h| h[k.is_a?(String) ? k.to_sym : k] = symbolize(v) } else obj end end |
#width ⇒ Object
Viewport width in cells (right - left + 1).
45 |
# File 'lib/space.rb', line 45 def width; right - left + 1 end |