Class: Minder::TaskEditor

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/minder/cli/task_editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, parent) ⇒ TaskEditor

Returns a new instance of TaskEditor.



9
10
11
12
13
14
# File 'lib/minder/cli/task_editor.rb', line 9

def initialize(task, parent)
  @original_text = task.description.dup
  @cursor_position = 0
  @task = task
  @parent = parent
end

Instance Attribute Details

#cursor_positionObject (readonly)

Returns the value of attribute cursor_position.



7
8
9
# File 'lib/minder/cli/task_editor.rb', line 7

def cursor_position
  @cursor_position
end

Instance Method Details

#handle_char_keypress(key) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/minder/cli/task_editor.rb', line 60

def handle_char_keypress(key)
  @task.description.insert(@cursor_position, key)
  @cursor_position += 1

  changed
  notify_observers(:redraw)
end

#handle_keypress(key) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/minder/cli/task_editor.rb', line 20

def handle_keypress(key)
  return unless key

  if key.is_a?(Fixnum)
      handle_non_char_keypress(key)
  else
    handle_char_keypress(key)
  end
end

#handle_non_char_keypress(key) ⇒ Object



30
31
32
33
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
# File 'lib/minder/cli/task_editor.rb', line 30

def handle_non_char_keypress(key)
  return unless key

  data = {}

  event =
    case key
    when Curses::Key::LEFT
      @cursor_position -= 1 unless cursor_position == 0
      :redraw
    when Curses::Key::RIGHT
      @cursor_position += 1 unless cursor_position > text.length - 1
      :redraw
    when *Curses::Key::BACKSPACE, 127
      return if @cursor_position == 0
      @task.description.slice!(@cursor_position - 1)
      @cursor_position -= 1 unless cursor_position == 0
      :redraw
    when 27, 9
      @task.description = @original_text
      :stop_editing
    when 10
      data = { description: @task.description }
      :update_task
    end

  changed
  notify_observers(event, data)
end

#textObject



16
17
18
# File 'lib/minder/cli/task_editor.rb', line 16

def text
  @task.description
end