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

Direct Known Subclasses

Line, Stack

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

Instance Method Summary collapse

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

#bordersObject

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

Parameters:

  • value

    the value to set the attribute bottom to.



32
33
34
# File 'lib/space.rb', line 32

def bottom=(value)
  @bottom = value
end

#colorsObject

Returns the value of attribute colors.



33
34
35
# File 'lib/space.rb', line 33

def colors
  @colors
end

#intervalObject

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

Parameters:

  • value

    the value to set the attribute left to.



32
33
34
# File 'lib/space.rb', line 32

def left=(value)
  @left = value
end

#marginObject

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

Parameters:

  • value

    the value to set the attribute right to.



32
33
34
# File 'lib/space.rb', line 32

def right=(value)
  @right = value
end

#top=(value) ⇒ Object (writeonly)

Sets the attribute top

Parameters:

  • value

    the value to set the attribute top to.



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

#drawObject



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

#heightObject



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

def height; bottom - top + 1 end

#startObject



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

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

#stopObject



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

def stop; @updater.terminate end

#widthObject



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

def width; right - left + 1 end