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
# File 'lib/clacky/ui2/components/todo_area.rb', line 15

def initialize
  @todos = []
  @pastel = Pastel.new
  @width = TTY::Screen.width
  @height = 0  # Dynamic height based on todos
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



87
88
89
90
91
92
93
# File 'lib/clacky/ui2/components/todo_area.rb', line 87

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



118
119
120
# File 'lib/clacky/ui2/components/todo_area.rb', line 118

def clear_to_end_of_line
  print "\e[0K"
end

#flushObject

Flush output



123
124
125
# File 'lib/clacky/ui2/components/todo_area.rb', line 123

def flush
  $stdout.flush
end

#move_cursor(row, col) ⇒ Object

Move cursor to position



113
114
115
# File 'lib/clacky/ui2/components/todo_area.rb', line 113

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



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
# File 'lib/clacky/ui2/components/todo_area.rb', line 45

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

#truncate_text(text, max_width) ⇒ Object

Truncate text to fit width



97
98
99
100
101
102
103
104
105
# File 'lib/clacky/ui2/components/todo_area.rb', line 97

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



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

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

  # Calculate height: 0 if no pending, otherwise 1 line per task (up to MAX_DISPLAY_TASKS)
  if @pending_todos.empty?
    @height = 0
  else
    @height = [@pending_todos.size, MAX_DISPLAY_TASKS].min
  end
end

#update_widthObject

Update width on resize



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

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

#visible?Boolean

Check if there are todos to display

Returns:

  • (Boolean)


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

def visible?
  @height > 0
end