Module: AiCli::Helpers::ShellHistory

Defined in:
lib/aicli/helpers/shell_history.rb

Class Method Summary collapse

Class Method Details

.append(command) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/aicli/helpers/shell_history.rb', line 8

def append(command)
  history_file = history_file_path
  return unless history_file

  last = last_command(history_file)
  return if last == command

  File.open(history_file, 'a') { |f| f.puts(command) }
rescue StandardError
  # Ignore history write errors
end

.history_file_pathObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aicli/helpers/shell_history.rb', line 20

def history_file_path
  shell = File.basename(ENV['SHELL'] || '')
  home = Dir.home

  case shell
  when 'bash', 'sh'
    File.join(home, '.bash_history')
  when 'zsh'
    File.join(home, '.zsh_history')
  when 'fish'
    File.join(home, '.local', 'share', 'fish', 'fish_history')
  when 'ksh'
    File.join(home, '.ksh_history')
  when 'tcsh'
    File.join(home, '.history')
  end
end

.last_command(history_file) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/aicli/helpers/shell_history.rb', line 38

def last_command(history_file)
  return nil unless File.exist?(history_file)

  lines = File.read(history_file).strip.split("\n")
  lines.last
rescue StandardError
  nil
end