Class: Clacky::UI2::Components::TodoArea

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/ui2/components/todo_area.rb

Overview

TodoArea displays active todos above the separator line

Constant Summary collapse

MAX_DISPLAY_TASKS =

Show current + next 2 tasks

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTodoArea

Returns a new instance of TodoArea.



16
17
18
19
20
21
22
23
24
25
# File 'lib/clacky/ui2/components/todo_area.rb', line 16

def initialize
  @todos = []
  @pending_todos = []
  @completed_count = 0
  @total_count = 0
  @pastel = Pastel.new
  @width = TTY::Screen.width
  @height = 0  # Dynamic height based on todos
  @hidden = false
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/clacky/ui2/components/todo_area.rb', line 11

def height
  @height
end

#todosObject (readonly)

Returns the value of attribute todos.



12
13
14
# File 'lib/clacky/ui2/components/todo_area.rb', line 12

def todos
  @todos
end

Instance Method Details

#clearObject

Clear the area



108
109
110
111
112
113
114
# File 'lib/clacky/ui2/components/todo_area.rb', line 108

def clear
  @todos = []
  @pending_todos = []
  @completed_count = 0
  @total_count = 0
  @height = 0
end

#clear_to_end_of_lineObject

Clear from cursor to end of line



139
140
141
# File 'lib/clacky/ui2/components/todo_area.rb', line 139

def clear_to_end_of_line
  print "\e[0K"
end

#flushObject

Flush output



144
145
146
# File 'lib/clacky/ui2/components/todo_area.rb', line 144

def flush
  $stdout.flush
end

#hideObject

Hide the area without discarding todos data; show again to restore.



39
40
41
42
43
# File 'lib/clacky/ui2/components/todo_area.rb', line 39

def hide
  return if @hidden
  @hidden = true
  @height = 0
end

#move_cursor(row, col) ⇒ Object

Move cursor to position



134
135
136
# File 'lib/clacky/ui2/components/todo_area.rb', line 134

def move_cursor(row, col)
  print "\e[#{row + 1};#{col + 1}H"
end

#render(start_row:) ⇒ Object

Render todos area

Parameters:

  • start_row (Integer)

    Screen row to start rendering



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/clacky/ui2/components/todo_area.rb', line 66

def render(start_row:)
  return unless visible?

  update_width

  # Render each task on separate line
  tasks_to_show = @pending_todos.take(MAX_DISPLAY_TASKS)
  
  tasks_to_show.each_with_index do |task, index|
    move_cursor(start_row + index, 0)
    
    # Build the line content
    line_content = if index == 0
      # First line: Task [2/4]: #3 - Current task description
      progress = "#{@completed_count}/#{@total_count}"
      prefix = "Task [#{progress}]: "
      task_text = "##{task[:id]} - #{task[:task]}"
      available_width = @width - prefix.length - 2
      truncated_task = truncate_text(task_text, available_width)
      
      "#{@pastel.cyan(prefix)}#{truncated_task}"
    else
      # Subsequent lines: -> Next: #4 - Next task description
      label = index == 1 ? "Next" : "After"
      prefix = "-> #{label}: "
      task_text = "##{task[:id]} - #{task[:task]}"
      available_width = @width - prefix.length - 2
      truncated_task = truncate_text(task_text, available_width)
      
      "#{@pastel.dim(prefix)}#{@pastel.dim(truncated_task)}"
    end

    # Use carriage return and print content directly (overwrite existing content)
    print "\r#{line_content}"
    # Clear any remaining characters from previous render if line is shorter
    clear_to_end_of_line
  end

  flush
end

#showObject



45
46
47
48
49
# File 'lib/clacky/ui2/components/todo_area.rb', line 45

def show
  return unless @hidden
  @hidden = false
  recalc_height
end

#truncate_text(text, max_width) ⇒ Object

Truncate text to fit width



118
119
120
121
122
123
124
125
126
# File 'lib/clacky/ui2/components/todo_area.rb', line 118

def truncate_text(text, max_width)
  return "" if text.nil?

  if text.length > max_width
    text[0...(max_width - 3)] + "..."
  else
    text
  end
end

#update(todos) ⇒ Object

Update todos list

Parameters:

  • todos (Array<Hash>)

    Array of todo items



29
30
31
32
33
34
35
36
# File 'lib/clacky/ui2/components/todo_area.rb', line 29

def update(todos)
  @todos = todos || []
  @pending_todos = @todos.select { |t| t[:status] == "pending" }
  @completed_count = @todos.count { |t| t[:status] == "completed" }
  @total_count = @todos.size

  recalc_height
end

#update_widthObject

Update width on resize



129
130
131
# File 'lib/clacky/ui2/components/todo_area.rb', line 129

def update_width
  @width = TTY::Screen.width
end

#visible?Boolean

Check if there are todos to display

Returns:

  • (Boolean)


60
61
62
# File 'lib/clacky/ui2/components/todo_area.rb', line 60

def visible?
  @height > 0
end