Class: Typr::Space

Inherits:
Object
  • Object
show all
Includes:
Typr
Defined in:
lib/space.rb

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).

Examples:

box = Space.new(left: 0, top: 2, right: 0.5, bottom: -4, border: :round)
box.show

Direct Known Subclasses

Line, Stack

Constant Summary

Constants included from Typr

COLORS, COLOR_MAP, ERASE_LINE, INPUT, KEY_ESCAPE, KEY_PAGEDOWN, KEY_PAGEUP, KEY_RETURN, KEY_TAB, MIMETYPES, MODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Typr

#background, clear, #clip, #coerce_type, #color, #color_code, column, #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

Returns a new instance of Space.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/space.rb', line 81

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

#bordersObject

Returns the value of attribute borders.



41
42
43
# File 'lib/space.rb', line 41

def borders
  @borders
end

#bottom=(value) ⇒ Object (writeonly)

Sets the attribute bottom

Parameters:

  • value

    the value to set the attribute bottom to.



40
41
42
# File 'lib/space.rb', line 40

def bottom=(value)
  @bottom = value
end

#colorsObject

Returns the value of attribute colors.



41
42
43
# File 'lib/space.rb', line 41

def colors
  @colors
end

#intervalObject

Returns the value of attribute interval.



41
42
43
# File 'lib/space.rb', line 41

def interval
  @interval
end

#left=(value) ⇒ Object (writeonly)

Sets the attribute left

Parameters:

  • value

    the value to set the attribute left to.



40
41
42
# File 'lib/space.rb', line 40

def left=(value)
  @left = value
end

#marginObject

Returns the value of attribute margin.



41
42
43
# File 'lib/space.rb', line 41

def margin
  @margin
end

#right=(value) ⇒ Object (writeonly)

Sets the attribute right

Parameters:

  • value

    the value to set the attribute right to.



40
41
42
# File 'lib/space.rb', line 40

def right=(value)
  @right = value
end

#top=(value) ⇒ Object (writeonly)

Sets the attribute top

Parameters:

  • value

    the value to set the attribute top to.



40
41
42
# File 'lib/space.rb', line 40

def top=(value)
  @top = value
end

Instance Method Details

#border=(border) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/space.rb', line 69

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



49
50
51
52
# File 'lib/space.rb', line 49

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

#heightObject



44
# File 'lib/space.rb', line 44

def height; bottom - top + 1 end

#showObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/space.rb', line 54

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

#startObject



46
# File 'lib/space.rb', line 46

def start; @updater = Thread.new{ loop{show; sleep @interval }} end

#stopObject



47
# File 'lib/space.rb', line 47

def stop; @updater.terminate end

#symbolize(obj) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/space.rb', line 32

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

#widthObject



43
# File 'lib/space.rb', line 43

def width; right - left + 1 end