lib/fasten/ui/curses.rb in fasten-0.5.4 vs lib/fasten/ui/curses.rb in fasten-0.6.0

- old
+ new

@@ -1,27 +1,28 @@ # frozen_string_literal: true +require 'curses' require 'forwardable' module Fasten module UI class Curses include ::Curses extend Forwardable - def_delegators :executor, :worker_list, :task_list, :task_done_list, :task_error_list, :task_running_list, :task_waiting_list, :worker_list - def_delegators :executor, :name, :workers, :workers=, :state, :state= + def_delegators :runner, :worker_list, :task_list, :task_done_list, :task_error_list, :task_running_list, :task_waiting_list, :worker_list + def_delegators :runner, :name, :workers, :workers=, :state, :state= - attr_accessor :n_rows, :n_cols, :clear_needed, :message, :executor + attr_accessor :n_rows, :n_cols, :selected, :sel_index, :clear_needed, :message, :runner SPINNER_STR = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' SPINNER_LEN = SPINNER_STR.length PROGRESSBAR_STR = ' ▏▎▍▌▋▊▉' PROGRESSBAR_LEN = PROGRESSBAR_STR.length - def initialize(executor:) - @executor = executor + def initialize(runner:) + @runner = runner end def update setup unless @setup_done ui_keyboard @@ -97,15 +98,21 @@ if key == Curses::Key::LEFT if workers <= 1 self.message = "Can't remove 1 worker left, press [P] to pause" else self.workers -= 1 - self.message = "Decreasing max workers to #{workers}" + self.message = "Decreasing workers to #{workers}" end elsif key == Curses::Key::RIGHT self.workers += 1 - self.message = "Increasing max workers to #{workers}" + self.message = "Increasing workers to #{workers}" + elsif key == Curses::Key::DOWN + self.sel_index = sel_index ? [sel_index + 1, task_list.count - 1].min : 0 + self.selected = task_list[sel_index] + elsif key == Curses::Key::UP + self.sel_index = sel_index ? [sel_index - 1, 0].max : task_list.count - 1 + self.selected = task_list[sel_index] elsif key == 'q' self.message = 'Will quit when running tasks end' self.state = :QUITTING elsif key == 'p' self.message = 'Will pause when running tasks end' @@ -120,11 +127,11 @@ def ui_workers_summary running_count = task_running_list.count waiting_count = task_waiting_list.count workers_count = worker_list.count - "Procs: #{running_count} run #{workers_count - running_count} idle #{workers} max #{waiting_count} wait" + "Procs run: #{running_count} idle: #{workers_count - running_count} #{runner.use_threads ? 'threads' : 'processes'}: #{workers} wait: #{waiting_count}" end def ui_workers l = ui_text_aligned(1, :left, ui_workers_summary) + 1 @@ -138,17 +145,17 @@ ui_state end def ui_state - if executor.running? + if runner.running? attrs = color_pair(2) - elsif executor.pausing? + elsif runner.pausing? attrs = color_pair(1) | A_BLINK | A_STANDOUT - elsif executor.paused? + elsif runner.paused? attrs = color_pair(1) | A_STANDOUT - elsif executor.quitting? + elsif runner.quitting? attrs = color_pair(3) | A_BLINK | A_STANDOUT end l = ui_text_aligned(1, :right, state.to_s, attrs) return unless message @@ -186,23 +193,20 @@ ' ' end end def ui_task_color(task) + rev = task == selected ? A_REVERSE : 0 case task.state when :RUNNING - color_pair(1) | A_TOP + color_pair(1) | A_TOP | rev when :FAIL - color_pair(3) | A_TOP + color_pair(3) | A_TOP | rev when :DONE - color_pair(2) | A_TOP + color_pair(2) | A_TOP | rev else - if task_waiting_list.include? task - A_TOP - else - color_pair(4) | A_DIM - end + task_waiting_list.include?(task) ? A_TOP | rev : color_pair(4) | A_DIM | rev end end def ui_task_string(task, y, x, icon: nil, str: nil) setpos y, x @@ -225,11 +229,11 @@ end count_done = task_done_list.count count_total = task_list.count tl = count_total.to_s.length - col_ini = ui_text_aligned(2, :left, format("Tasks: %#{tl}d/%d", count_done, count_total)) + 1 + col_ini = ui_text_aligned(2, :left, format("Tasks %#{tl}d/%d", count_done, count_total)) + 1 col_fin = n_cols - 5 ui_text_aligned(2, :right, "#{(count_done * 100 / count_total).to_i}%") if count_total.positive? ui_progressbar(2, col_ini, col_fin, count_done, count_total) @@ -251,10 +255,10 @@ task.depends.each do |dependant_task| x = ui_task_string(dependant_task, 3 + index, x) + 1 end else x = max + 1 - last = executor.stats_last(task) + last = runner.stats_last(task) if task.dif str = format ' %.2f s', task.dif elsif last['avg'] && last['err'] str = format '≈ %.2f s ± %.2f %s', last['avg'], last['err'], task.worker&.name elsif last['avg']