Module: AiCli::Helpers::ShellHistory
- Defined in:
- lib/aicli/helpers/shell_history.rb
Constant Summary collapse
- SOURCE_MARKER =
'# aicli shell history integration'
Class Method Summary collapse
- .activate_shell_integration! ⇒ Object
- .append_source_to_rc!(rc_path, source_path) ⇒ Object
- .bash_wrapper ⇒ Object
- .current_shell ⇒ Object
- .install_shell_integration! ⇒ Object
- .pending_path ⇒ Object
-
.record_executed(command) ⇒ Object
Queue the executed command for the shell wrapper to store in history instead of the original
ai ...invocation. - .shell_integration_hint(activated: false, rc_path: nil, source_path: nil) ⇒ Object
- .shell_rc_path ⇒ Object
- .wrapper_source_path ⇒ Object
- .zsh_wrapper ⇒ Object
Class Method Details
.activate_shell_integration! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/aicli/helpers/shell_history.rb', line 34 def activate_shell_integration! install_shell_integration! rc_path = shell_rc_path source_path = wrapper_source_path if rc_path && File.exist?(rc_path) append_source_to_rc!(rc_path, source_path) { rc_path: rc_path, source_path: source_path, activated: true } else { rc_path: nil, source_path: source_path, activated: false } end end |
.append_source_to_rc!(rc_path, source_path) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/aicli/helpers/shell_history.rb', line 47 def append_source_to_rc!(rc_path, source_path) content = File.read(rc_path) return if content.include?(SOURCE_MARKER) || content.include?(source_path) block = <<~RC #{SOURCE_MARKER} [[ -f #{source_path} ]] && source #{source_path} RC File.write(rc_path, content.rstrip + block) end |
.bash_wrapper ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/aicli/helpers/shell_history.rb', line 154 def bash_wrapper <<~'BASH' # aicli shell history integration for bash if [[ -z "$AICLI_BASH_INTEGRATION" ]]; then AICLI_BASH_INTEGRATION=1 _aicli_bash_history() { if [[ -n "$AICLI_PENDING_HISTORY" ]]; then local cmd="$AICLI_PENDING_HISTORY" unset AICLI_PENDING_HISTORY history -d "$HISTCMD" 2>/dev/null history -s "$cmd" fi } ai() { AICLI_SHELL_WRAPPER=1 command ai "$@" local ret=$? if [[ -f "$HOME/.aicli/history.pending" ]]; then AICLI_PENDING_HISTORY=$(<"$HOME/.aicli/history.pending") rm -f "$HOME/.aicli/history.pending" fi return $ret } PROMPT_COMMAND="_aicli_bash_history${PROMPT_COMMAND:+; $PROMPT_COMMAND}" fi BASH end |
.current_shell ⇒ Object
108 109 110 |
# File 'lib/aicli/helpers/shell_history.rb', line 108 def current_shell File.basename(ENV['SHELL'] || 'bash') end |
.install_shell_integration! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aicli/helpers/shell_history.rb', line 22 def install_shell_integration! Config.ensure_home! target_dir = Config.home_dir { 'ai.zsh' => zsh_wrapper, 'ai.bash' => bash_wrapper }.each do |name, contents| File.write(File.join(target_dir, name), contents) end end |
.pending_path ⇒ Object
104 105 106 |
# File 'lib/aicli/helpers/shell_history.rb', line 104 def pending_path File.join(Config.home_dir, 'history.pending') end |
.record_executed(command) ⇒ Object
Queue the executed command for the shell wrapper to store in history
instead of the original ai ... invocation.
12 13 14 15 16 17 18 19 20 |
# File 'lib/aicli/helpers/shell_history.rb', line 12 def record_executed(command) text = command.to_s.strip return if text.empty? Config.ensure_home! File.write(pending_path, text) rescue StandardError nil end |
.shell_integration_hint(activated: false, rc_path: nil, source_path: nil) ⇒ Object
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 102 |
# File 'lib/aicli/helpers/shell_history.rb', line 74 def shell_integration_hint(activated: false, rc_path: nil, source_path: nil) source_path ||= wrapper_source_path if activated && rc_path <<~HINT.strip Added to #{rc_path}. Reload your shell: source #{rc_path} Or open a new terminal tab. HINT else case current_shell when 'zsh' <<~HINT.strip Add to ~/.zshrc for shell history integration: source #{source_path} HINT when 'bash' <<~HINT.strip Add to ~/.bashrc for shell history integration: source #{source_path} HINT else '' end end end |
.shell_rc_path ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/aicli/helpers/shell_history.rb', line 59 def shell_rc_path home = Dir.home case current_shell when 'zsh' File.join(home, '.zshrc') when 'bash' File.join(home, '.bashrc') end end |
.wrapper_source_path ⇒ Object
69 70 71 72 |
# File 'lib/aicli/helpers/shell_history.rb', line 69 def wrapper_source_path name = current_shell == 'bash' ? 'ai.bash' : 'ai.zsh' File.join(Config.home_dir, name) end |
.zsh_wrapper ⇒ Object
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/aicli/helpers/shell_history.rb', line 112 def zsh_wrapper <<~'ZSH' # aicli shell history integration for zsh if [[ -z "$AICLI_ZSH_INTEGRATION" ]]; then AICLI_ZSH_INTEGRATION=1 _aicli_zshaddhistory() { if [[ -n "$AICLI_PENDING_HISTORY" ]]; then print -s -- "$AICLI_PENDING_HISTORY" unset AICLI_PENDING_HISTORY return 1 fi } _aicli_precmd() { if [[ -n "$AICLI_PENDING_HISTORY" ]]; then local cmd="$AICLI_PENDING_HISTORY" unset AICLI_PENDING_HISTORY local last="${${(z)${(f)"$(fc -l -1 2>/dev/null)"}}[2]}" if [[ "$last" == ai\ * || "$last" == aicli\ * ]]; then fc -d -1 -I 2>/dev/null fi print -s -- "$cmd" fi } ai() { AICLI_SHELL_WRAPPER=1 command ai "$@" local ret=$? if [[ -f "$HOME/.aicli/history.pending" ]]; then typeset -g AICLI_PENDING_HISTORY="${$(<"$HOME/.aicli/history.pending")}" rm -f "$HOME/.aicli/history.pending" fi return $ret } zshaddhistory_functions+=(_aicli_zshaddhistory) precmd_functions+=(_aicli_precmd) fi ZSH end |