Class: TTY::Draft
- Inherits:
-
Object
- Object
- TTY::Draft
- Defined in:
- lib/tty-draft.rb
Constant Summary collapse
- VERSION =
'0.1.1'
Instance Attribute Summary collapse
-
#done ⇒ Object
readonly
Returns the value of attribute done.
Class Method Summary collapse
Instance Method Summary collapse
- #edited ⇒ Object
- #editing ⇒ Object
-
#initialize ⇒ Draft
constructor
A new instance of Draft.
- #keypress(event) ⇒ Object
Constructor Details
#initialize ⇒ Draft
Returns a new instance of Draft.
31 32 33 34 35 |
# File 'lib/tty-draft.rb', line 31 def initialize @chars = [[]] @row = 0 @col = 0 end |
Instance Attribute Details
#done ⇒ Object (readonly)
Returns the value of attribute done.
29 30 31 |
# File 'lib/tty-draft.rb', line 29 def done @done end |
Class Method Details
.gets ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/tty-draft.rb', line 9 def gets draft = new reader = Reader.new reader.subscribe(draft) live = Live.new loop do reader.read_char if draft.done live.update('') reader.unsubscribe(draft) print live.show return draft.edited end live.update(draft.editing) print live.hide end end |
Instance Method Details
#edited ⇒ Object
80 81 82 |
# File 'lib/tty-draft.rb', line 80 def edited to_string(@chars) end |
#editing ⇒ Object
84 85 86 87 88 89 |
# File 'lib/tty-draft.rb', line 84 def editing copy = @chars.dup copy[@row] = current_row.dup copy[@row][@col] = "\e[7m#{copy[@row][@col] || ' '}\e[27m" to_string(copy) end |
#keypress(event) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tty-draft.rb', line 37 def keypress(event) @done = false case event.key.name when :ctrl_d, :ctrl_z @done = true when :backspace if @col > 0 @col -= 1 return delete_char end if @row > 0 @col = @chars[@row - 1].size @chars[@row - 1] += delete_row return @row -= 1 end when :delete return delete_char if @col < current_row.size @chars[@row] = current_row + ((@row += 1) && delete_row.tap{@row -= 1}) when :left return @col -= 1 if @col > 0 return if @row <= 0 @row -= 1 @col = current_row.size when :right return @col += 1 if @col < current_row.size return if (@row + 1) >= @chars.size @row += 1 @col = 0 when :up @row -= 1 if @row > 0 fix_col when :down @row += 1 if (@row + 1) < @chars.size fix_col when :return insert_row else return insert_row if event.value == "\n" current_row.insert(@col, event.value) @col += 1 end end |