Class: Vivlio::Starter::CLI::Metrics::LiveDisplay

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/starter/cli/metrics/live_display.rb

Overview

章別分量のライブ表示を制御する

Defined Under Namespace

Classes: Entry

Constant Summary collapse

CLEAR_LINE =
"\e[2K"

Instance Method Summary collapse

Constructor Details

#initialize(placeholders:, formatter:, show_sections: false) ⇒ LiveDisplay

Returns a new instance of LiveDisplay.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vivlio/starter/cli/metrics/live_display.rb', line 13

def initialize(placeholders:, formatter:, show_sections: false)
  @formatter = formatter
  @show_sections = show_sections
  @entries = placeholders.map do |placeholder|
    Entry.new(path: placeholder.path, placeholder:, analysis: nil)
  end
  @path_index = @entries.each_with_index.to_h { |entry, index| [entry.path, index] }
  @rendered = false
  @finalized = false
  @line_count = 0
  @current_lines = []
end

Instance Method Details

#render_emptyObject



67
68
69
70
71
72
73
# File 'lib/vivlio/starter/cli/metrics/live_display.rb', line 67

def render_empty
  clear_block if @rendered

  puts '(対象章がありません)'
  @line_count = 1
  @rendered = true
end

#render_final(analyses) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vivlio/starter/cli/metrics/live_display.rb', line 48

def render_final(analyses)
  @finalized = true
  @entries = analyses.map do |analysis|
    Entry.new(path: analysis.chapter.path, placeholder: nil, analysis:)
  end
  @path_index = @entries.each_with_index.to_h { |entry, index| [entry.path, index] }

  if @entries.empty?
    render_empty
  elsif @rendered
    redraw
  else
    @current_lines = build_lines
    @line_count = @current_lines.size
    @current_lines.each { puts it }
    @rendered = true
  end
end

#render_initialObject



26
27
28
29
30
31
32
33
# File 'lib/vivlio/starter/cli/metrics/live_display.rb', line 26

def render_initial
  return if @rendered

  @current_lines = build_lines
  @line_count = @current_lines.size
  @current_lines.each { puts it }
  @rendered = true
end

#update_analysis(path, analysis) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vivlio/starter/cli/metrics/live_display.rb', line 35

def update_analysis(path, analysis)
  return unless @rendered
  return if @finalized

  idx = @path_index[path]
  return unless idx

  entry = @entries[idx]
  entry.analysis = analysis
  entry.placeholder = nil
  redraw
end