Class: Minder::Frame

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Frame

Returns a new instance of Frame.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/minder/cli/frame.rb', line 18

def initialize(**options)
  height = options.fetch(:height, 3)
  width = options.fetch(:width, 40)
  top = options.fetch(:top, 0)
  left = options.fetch(:left, 0)
  task_manager = options.fetch(:task_manager)
  pomodoro_runner = options.fetch(:pomodoro_runner)

  @focused = false
  @hidden = false
  @has_cursor = false
  self.pomodoro_runner = pomodoro_runner
  self.task_manager = task_manager
  self.min_height = height

  self.height = height
  self.width = width
  self.top = top
  self.left = left

  self.window = build_window
  window.keypad(true)
  self.lines = []
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def height
  @height
end

#leftObject

Returns the value of attribute left.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def left
  @left
end

#linesObject

Returns the value of attribute lines.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def lines
  @lines
end

#min_heightObject

Returns the value of attribute min_height.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def min_height
  @min_height
end

#pomodoro_runnerObject

Returns the value of attribute pomodoro_runner.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def pomodoro_runner
  @pomodoro_runner
end

#task_managerObject

Returns the value of attribute task_manager.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def task_manager
  @task_manager
end

#topObject

Returns the value of attribute top.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def top
  @top
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def width
  @width
end

#windowObject

Returns the value of attribute window.



8
9
10
# File 'lib/minder/cli/frame.rb', line 8

def window
  @window
end

Instance Method Details

#build_windowObject



43
44
45
# File 'lib/minder/cli/frame.rb', line 43

def build_window
  Curses::Window.new(min_height, width, top, left)
end

#eraseObject



135
136
137
138
139
140
141
# File 'lib/minder/cli/frame.rb', line 135

def erase
  height.times do |index|
    window.setpos(index, 0)
    window.addstr(' ' * width )
  end
  window_refresh
end

#focusObject



47
48
49
50
# File 'lib/minder/cli/frame.rb', line 47

def focus
  @focused = true
  @has_cursor = true
end

#focused?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/minder/cli/frame.rb', line 57

def focused?
  @focused
end

#handle_char_keypress(key) ⇒ Object



171
172
# File 'lib/minder/cli/frame.rb', line 171

def handle_char_keypress(key)
end

#handle_keypress(key) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/minder/cli/frame.rb', line 153

def handle_keypress(key)
  return unless key

  if key.is_a?(Fixnum)
    if key == 9 # tab
      changed
      notify_observers(:switch_focus)
    else
      handle_non_char_keypress(key)
    end
  else
    handle_char_keypress(key)
  end
end

#handle_non_char_keypress(key) ⇒ Object



168
169
# File 'lib/minder/cli/frame.rb', line 168

def handle_non_char_keypress(key)
end

#has_cursor?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/minder/cli/frame.rb', line 82

def has_cursor?
  @has_cursor
end

#hidden?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/minder/cli/frame.rb', line 61

def hidden?
  @hidden
end

#hideObject



65
66
67
68
# File 'lib/minder/cli/frame.rb', line 65

def hide
  erase
  @hidden = true
end

#listenObject



86
87
88
89
# File 'lib/minder/cli/frame.rb', line 86

def listen
  window.timeout = 0
  handle_keypress(window.getch)
end

#move(top, left) ⇒ Object



78
79
80
# File 'lib/minder/cli/frame.rb', line 78

def move(top, left)
  window.move(top, left)
end

#parse_templateObject



102
103
104
105
# File 'lib/minder/cli/frame.rb', line 102

def parse_template
  b = binding
  self.lines = ERB.new(template).result(b).split("\n")
end


147
148
149
150
151
# File 'lib/minder/cli/frame.rb', line 147

def print_line(text)
  text = text[0,width - 2]
  remainder = width - 2 - text.length
  window.addstr(text + ' ' * remainder)
end

#refreshObject



91
92
93
94
95
96
# File 'lib/minder/cli/frame.rb', line 91

def refresh
  return if @hidden
  parse_template
  set_text
  window_refresh
end

#resizeObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/minder/cli/frame.rb', line 107

def resize
  erase

  self.width = Curses.cols
  if lines.length >= min_height - 2
    self.height = lines.length + 2
  else
    self.height = min_height
  end

  window.resize(height, width)
  window_refresh
end

#set_cursor_positionObject



143
144
145
# File 'lib/minder/cli/frame.rb', line 143

def set_cursor_position
  window.setpos(1, 0)
end

#set_textObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/minder/cli/frame.rb', line 121

def set_text
  window.box(?|, ?-)
  height.times do |index|
    next if index >= height - 2
    window.setpos(index + 1, 1)
    line = lines[index]
    if line
      print_line(line)
    else
      print_line('')
    end
  end
end

#templateObject

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/minder/cli/frame.rb', line 74

def template
  raise NotImplementedError
end

#unfocusObject



52
53
54
55
# File 'lib/minder/cli/frame.rb', line 52

def unfocus
  @focused = false
  @has_cursor = false
end

#unhideObject



70
71
72
# File 'lib/minder/cli/frame.rb', line 70

def unhide
  @hidden = false
end

#window_refreshObject



98
99
100
# File 'lib/minder/cli/frame.rb', line 98

def window_refresh
  window.refresh
end