Class: Clacky::ProgressIndicator
- Inherits:
-
Object
- Object
- Clacky::ProgressIndicator
- Defined in:
- lib/clacky/ui2/progress_indicator.rb
Instance Method Summary collapse
- #finish ⇒ Object
-
#initialize(verbose: false, message: nil) ⇒ ProgressIndicator
constructor
A new instance of ProgressIndicator.
- #print_thinking_status(text) ⇒ Object
- #start ⇒ Object
- #update ⇒ Object
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 = @thinking_verb = || THINKING_VERBS.sample @running = false @update_thread = nil end |
Instance Method Details
#finish ⇒ Object
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 |
#print_thinking_status(text) ⇒ Object
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 |
#start ⇒ Object
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 |
#update ⇒ Object
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 |