Class: Sfdown::StatusBar

Inherits:
Object
  • Object
show all
Defined in:
lib/sfdown.rb

Overview

Two-line status region pinned to the bottom of the terminal, redrawn in place with ANSI. Thread-safe: a mutex serializes updates, logs and teardown so the ticker and log() never interleave. Assumes a VT-capable terminal.

Instance Method Summary collapse

Constructor Details

#initialize(out: $stdout, width: nil) ⇒ StatusBar

Returns a new instance of StatusBar.



451
452
453
454
455
456
457
458
459
# File 'lib/sfdown.rb', line 451

def initialize(out: $stdout, width: nil)
  @out = out
  @width = width
  @mutex = Mutex.new
  @top = ""
  @bottom = ""
  @drawn = false
  @out.sync = true
end

Instance Method Details

#finishObject

Remove the bar entirely (on completion).



481
482
483
# File 'lib/sfdown.rb', line 481

def finish
  @mutex.synchronize { erase }
end

#log(msg) ⇒ Object

Print a message above the bar (scrolls into history), then redraw the bar.



472
473
474
475
476
477
478
# File 'lib/sfdown.rb', line 472

def log(msg)
  @mutex.synchronize do
    erase
    @out.puts(msg)
    draw
  end
end

#update(top, bottom) ⇒ Object

Set both lines and redraw in place.



462
463
464
465
466
467
468
469
# File 'lib/sfdown.rb', line 462

def update(top, bottom)
  @mutex.synchronize do
    @top = top
    @bottom = bottom
    erase
    draw
  end
end