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.



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

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.



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

def height
  @height
end

#todosObject (readonly)

Returns the value of attribute todos.



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

def todos
  @todos
end

Instance Method Details

#clearObject

Clear the area



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

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



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

def clear_to_end_of_line
  print "\e[0K"
end

#flushObject

Flush output



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

def flush
  $stdout.flush
end

#hideObject

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



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

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

#move_cursor(row, col) ⇒ Object

Move cursor to position



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

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



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

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



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

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

#truncate_text(text, max_width) ⇒ Object

Truncate text to fit width



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

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



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

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



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

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

#visible?Boolean

Check if there are todos to display

Returns:

  • (Boolean)


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

def visible?
  @height > 0
end