Class: Clacky::ProgressIndicator

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

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false, message: nil) ⇒ ProgressIndicator

Returns a new instance of ProgressIndicator.



5
6
7
8
9
10
11
12
# File 'lib/clacky/ui2/progress_indicator.rb', line 5

def initialize(verbose: false, message: nil)
  @verbose = verbose
  @start_time = nil
  @custom_message = message
  @thinking_verb = message || THINKING_VERBS.sample
  @running = false
  @update_thread = nil
end

Instance Method Details

#finishObject



37
38
39
40
41
42
43
44
# File 'lib/clacky/ui2/progress_indicator.rb', line 37

def finish
  @running = false
  @update_thread&.join
  # Restore cursor and clear to end of line
  print "\e[u"     # Restore cursor position
  print "\e[K"     # Clear to end of line
  puts ""          # Add newline after finishing
end


47
48
49
50
51
52
53
# File 'lib/clacky/ui2/progress_indicator.rb', line 47

def print_thinking_status(text)
  print "\e[u"     # Restore cursor position (to after [..] symbol)
  print "\e[K"     # Clear to end of line from cursor
  print text
  print " "
  $stdout.flush
end

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clacky/ui2/progress_indicator.rb', line 14

def start
  @start_time = Time.now
  @running = true
  # Save cursor position after the [..] symbol
  print "\e[s"  # Save cursor position
  print_thinking_status("#{@thinking_verb}… (ctrl+c to interrupt)")

  # Start background thread to update elapsed time
  @update_thread = Thread.new do
    while @running
      sleep 0.1
      update if @running
    end
  end
end

#updateObject



30
31
32
33
34
35
# File 'lib/clacky/ui2/progress_indicator.rb', line 30

def update
  return unless @start_time

  elapsed = (Time.now - @start_time).to_i
  print_thinking_status("#{@thinking_verb}… (ctrl+c to interrupt · #{elapsed}s)")
end