Class: Lemans::CLI::BoardReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/lemans/cli/board_reporter.rb

Overview

A live table — tasks down, models across — redrawn in place, each cell one glyph per attempt: · queued, spinner running, ✔ solved, ✘ scored short, ! invalid.

Constant Summary collapse

FRAMES =
%w[         ].freeze
REDRAW_SEC =
0.1
MAX_DETAIL_CHARS =
200
GREEN =
"\e[32m"
RED =
"\e[31m"
YELLOW =
"\e[33m"
DIM =
"\e[2m"
RESET =
"\e[0m"

Instance Method Summary collapse

Constructor Details

#initialize(tasks:, models:, attempts:, total: nil, out: $stderr) ⇒ BoardReporter

Returns a new instance of BoardReporter.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lemans/cli/board_reporter.rb', line 18

def initialize(tasks:, models:, attempts:, total: nil, out: $stderr)
  @tasks = tasks
  @models = models.map { short(_1) }
  @attempts = attempts
  # Injected when known: under --resume the schedule is smaller than
  # tasks × models × attempts.
  @total = total || (tasks.size * models.size * attempts)
  @out = out
  @lock = Mutex.new
  @cells = Hash.new { |cells, key| cells[key] = Array.new(@attempts, :queued) }
  @drawn = 0
  @frame = 0
  @done = 0
  @in_flight = 0
end

Instance Method Details

#record(event, data) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lemans/cli/board_reporter.rb', line 44

def record(event, data)
  @lock.synchronize do
    case event
    when :started
      @in_flight += 1
      cell(data)[data[:index] - 1] = :running
    when :finished
      @in_flight -= 1
      @done += 1
      cell(data)[data[:index] - 1] = data
      announce_error(data)
    when :interrupted
      erase
      @out.puts "#{YELLOW}^C — waiting for #{data[:in_flight]} in-flight trial(s), ^C again to abandon#{RESET}"
      @drawn = 0
    end
  end
end

#startObject



34
35
36
37
38
39
40
41
42
# File 'lib/lemans/cli/board_reporter.rb', line 34

def start
  @thread = Thread.new do
    loop do
      @lock.synchronize { draw }
      sleep REDRAW_SEC
    end
  end
  self
end

#stopObject



63
64
65
66
67
68
69
70
# File 'lib/lemans/cli/board_reporter.rb', line 63

def stop
  return unless @thread

  @thread.kill
  @thread = nil
  @lock.synchronize { draw } # the final frame stays on screen
  @out.puts
end