Module: NA::Prompt

Defined in:
lib/na/prompt.rb

Overview

Prompt Hooks

Class Method Summary collapse

Class Method Details

.install_prompt_hook(shell) ⇒ void

This method returns an undefined value.

Install the prompt hook script into the shell config file

Parameters:

  • shell (Symbol)

    Shell type



112
113
114
115
116
117
118
# File 'lib/na/prompt.rb', line 112

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) ⇒ String

Get the configuration file path for the given shell

Parameters:

  • shell (Symbol)

    Shell type

Returns:

  • (String)

    Path to shell config file



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

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

  files[shell]
end

.prompt_hook(shell) ⇒ String

Generate the shell prompt hook script for na

Parameters:

  • shell (Symbol)

    Shell type (:zsh, :fish, :bash)

Returns:

  • (String)

    Shell script for prompt hook



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
72
73
74
75
76
77
78
79
80
81
# File 'lib/na/prompt.rb', line 11

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) ⇒ void

This method returns an undefined value.

Display the prompt hook script and notify user of config file

Parameters:

  • shell (Symbol)

    Shell type



101
102
103
104
105
106
# File 'lib/na/prompt.rb', line 101

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