Class: Kotoshu::Cli::DisplayFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/cli/display_formatter.rb

Overview

Formats output for interactive review mode.

Provides methods to display errors, context, suggestions, and navigation prompts in a user-friendly CLI format.

Examples:

Displaying an error

formatter = DisplayFormatter.new(verbose: true)
puts formatter.issue_screen(error, index: 1, total: 10)

Constant Summary collapse

COLORS =

ANSI color codes for terminal output

{
  error: "\e[31m",      # Red
  warning: "\e[33m",    # Yellow
  success: "\e[32m",    # Green
  info: "\e[36m",       # Cyan
  dim: "\e[2m",         # Dim
  bold: "\e[1m",        # Bold
  reset: "\e[0m"        # Reset
}.freeze
CONFIDENCE_LABELS =

Confidence level indicators

{
  high: '✓ High',
  medium: '~ Medium',
  low: '? Low'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false, color_enabled: nil) ⇒ DisplayFormatter

Create a new display formatter.

Parameters:

  • verbose (Boolean) (defaults to: false)

    Enable verbose output

  • color_enabled (Boolean) (defaults to: nil)

    Enable ANSI colors (default: true for TTY)



41
42
43
44
# File 'lib/kotoshu/cli/display_formatter.rb', line 41

def initialize(verbose: false, color_enabled: nil)
  @verbose = verbose
  @color_enabled = color_enabled.nil? ? $stdout.tty? : color_enabled
end

Instance Attribute Details

#color_enabledObject (readonly)

Returns the value of attribute color_enabled.



35
36
37
# File 'lib/kotoshu/cli/display_formatter.rb', line 35

def color_enabled
  @color_enabled
end

#verboseObject (readonly)

Returns the value of attribute verbose.



35
36
37
# File 'lib/kotoshu/cli/display_formatter.rb', line 35

def verbose
  @verbose
end

Instance Method Details

#error(message) ⇒ String

Display error message.

Parameters:

  • message (String)

    Error message

Returns:

  • (String)

    Formatted error



348
349
350
# File 'lib/kotoshu/cli/display_formatter.rb', line 348

def error(message)
  colorize("Error: #{message}", :error)
end

#export_summary(navigation, export_path) ⇒ String

Display export summary.

Parameters:

  • navigation (NavigationManager)

    Navigation state

  • export_path (String)

    Path to export file

Returns:

  • (String)

    Formatted export summary



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/kotoshu/cli/display_formatter.rb', line 301

def export_summary(navigation, export_path)
  corrections = navigation.export_corrections

  lines = []
  lines << ""
  lines << colorize("═══ Export Summary ═══", :bold)
  lines << ""
  lines << "Exported #{corrections.size} corrections to:"
  lines << colorize(export_path, :info)
  lines << ""

  if corrections.any? && @verbose
    lines << colorize("Corrections:", :bold)
    corrections.first(10).each do |corr|
      lines << "  Line #{corr[:line]}: #{corr[:original]}#{corr[:replacement]}"
    end

    if corrections.size > 10
      lines << colorize("  ... and #{corrections.size - 10} more", :dim)
    end

    lines << ""
  end

  lines.join("\n")
end

#format_suggestions(suggestions, max_display: 5) ⇒ String

Format suggestions list with confidence scores.

Parameters:

  • suggestions (Array<Suggestion>)

    List of suggestions

  • max_display (Integer) (defaults to: 5)

    Maximum suggestions to show (default: 5)

Returns:

  • (String)

    Formatted suggestions



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/kotoshu/cli/display_formatter.rb', line 179

def format_suggestions(suggestions, max_display: 5)
  return colorize("No suggestions", :dim) unless suggestions&.any?

  lines = []
  suggestions.first(max_display).each_with_index do |suggestion, idx|
    number = colorize((idx + 1).to_s + '.', :info)
    word = colorize(suggestion.word, suggestion.high_confidence? ? :success : :warning)
    confidence = "(#{(suggestion.confidence * 100).round(0)}%)"

    line = "  #{number} #{word} #{confidence}"
    line += " [#{suggestion.source}]" if suggestion.source && @verbose
    lines << line
  end

  if suggestions.size > max_display
    remaining = suggestions.size - max_display
    lines << colorize("  ... and #{remaining} more", :dim)
  end

  lines.join("\n")
end

#help_screenString

Display help screen.

Returns:

  • (String)

    Formatted help text



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/kotoshu/cli/display_formatter.rb', line 259

def help_screen
  lines = []
  lines << ""
  lines << colorize("═══ Interactive Review Help ═══", :bold)
  lines << ""
  lines << colorize("Navigation Commands:", :bold)
  lines << "  [Enter] or [n]    Move to next error"
  lines << "  [b]               Move to previous error"
  lines << "  [j] <number>      Jump to error by number"
  lines << "  [f]               Jump to first error"
  lines << "  [l]               Jump to last error"
  lines << ""
  lines << colorize("Error Actions:", :bold)
  lines << "  [1-9]             Accept suggestion by number"
  lines << "  [s]               Skip current error"
  lines << "  [e]               Edit custom replacement"
  lines << ""
  lines << colorize("Display Commands:", :bold)
  lines << "  [l]               List all errors with status"
  lines << "  [t]               Toggle show/hide context"
  lines << "  [v]               Toggle verbose mode"
  lines << "  [?]               Show this summary"
  lines << ""
  lines << colorize("Program Commands:", :bold)
  lines << "  [q]               Quit and save changes"
  lines << "  [Q]               Quit without saving"
  lines << "  [!]               Export corrections to file"
  lines << ""
  lines << colorize("Filter Commands:", :bold)
  lines << "  [c] <level>       Filter by confidence (high, medium, low)"
  lines << "  [y] <type>        Filter by error type"
  lines << "  [a]               Show all errors (clear filters)"
  lines << ""

  lines.join("\n")
end

#highlight_error(error) ⇒ String

Highlight the error word within its context.

Parameters:

  • error (SemanticError)

    The error to highlight

Returns:

  • (String)

    Highlighted error with context



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/kotoshu/cli/display_formatter.rb', line 158

def highlight_error(error)
  return error.original unless error.context

  ctx = error.context
  highlighted = ctx.current.gsub(/\b#{Regexp.escape(error.original)}\b/i) do |match|
    colorize(match, :error)
  end

  # Truncate if too long
  if highlighted.length > 100
    "..." + highlighted[-100..-1]
  else
    highlighted
  end
end

#issue_screen(error, index:, total:, show_context: true) ⇒ String

Display individual error screen.

Shows the error with context, suggestions, and action prompt.

Parameters:

  • error (SemanticError)

    The error to display

  • index (Integer)

    Current error index (1-based)

  • total (Integer)

    Total number of errors

  • show_context (Boolean) (defaults to: true)

    Show context window (default: true)

Returns:

  • (String)

    Formatted error screen



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kotoshu/cli/display_formatter.rb', line 112

def issue_screen(error, index:, total:, show_context: true)
  lines = []
  lines << ""
  lines << colorize("" * 70, :bold)
  lines << colorize("Error #{index} of #{total}", :bold) + "#{error_type_label(error.error_type)}"
  lines << colorize("" * 70, :bold)
  lines << ""

  # Error location
  lines << colorize("Location:", :bold) + " #{error.location}"
  lines << ""

  # Error with highlighting
  lines << colorize("Found:", :error) + " #{highlight_in_context(error)}"
  lines << ""

  # Context window
  if show_context && error.context
    lines << colorize("Context:", :bold)
    lines << format_context(error.context)
    lines << ""
  end

  # Confidence indicator
  conf_label = confidence_label(error.confidence)
  lines << colorize("Confidence:", :bold) + " #{conf_label} (#{(error.confidence * 100).round(1)}%)"
  lines << ""

  # Suggestions
  if error.suggestions&.any?
    lines << colorize("Suggestions:", :bold)
    lines << format_suggestions(error.suggestions)
    lines << ""
  end

  # Action prompt
  lines << colorize("Actions:", :bold) + " [Enter] Next [1-#{error.suggestions&.size || 0}] Accept [s] Skip [b] Back [q] Quit"
  lines << ""

  lines.join("\n")
end

#list_all_errors(navigation) ⇒ String

Display all errors with status indicators.

Parameters:

Returns:

  • (String)

    Formatted error list



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/kotoshu/cli/display_formatter.rb', line 205

def list_all_errors(navigation)
  lines = []
  lines << ""
  lines << colorize("All Errors (#{navigation.errors.size})", :bold)
  lines << "" * 70

  navigation.list_all.each do |line|
    # Add color coding based on status
    colored_line = if line.include?('[DONE]')
                    colorize(line, :success)
                  elsif line.include?('[SKIP]')
                    colorize(line, :dim)
                  else
                    line
                  end
    lines << colored_line
  end

  lines << ""
  lines.join("\n")
end

#prompt(text) ⇒ String

Format input prompt.

Parameters:

  • text (String)

    Prompt text

Returns:

  • (String)

    Formatted prompt



332
333
334
# File 'lib/kotoshu/cli/display_formatter.rb', line 332

def prompt(text)
  colorize("#{text} ", :info)
end

#statistics(navigation) ⇒ String

Display statistics summary.

Parameters:

Returns:

  • (String)

    Formatted statistics



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/kotoshu/cli/display_formatter.rb', line 231

def statistics(navigation)
  stats = navigation.statistics

  lines = []
  lines << ""
  lines << colorize("Review Statistics", :bold)
  lines << "" * 40
  lines << "Total: #{stats[:total]}"
  lines << "Pending: #{stats[:pending]}"
  lines << colorize("Modified: #{stats[:modified]}", :success)
  lines << colorize("Skipped: #{stats[:skipped]}", :dim)
  lines << ""

  if stats[:by_type]&.any?
    lines << colorize("By Type:", :bold)
    stats[:by_type].each do |type, count|
      label = SemanticError::ERROR_TYPES[type] || type.to_s.capitalize
      lines << "  #{label}: #{count}"
    end
    lines << ""
  end

  lines.join("\n")
end

#success(message) ⇒ String

Display success message.

Parameters:

  • message (String)

    Success message

Returns:

  • (String)

    Formatted success



356
357
358
# File 'lib/kotoshu/cli/display_formatter.rb', line 356

def success(message)
  colorize(message, :success)
end

#summary_screen(document, navigation) ⇒ String

Display summary screen for document review.

Shows error breakdown by type and confidence, plus navigation hints.

Parameters:

  • document (Document)

    The document being reviewed

  • navigation (NavigationManager)

    Navigation state

Returns:

  • (String)

    Formatted summary screen



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kotoshu/cli/display_formatter.rb', line 53

def summary_screen(document, navigation)
  stats = navigation.statistics

  lines = []
  lines << ""
  lines << colorize("═══ Document Review Summary", :bold)
  lines << ""
  lines << "Document: #{document.name}"
  lines << "Format: #{Document::FORMATS[document.format]}"
  lines << "Language: #{document.language_code}"
  lines << ""
  lines << colorize("Error Summary", :bold)
  lines << "" * 40

  # Total counts
  lines << "Total errors found: #{stats[:total]}"
  lines << "  • Pending: #{stats[:pending]}"
  lines << "  • Modified: #{stats[:modified]}"
  lines << "  • Skipped: #{stats[:skipped]}"
  lines << ""

  # Breakdown by type
  if stats[:by_type]&.any?
    lines << colorize("By Type", :bold)
    stats[:by_type].each do |type, count|
      label = SemanticError::ERROR_TYPES[type] || type.to_s.capitalize
      lines << "#{label}: #{count}"
    end
    lines << ""
  end

  # Breakdown by confidence
  if stats[:by_confidence]&.any?
    lines << colorize("By Confidence", :bold)
    lines << "  • High (>0.8): #{stats[:by_confidence][:high]}"
    lines << "  • Medium (0.5-0.8): #{stats[:by_confidence][:medium]}"
    lines << "  • Low (≤0.5): #{stats[:by_confidence][:low]}"
    lines << ""
  end

  # Navigation hints
  lines << colorize("Navigation", :bold)
  lines << "  [Enter] Next error    [s] Skip    [a] Accept suggestion"
  lines << "  [b] Back             [q] Quit    [l] List all errors"
  lines << "  [j] Jump to error    [h] Help    [?] Show this summary"
  lines << ""

  lines.join("\n")
end

#warning(message) ⇒ String

Display warning message.

Parameters:

  • message (String)

    Warning message

Returns:

  • (String)

    Formatted warning



340
341
342
# File 'lib/kotoshu/cli/display_formatter.rb', line 340

def warning(message)
  colorize("Warning: #{message}", :warning)
end