Class: Consolle::History

Inherits:
Object
  • Object
show all
Defined in:
lib/consolle/history.rb

Overview

Manages command history for sessions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_path = Dir.pwd) ⇒ History

Returns a new instance of History.



11
12
13
# File 'lib/consolle/history.rb', line 11

def initialize(project_path = Dir.pwd)
  @registry = SessionRegistry.new(project_path)
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



9
10
11
# File 'lib/consolle/history.rb', line 9

def registry
  @registry
end

Instance Method Details

#command_count(session_id) ⇒ Object

Get total command count for a session



148
149
150
# File 'lib/consolle/history.rb', line 148

def command_count(session_id)
  load_history(session_id).size
end

#format_entry(entry, show_session: true) ⇒ Object

Format history entry for display (compact)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/consolle/history.rb', line 91

def format_entry(entry, show_session: true)
  timestamp = Time.parse(entry['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
  session_prefix = show_session ? "[#{entry['_session']&.dig('short_id') || entry['session_id']&.[](0, 4)}] " : ''
  code_preview = entry['code'].to_s.gsub("\n", ' ').strip
  code_preview = "#{code_preview[0, 60]}..." if code_preview.length > 63

  lines = []
  lines << "#{session_prefix}#{timestamp} | #{code_preview}"

  if entry['success']
    result_preview = entry['result'].to_s.gsub("\n", ' ').strip
    result_preview = "#{result_preview[0, 70]}..." if result_preview.length > 73
    exec_time = entry['execution_time'] ? " (#{entry['execution_time'].round(3)}s)" : ''
    lines << "#{result_preview}#{exec_time}"
  else
    error_msg = entry['error'] || entry['message'] || 'Error'
    lines << "ERROR: #{error_msg}"
  end

  lines.join("\n")
end

#format_entry_verbose(entry) ⇒ Object

Format history entry for verbose display



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
# File 'lib/consolle/history.rb', line 114

def format_entry_verbose(entry)
  lines = []
  lines << '' * 60
  timestamp = Time.parse(entry['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
  session_info = entry['_session']
  session_str = session_info ? "#{session_info['short_id']} (#{session_info['target']})" : entry['session_id']

  lines << "[#{timestamp}] Session: #{session_str}"
  lines << '' * 60
  lines << 'Code:'
  entry['code'].to_s.lines.each { |line| lines << "  #{line.chomp}" }
  lines << ''

  if entry['success']
    lines << 'Result:'
    entry['result'].to_s.lines.each { |line| lines << "  #{line.chomp}" }
  else
    lines << "Error: #{entry['error']}"
    lines << entry['message'] if entry['message']
  end

  lines << ''
  lines << "Execution time: #{entry['execution_time']&.round(3)}s" if entry['execution_time']
  lines << '' * 60

  lines.join("\n")
end

#format_json(entries) ⇒ Object

Format history as JSON



143
144
145
# File 'lib/consolle/history.rb', line 143

def format_json(entries)
  JSON.pretty_generate(entries.map { |e| e.except('_session') })
end

#log_command(session_id:, target:, code:, result:) ⇒ Object

Log a command execution to session history



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/consolle/history.rb', line 16

def log_command(session_id:, target:, code:, result:)
  log_path = registry.history_log_path(session_id)

  entry = {
    'timestamp' => Time.now.iso8601,
    'session_id' => session_id,
    'target' => target,
    'request_id' => result['request_id'],
    'code' => code,
    'success' => result['success'],
    'result' => result['result'],
    'error' => result['error'],
    'message' => result['message'],
    'execution_time' => result['execution_time']
  }

  FileUtils.mkdir_p(File.dirname(log_path))
  File.open(log_path, 'a') do |f|
    f.puts JSON.generate(entry)
  end

  # Update command count in registry
  registry.record_command(session_id)

  entry
rescue StandardError
  nil
end

#query(session_id: nil, target: nil, limit: nil, today: false, date: nil, success_only: false, failed_only: false, grep: nil, all_sessions: false) ⇒ Object

Query history for a session



46
47
48
49
50
51
52
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
# File 'lib/consolle/history.rb', line 46

def query(session_id: nil, target: nil, limit: nil, today: false, date: nil,
          success_only: false, failed_only: false, grep: nil, all_sessions: false)
  entries = []

  # Get sessions to query
  sessions = if session_id
               [registry.find_session(session_id: session_id)]
             elsif target
               if all_sessions
                 # All sessions with this target (including stopped)
                 registry.list_sessions(include_stopped: true).select { |s| s['target'] == target }
               else
                 # Most recent session with this target
                 [registry.find_session(target: target)]
               end
             else
               # Current project sessions
               if all_sessions
                 registry.list_sessions(include_stopped: true)
               else
                 registry.list_sessions(include_stopped: false)
               end
             end

  sessions.compact.each do |session|
    session_entries = load_history(session['id'])
    session_entries.each { |e| e['_session'] = session }
    entries.concat(session_entries)
  end

  # Apply filters
  entries = filter_by_date(entries, today: today, date: date)
  entries = filter_by_status(entries, success_only: success_only, failed_only: failed_only)
  entries = filter_by_grep(entries, grep) if grep

  # Sort by timestamp descending
  entries = entries.sort_by { |e| e['timestamp'] }.reverse

  # Apply limit
  entries = entries.first(limit) if limit

  entries
end