Class: Appydave::Tools::ZshHistory::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/zsh_history/formatter.rb

Overview

Handles output formatting and file writing

Constant Summary collapse

DEFAULT_DATETIME_FORMAT =
'%Y-%m-%d %H:%M:%S'
MAX_COMMAND_LENGTH =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datetime_format: nil, max_length: nil) ⇒ Formatter

Returns a new instance of Formatter.



13
14
15
16
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 13

def initialize(datetime_format: nil, max_length: nil)
  @datetime_format = datetime_format || DEFAULT_DATETIME_FORMAT
  @max_length = max_length || MAX_COMMAND_LENGTH
end

Instance Attribute Details

#datetime_formatObject (readonly)

Returns the value of attribute datetime_format.



11
12
13
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 11

def datetime_format
  @datetime_format
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



11
12
13
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 11

def max_length
  @max_length
end

Instance Method Details

#format_command(cmd, verbose: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 22

def format_command(cmd, verbose: false)
  datetime_str = cmd.formatted_datetime(datetime_format)
  text = truncate(cmd.text)

  if verbose && cmd.matched_pattern
    category = cmd.category.to_s.upcase
    "#{datetime_str}  [#{category}: #{cmd.matched_pattern}]  #{text}"
  else
    "#{datetime_str}  #{text}"
  end
end

#format_commands(commands, verbose: false) ⇒ Object



18
19
20
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 18

def format_commands(commands, verbose: false)
  commands.map { |cmd| format_command(cmd, verbose: verbose) }.join("\n")
end

#format_stats(stats, date_range: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 34

def format_stats(stats, date_range: nil)
  lines = []
  lines << 'ZSH History Statistics'
  lines << ('=' * 50)

  lines << format('Total commands:    %<total>d', stats)
  lines << format('Wanted:            %<wanted>d  (%<wanted_pct>.1f%%)', stats)
  lines << format('Unwanted:          %<unwanted>d  (%<unwanted_pct>.1f%%)', stats)
  lines << format('Unsure:            %<unsure>d  (%<unsure_pct>.1f%%)', stats)

  if date_range
    lines << ''
    lines << "Date range: #{date_range[:from]} to #{date_range[:to]} (#{date_range[:days]} days)"
  end

  lines.join("\n")
end

#write_history(commands, output_path, backup: true) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/appydave/tools/zsh_history/formatter.rb', line 52

def write_history(commands, output_path, backup: true)
  if backup && File.exist?(output_path)
    backup_path = "#{output_path}.backup.#{Time.now.strftime('%Y%m%d-%H%M%S')}"
    FileUtils.cp(output_path, backup_path)
    puts "Backup created: #{backup_path}"
  end

  content = commands.map(&:to_history_format).join("\n")
  File.write(output_path, "#{content}\n")

  puts "Wrote #{commands.size} commands to #{output_path}"
end