Class: Kotoshu::Cli::NavigationManager

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

Overview

Manages navigation through errors in interactive review mode.

Tracks current position, user decisions, and provides filtering for efficient error review.

Examples:

Creating navigation for errors

nav = NavigationManager.new(errors)
nav.forward  # Move to next error
nav.accept_suggestion(error.suggestions.first)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors) ⇒ NavigationManager

Create a new navigation manager.

Parameters:

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kotoshu/cli/navigation_manager.rb', line 20

def initialize(errors)
  raise ArgumentError, "Errors cannot be nil" if errors.nil?
  raise ArgumentError, "Errors must be an Array" unless errors.is_a?(Array)

  @errors = errors.sort # Errors are Comparable
  @current_index = 0
  @history = []
  @skipped = Set.new
  @modified = Set.new
  @filters = {}
end

Instance Attribute Details

#current_indexObject (readonly)

Returns the value of attribute current_index.



15
16
17
# File 'lib/kotoshu/cli/navigation_manager.rb', line 15

def current_index
  @current_index
end

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'lib/kotoshu/cli/navigation_manager.rb', line 15

def errors
  @errors
end

#historyObject (readonly)

Returns the value of attribute history.



15
16
17
# File 'lib/kotoshu/cli/navigation_manager.rb', line 15

def history
  @history
end

#modifiedObject (readonly)

Returns the value of attribute modified.



15
16
17
# File 'lib/kotoshu/cli/navigation_manager.rb', line 15

def modified
  @modified
end

#skippedObject (readonly)

Returns the value of attribute skipped.



15
16
17
# File 'lib/kotoshu/cli/navigation_manager.rb', line 15

def skipped
  @skipped
end

Instance Method Details

#accept_suggestion(suggestion) ⇒ Models::SemanticError?

Accept a suggestion for the current error.

Records the decision and marks the error as modified.

Parameters:

Returns:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kotoshu/cli/navigation_manager.rb', line 116

def accept_suggestion(suggestion)
  error = current
  return nil unless error

  record_decision(
    error_id: error.id,
    action: :accept,
    original: error.original,
    replacement: suggestion.word,
    confidence: error.confidence
  )

  @modified.add(@current_index)
  forward
end

#backwardModels::SemanticError?

Move to previous error.

Returns:



70
71
72
73
74
75
# File 'lib/kotoshu/cli/navigation_manager.rb', line 70

def backward
  return nil unless previous?

  @current_index -= 1
  current
end

#by_statusArray<Models::SemanticError>

Get all errors sorted by status (pending first).

Returns:



188
189
190
191
192
193
194
# File 'lib/kotoshu/cli/navigation_manager.rb', line 188

def by_status
  pending + @errors.each_with_index.select { |_, idx|
    @modified.include?(idx)
  }.map(&:last).reverse + @errors.each_with_index.select { |_, idx|
    @skipped.include?(idx)
  }.map(&:last).reverse
end

#currentModels::SemanticError?

Get the current error.

Returns:



35
36
37
38
39
# File 'lib/kotoshu/cli/navigation_manager.rb', line 35

def current
  return nil if @current_index >= @errors.size

  @errors[@current_index]
end

#export_correctionsArray<Hash>

Export corrections as a list of changes.

Returns:

  • (Array<Hash>)

    List of correction changes



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kotoshu/cli/navigation_manager.rb', line 209

def export_corrections
  @modified.to_a.sort.map do |idx|
    error = @errors[idx]
    suggestion = error.recommended_suggestion

    {
      line: error.location.line,
      original: error.original,
      replacement: suggestion.word,
      error_type: error.error_type,
      confidence: error.confidence
    }
  end
end

#filter_by_confidence(min_confidence: 0.0) ⇒ Array<Models::SemanticError>

Filter errors by minimum confidence.

Parameters:

  • min_confidence (Float) (defaults to: 0.0)

    Minimum confidence threshold (0.0 to 1.0)

Returns:



146
147
148
# File 'lib/kotoshu/cli/navigation_manager.rb', line 146

def filter_by_confidence(min_confidence: 0.0)
  @errors.select { |e| e.confidence >= min_confidence }
end

#filter_by_type(*types) ⇒ Array<Models::SemanticError>

Filter errors by type(s).

Parameters:

  • types (Array<Symbol>)

    Error types to include

Returns:



154
155
156
# File 'lib/kotoshu/cli/navigation_manager.rb', line 154

def filter_by_type(*types)
  @errors.select { |e| types.include?(e.error_type) }
end

#firstModels::SemanticError?

Jump to first error.

Returns:



91
92
93
# File 'lib/kotoshu/cli/navigation_manager.rb', line 91

def first
  jump_to(0)
end

#forwardModels::SemanticError?

Move to next error.

Returns:



60
61
62
63
64
65
# File 'lib/kotoshu/cli/navigation_manager.rb', line 60

def forward
  return nil unless next?

  @current_index += 1
  current
end

#history_summaryArray<Hash>

Get user decision history.

Returns:

  • (Array<Hash>)

    List of decisions made



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/kotoshu/cli/navigation_manager.rb', line 227

def history_summary
  @history.map.with_index.map do |decision, idx|
    {
      id: idx + 1,
      error_id: decision[:error_id],
      action: decision[:action],
      original: decision[:original],
      replacement: decision[:replacement],
      confidence: decision[:confidence],
      timestamp: decision[:timestamp]
    }
  end
end

#jump_to(index) ⇒ Models::SemanticError?

Jump to specific error by index.

Parameters:

  • index (Integer)

    Error index (0-based)

Returns:



81
82
83
84
85
86
# File 'lib/kotoshu/cli/navigation_manager.rb', line 81

def jump_to(index)
  return nil if index < 0 || index >= @errors.size

  @current_index = index
  current
end

#lastModels::SemanticError?

Jump to last error.

Returns:



98
99
100
# File 'lib/kotoshu/cli/navigation_manager.rb', line 98

def last
  jump_to(@errors.size - 1)
end

#list_allArray<String>

List all errors with their status.

Returns:

  • (Array<String>)

    Formatted error list



135
136
137
138
139
140
# File 'lib/kotoshu/cli/navigation_manager.rb', line 135

def list_all
  @errors.each_with_index.map do |error, idx|
    status = status_for(idx)
    "#{idx + 1}. #{status} #{error.abbreviated}"
  end
end

#next?Boolean

Check if there’s a next error.

Returns:

  • (Boolean)

    True if there’s a next error



44
45
46
# File 'lib/kotoshu/cli/navigation_manager.rb', line 44

def next?
  @current_index < @errors.size - 1
end

#pendingArray<Models::SemanticError>

Get only pending errors (not skipped or modified).

Returns:



161
162
163
164
165
# File 'lib/kotoshu/cli/navigation_manager.rb', line 161

def pending
  @errors.each_with_index.reject do |_, idx|
    @skipped.include?(idx) || @modified.include?(idx)
  end.map(&:last)
end

#previous?Boolean

Check if there’s a previous error.

Returns:

  • (Boolean)

    True if there’s a previous error



51
52
53
# File 'lib/kotoshu/cli/navigation_manager.rb', line 51

def previous?
  @current_index > 0
end

#resetvoid

This method returns an undefined value.

Reset navigation state (clear all decisions).



199
200
201
202
203
204
# File 'lib/kotoshu/cli/navigation_manager.rb', line 199

def reset
  @current_index = 0
  @history.clear
  @skipped.clear
  @modified.clear
end

#skip_currentModels::SemanticError?

Skip the current error.

Returns:



105
106
107
108
# File 'lib/kotoshu/cli/navigation_manager.rb', line 105

def skip_current
  @skipped.add(@current_index)
  forward
end

#statisticsHash

Get statistics about the errors.

Returns:

  • (Hash)

    Statistics hash



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kotoshu/cli/navigation_manager.rb', line 170

def statistics
  {
    total: @errors.size,
    skipped: @skipped.size,
    modified: @modified.size,
    pending: pending.size,
    by_type: @errors.group_by(&:error_type).transform_values(&:size),
    by_confidence: {
      high: @errors.count(&:high_confidence?),
      medium: @errors.count { |e| e.confidence > 0.5 && e.confidence <= 0.8 },
      low: @errors.count { |e| e.confidence <= 0.5 }
    }
  }
end