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, INPUT, KEY_DELETE, KEY_ENTER, KEY_ESCAPE, KEY_INSERT, KEY_PAGEDOWN, KEY_PAGEUP, KEY_RETURN, KEY_TAB, MIMETYPES, MODES
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
- #draw ⇒ Object
- #draw_border(x, y, char) ⇒ Object
- #height ⇒ Object
-
#initialize(args = {}) ⇒ Space
constructor
A new instance of Space.
- #start ⇒ Object
- #stop ⇒ Object
- #width ⇒ Object
Methods included from Typr
#background, clear, #color, #color_code, column, exit, #fade, #foreground, #get_background, #get_foreground, height, init, #mode, #mode_code, #move, #move_code, position, #prepare, #printables, read, #real_size, row, #show, size, width
Constructor Details
#initialize(args = {}) ⇒ Space
Returns a new instance of Space.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/space.rb', line 73 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 } @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.
33 34 35 |
# File 'lib/space.rb', line 33 def borders @borders end |
#bottom=(value) ⇒ Object (writeonly)
Sets the attribute bottom
32 33 34 |
# File 'lib/space.rb', line 32 def bottom=(value) @bottom = value end |
#colors ⇒ Object
Returns the value of attribute colors.
33 34 35 |
# File 'lib/space.rb', line 33 def colors @colors end |
#interval ⇒ Object
Returns the value of attribute interval.
33 34 35 |
# File 'lib/space.rb', line 33 def interval @interval end |
#left=(value) ⇒ Object (writeonly)
Sets the attribute left
32 33 34 |
# File 'lib/space.rb', line 32 def left=(value) @left = value end |
#margin ⇒ Object
Returns the value of attribute margin.
33 34 35 |
# File 'lib/space.rb', line 33 def margin @margin end |
#right=(value) ⇒ Object (writeonly)
Sets the attribute right
32 33 34 |
# File 'lib/space.rb', line 32 def right=(value) @right = value end |
#top=(value) ⇒ Object (writeonly)
Sets the attribute top
32 33 34 |
# File 'lib/space.rb', line 32 def top=(value) @top = value end |
Instance Method Details
#border=(border) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/space.rb', line 61 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 ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/space.rb', line 46 def draw 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 |
#draw_border(x, y, char) ⇒ Object
41 42 43 44 |
# File 'lib/space.rb', line 41 def draw_border x,y,char show 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
36 |
# File 'lib/space.rb', line 36 def height; bottom - top + 1 end |
#start ⇒ Object
38 |
# File 'lib/space.rb', line 38 def start; @updater = Thread.new{ loop{draw; sleep @interval }} end |
#stop ⇒ Object
39 |
# File 'lib/space.rb', line 39 def stop; @updater.terminate end |
#width ⇒ Object
35 |
# File 'lib/space.rb', line 35 def width; right - left + 1 end |