Class: Typr::Line

Inherits:
Space
  • Object
show all
Defined in:
lib/line.rb

Overview

A single-line UI element for displaying text and collecting input.

Example

line = Line.new( top: 5, left: 10, width: 40 )
line.draw "Hello world"
line.append " more"
line.ask([ ["Name?", :string], ["Age?", :integer] ])

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

Attributes inherited from Space

#borders, #bottom, #colors, #interval, #left, #margin, #right, #top

Instance Method Summary collapse

Methods inherited from Space

#border=, #draw_border, #height, #start, #stop, #width

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) ⇒ Line

Returns a new instance of Line.



89
90
91
92
93
94
95
96
# File 'lib/line.rb', line 89

def initialize args
  @right= -1
  @prompt = ?>
  super
  @bottom = @top
  @colors = { question: :green, answer: :yellow }.merge @colors
  reset
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



15
16
17
# File 'lib/line.rb', line 15

def bindings
  @bindings
end

#dataObject

Returns the value of attribute data.



15
16
17
# File 'lib/line.rb', line 15

def data
  @data
end

#defaultObject

Returns the value of attribute default.



15
16
17
# File 'lib/line.rb', line 15

def default
  @default
end

#promptObject

Returns the value of attribute prompt.



15
16
17
# File 'lib/line.rb', line 15

def prompt
  @prompt
end

Instance Method Details

#append(str) ⇒ Object Also known as: <<

Append str to the current content and redraw.

line.draw "hello"
line.append " world"   #=> "hello world"
line << "!"            #=> "hello world!"


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

def append(str); draw @data+str end

#ask(sentence) ⇒ Object

Prompt the user with a sequence of questions. sentence is an array of [question, type] pairs. Returns a single answer or an array when multiple questions.

line.ask([ ["Name?", :string] ])             #=> "Alice"
line.ask([ ["Name?", :string], ["Ok?", ["y","n"]] ])  #=> ["Alice", "y"]


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/line.rb', line 68

def ask sentence
  answer = []
  move left, top
  Typr.clear :line
  draw @prompt
  for question, object in sentence
    color( @colors[:question] )
    append " " + question.to_s + " "
    column = Typr.column
    color( @colors[:answer] )
    case object
      when Array then answer << object.first.pick( object.last )
      else answer << Typr.read( object, @data )
    end
    return if !answer.last or answer.last == @keymap[:exit]
    move column, top
    append answer.last.to_s
  end
  return ( answer.one? ? answer.first : answer )
end

#draw(msg = @data) ⇒ Object

Render msg (String, Proc, or Space) on this line.

line.draw "status: ok"
line.draw Proc.new { show_time }


22
23
24
25
26
27
28
29
30
31
# File 'lib/line.rb', line 22

def draw msg=@data
  @data = msg
  move left, top
  color @colors[:default]
  case msg
    when Proc; msg.call
    when Space; msg.draw
    when String; show( prepare msg, width )
  end
end

#help(conf = {}) ⇒ Object

Display keybinding help in a floating grid.

line.bindings = ["Ctrl-S: Save", "Ctrl-Q: Quit"]
line.help


47
48
49
50
51
52
53
# File 'lib/line.rb', line 47

def help conf={}
  Grid.new( { input: ['']+@bindings+[''], top: 3, left:0.4,
    header: "KEYBINDINGS",
    colors: { header: [:white,:black], border:[:white,:grey10] },
    alternate: false, border: ' ' }.merge conf ).pick :none
  return
end

#resetObject

Reset the line to its default text or prompt.

line.reset


59
# File 'lib/line.rb', line 59

def reset; draw( @default || ( @bindings || [@prompt] ).join(' ') ) end