Class: TTY::Draft
- Inherits:
-
Object
- Object
- TTY::Draft
- Defined in:
- lib/tty-draft.rb
Constant Summary collapse
- VERSION =
'0.1.2'
Instance Attribute Summary collapse
-
#done ⇒ Object
readonly
Returns the value of attribute done.
Instance Method Summary collapse
- #gets ⇒ Object
-
#initialize(prompt: ->(n){''}) ⇒ Draft
constructor
A new instance of Draft.
- #keypress(event) ⇒ Object
Constructor Details
#initialize(prompt: ->(n){''}) ⇒ Draft
Returns a new instance of Draft.
10 11 12 13 |
# File 'lib/tty-draft.rb', line 10 def initialize(prompt: ->(n){''}) @prompt = prompt new_draft end |
Instance Attribute Details
#done ⇒ Object (readonly)
Returns the value of attribute done.
8 9 10 |
# File 'lib/tty-draft.rb', line 8 def done @done end |
Instance Method Details
#gets ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tty-draft.rb', line 15 def gets reader = Reader.new reader.subscribe(self) live = Live.new live.update(edited) loop do reader.read_char if done live.update(edited) reader.unsubscribe(self) puts live.show return to_string(@chars).tap{new_draft} end live.update(editing) print live.hide end end |
#keypress(event) ⇒ Object
34 35 36 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 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tty-draft.rb', line 34 def keypress(event) @done = false case event.key.name when :ctrl_d, :ctrl_z history << @chars @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 if @row > 0 @row -= 1 return fix_col end if @work > 0 return load_history(@work -= 1) end when :down if (@row + 1) < @chars.size @row += 1 return fix_col end if @work + 1 < @workspaces.size return load_history(@work += 1) end when :home, :ctrl_a @col = 0 when :end, :ctrl_e @col = current_row.size when :return insert_row else return insert_row if event.value == "\n" current_row.insert(@col, event.value) @col += 1 end end |