Module: NA::Prompt

Defined in:
lib/na/prompt.rb

Overview

Prompt Hooks

Class Method Summary collapse

Class Method Details

.install_prompt_hook(shell) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/na/prompt.rb', line 53

def install_prompt_hook(shell)
  file = prompt_file(shell)

  File.open(File.expand_path(file), 'a') { |f| f.puts prompt_hook(shell) }
  $stderr.puts NA::Color.template("{y}Added {bw}#{shell}{xy} prompt hook to {bw}#{file}{xy}.{x}")
  $stderr.puts NA::Color.template("{y}You may need to close the current terminal and open a new one to enable the script.{x}")
end

.prompt_file(shell) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/na/prompt.rb', line 36

def prompt_file(shell)
  files = {
    zsh: '~/.zshrc',
    fish: '~/.config/fish/conf.d/na.fish',
    bash: '~/.bash_profile'
  }

  files[shell]
end

.prompt_hook(shell) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/na/prompt.rb', line 7

def prompt_hook(shell)
  case shell
  when :zsh
    <<~EOHOOK
      # zsh prompt hook for na
      chpwd() { na next }
    EOHOOK
  when :fish
    <<~EOHOOK
      # Fish Prompt Command
      function __should_na --on-variable PWD
        test -s (basename $PWD)".#{NA.extension}" && na next
      end
    EOHOOK
  when :bash
    <<~EOHOOK
      # Bash PROMPT_COMMAND for na
      last_command_was_cd() {
        [[ $(history 1|sed -e "s/^[ ]*[0-9]*[ ]*//") =~ ^((cd|z|j|jump|g|f|pushd|popd|exit)([ ]|$)) ]] && na next
      }
      if [[ -z "$PROMPT_COMMAND" ]]; then
        PROMPT_COMMAND="eval 'last_command_was_cd'"
      else
        echo $PROMPT_COMMAND | grep -v -q "last_command_was_cd" && PROMPT_COMMAND="$PROMPT_COMMAND;"'eval "last_command_was_cd"'
      fi
    EOHOOK
  end
end

.show_prompt_hook(shell) ⇒ Object



46
47
48
49
50
51
# File 'lib/na/prompt.rb', line 46

def show_prompt_hook(shell)
  file = prompt_file(shell)

  $stderr.puts NA::Color.template("{bw}# Add this to {y}#{file}{x}")
  puts prompt_hook(shell)
end