Module: NA::Prompt

Defined in:
lib/na/prompt.rb

Overview

Prompt Hooks

Class Method Summary collapse

Class Method Details

.install_prompt_hook(shell) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/na/prompt.rb', line 90

def install_prompt_hook(shell)
  file = prompt_file(shell)

  File.open(File.expand_path(file), 'a') { |f| f.puts prompt_hook(shell) }
  NA.notify("#{NA.theme[:success]}Added #{NA.theme[:filename]}#{shell}{x}#{NA.theme[:success]} prompt hook to #{NA.theme[:filename]}#{file}#{NA.theme[:success]}.")
  NA.notify("#{NA.theme[:warning]}You may need to close the current terminal and open a new one to enable the script.")
end

.prompt_file(shell) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/na/prompt.rb', line 73

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
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/na/prompt.rb', line 7

def prompt_hook(shell)
  case shell
  when :zsh
    cmd = if NA.global_file
            case NA.cwd_is
            when :project
              'na next --proj $(basename "$PWD")'
            when :tag
              'na tagged $(basename "$PWD")'
            else
              NA.notify("#{NA.theme[:error]}When using a global file, a prompt hook requires `--cwd_as [tag|project]`", exit_code: 1)
            end
          else
            'na next'
          end
    <<~EOHOOK
      # zsh prompt hook for na
      chpwd() { #{cmd} }
    EOHOOK
  when :fish
    cmd = if NA.global_file
            case NA.cwd_is
            when :project
              'na next --proj (basename "$PWD")'
            when :tag
              'na tagged (basename "$PWD")'
            else
              NA.notify("#{NA.theme[:error]}When using a global file, a prompt hook requires `--cwd_as [tag|project]`", exit_code: 1)
            end
          else
            'na next'
          end
    <<~EOHOOK
      # Fish Prompt Command
      function __should_na --on-variable PWD
        test -s (basename $PWD)".#{NA.extension}" && #{cmd}
      end
    EOHOOK
  when :bash
    cmd = if NA.global_file
            case NA.cwd_is
            when :project
              'na next --proj $(basename "$PWD")'
            when :tag
              'na tagged $(basename "$PWD")'
            else
              NA.notify("#{NA.theme[:error]}When using a global file, a prompt hook requires `--cwd_as [tag|project]`", exit_code: 1)
            end
          else
            'na next'
          end

    <<~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)([ ]|$)) ]] && #{cmd}
      }
      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



83
84
85
86
87
88
# File 'lib/na/prompt.rb', line 83

def show_prompt_hook(shell)
  file = prompt_file(shell)

  NA.notify("#{NA.theme[:warning]}# Add this to #{NA.theme[:filename]}#{file}")
  puts prompt_hook(shell)
end