summaryrefslogtreecommitdiff
path: root/hm/env/otter.nix
blob: b7324e09a26fa2b309e9c511f4479907b3506199 (plain)
1
2
3
4
5
6
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
72
73
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
103
104
105
106
107
108
109
110
111
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
153
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
183
184
185
186
187
188
189
190
191
{
  inputs,
  config,
  lib,
  ...
}:
let
  cfg = config.hm.env.otter;
in
{
  imports = [ inputs.otter-launcher.homeModules.otter-launcher ];

  options.hm.env.otter.enable = lib.mkEnableOption "enables otter launcher";
  config = lib.mkIf cfg.enable {
    programs.otter-launcher = {
      enable = true;
      # settings = {
      #   interface = {
      #     suggestion_mode = "list";
      #   };
      # };
      settingsExtra = ''
        [general]
        default_module = "gg" # module to run when no prefix is matched
        empty_module = "app" # run with an empty prompt
        exec_cmd = "sh -c" # exec command of your shell
        # for example: "bach -c" for bash; "zsh -c" for zsh; also accept wm commands like "hyprctl dispatch exec"
        vi_mode = false # set true to use vi keybinds, false emacs keybinds
        esc_to_abort = true # useful for vi users
        cheatsheet_entry = "?" # when prompted, will show a list of configured modules
        cheatsheet_viewer = "less -R; clear" # command to show cheatsheet; through piping stdout
        clear_screen_after_execution = false
        loop_mode = false # don't quit after executing a module, useful with scratchpads; stderr is hidden in loop mode
        external_editor = "vi" # if set, press ctrl+x ctrl+ee (or v in vi normal mode) to edit prompt in the specified program
        delay_startup = 0 # sometimes the otter runs too fast even before the terminal window is ready; this slows it down by milliseconds; useful when chafa image is skewed
        #callback = "" # if set, will run after module execution; for example, calling swaymsg to adjust window size


        # ANSI color codes are allowed. However, \x1b should be replaced with \u001B, because the rust toml crate cannot read \x as an escaped character
        [interface]
        # use three quotes to write longer codes
        header = """
          \u001B[34;1m$USER@$(printf $HOSTNAME)\u001B[0m     \u001B[31m\u001B[0m $(mpstat | awk 'FNR ==4 {print $4}')
          """
        header_cmd = "" # run a command and print stdout above the header
        header_cmd_trimmed_lines = 0 # remove trailing lines from header_cmd output, in case of some programs appending excessive empty lines
        place_holder = "otterly awesome" # at the input field
        suggestion_mode = "list" # available options: list, hint
        separator = "                  \u001B[90mmodules ────────────────" # add a line between intput field and suggestion list; only effective in list mode
        footer = "" # add a line after suggestion list
        suggestion_lines = 3 # 0 to disable suggestions and tab completion
        list_prefix = "  "
        selection_prefix = "\u001B[31;1m▌ "
        prefix_padding = 3 # format prefixes to have a uniformed width
        default_module_message = "  \u001B[33msearch\u001B[0m the internet" # shown when the default module is in use
        empty_module_message = "" # shown when the empty module is in use
        customized_list_order = false # false to list modules alphabetically; true to list as per the configured order in the below [[modules]] section
        indicator_with_arg_module = "" # the sign showing whether a module should run with an argument
        indicator_no_arg_module = ""
        # below color options affect all modules; per-module coloring can be configured using ansi codes individually
        prefix_color = "\u001B[33m"
        description_color = "\u001B[39m"
        place_holder_color = "\u001B[30m"
        hint_color = "\u001B[30m" # suggestion color in hint mode
        # move the interface rightward or downward
        move_interface_right = 16
        move_interface_down = 1


        # overlay is a floating layer that can be printed with stdout and moved around; useful for integrating chafa images
        [overlay]
        overlay_cmd = """
        """
        overlay_height = 0


        # modules are defined as followed
        [[modules]]
        description = "google search"
        prefix = "gg"
        cmd = "xdg-open https://www.google.com/search?q='{}'"
        with_argument = true # if true, {} in cmd will be replaced with user input. if not explicitly set, taken as false.
        url_encode = true # should be true when calling webpages; this ensures special characters in url being readable to browsers; taken as false if not explicitly set
        unbind_proc = true # run cmd in a forked shell as opposed to as a child process (simply by prepending setsid -f to the configured cmd); useful for launching gui programs; taken as false if not explicitly set; try wm's exec command for unbinding if this option goes wrong (like swaymsg exec)

        # fzf is needed to run below functions
        [[modules]]
        description = "desktop programs"
        prefix = "app"
        cmd = """
        desktop_file() {
        find /usr/share/applications -name "*.desktop" 2>/dev/null
        find /usr/local/share/applications -name "*.desktop" 2>/dev/null
        find "$HOME/.local/share/applications" -name "*.desktop" 2>/dev/null
        find /var/lib/flatpak/exports/share/applications -name "*.desktop" 2>/dev/null
        find "$HOME/.local/share/flatpak/exports/share/applications" -name "*.desktop" 2>/dev/null
        }
        selected="$(desktop_file | awk -F/ '{name=$NF; sub(/\\.desktop$/, "", name); print name}' | sort -k1,1 | cut -f2- | fzf --reverse --padding 1,3 --info-command 'echo -e " desktop apps ($FZF_POS/$FZF_TOTAL_COUNT)"' --cycle --gutter " " --pointer " ▌" --color "bg+:-1,pointer:1,info:8,separator:8,scrollbar:0" --prompt '  ' -m -d / --with-nth -1 )"
        [ -z "$selected" ] && exit
        echo "$selected" | while read -r line ; do setsid -f gtk-launch "$(basename $line)"; done
        """

        [[modules]]
        description = "power menu (fzf)"
        prefix = "po"
        cmd = """
        function power {
        if [[ -n $1 ]]; then
        case $1 in
        "logout") session=`loginctl session-status | head -n 1 | awk '{print $1}'`; loginctl terminate-session $session ;;
        "suspend") systemctl suspend ;;
        "hibernate") systemctl hibernate ;;
        "reboot") systemctl reboot ;;
        "shutdown") systemctl poweroff ;;
        esac fi }
        power $(echo -e 'reboot\nshutdown\nlogout\nsuspend\nhibernate' | fzf --reverse --padding 1,2 --info-command 'printf " power menu ($FZF_POS/$FZF_TOTAL_COUNT)"' --cycle --gutter " " --pointer " ▌" --color "bg+:-1,pointer:1,info:8,separator:8,scrollbar:0" --prompt '  ' | tail -1)
        """

        [[modules]]
        description = "run commands"
        prefix = "sh"
        cmd = """
        $(printf $TERM | sed 's/xterm-//g') -e sh -c "{}"
        """
        with_argument = true
        unbind_proc = true

        [[modules]]
        description = "search archwiki"
        prefix = "aw"
        cmd = "xdg-open https://wiki.archlinux.org/index.php?search='{}'"
        with_argument = true
        url_encode = true
        unbind_proc = true

        [[modules]]
        description = "search packages"
        prefix = "pac"
        cmd = "xdg-open https://archlinux.org/packages/?q='{}'"
        with_argument = true
        url_encode = true
        unbind_proc = true

        [[modules]]
        description = "search the AUR"
        prefix = "aur"
        cmd = "xdg-open https://aur.archlinux.org/packages?K='{}'"
        with_argument = true
        url_encode = true
        unbind_proc = true

        [[modules]]
        description = "cambridge dict"
        prefix = "dc"
        cmd = "xdg-open 'https://dictionary.cambridge.org/dictionary/english/{}'"
        with_argument = true
        url_encode = true
        unbind_proc = true

        [[modules]]
        description = "open files (fzf)"
        prefix = "fo"
        cmd = """
        find $HOME -type f -not -path '*/.cache/*' 2>/dev/null | fzf --reverse --padding 1,3 --info-command 'printf " files ($FZF_POS/$FZF_TOTAL_COUNT)"' --cycle --gutter ' ' --pointer ' ▌' --color 'bg+:-1,pointer:1,info:8,separator:8,scrollbar:0' --prompt '  ' | setsid -f xargs -r -I [] xdg-open '[]'
        """

        [[modules]]
        description = "open dirs (yazi)"
        prefix = "yz"
        cmd = """
        find $HOME -type d -not -path '*/.cache/*' 2>/dev/null | fzf --reverse --padding 1,3 --info-command 'printf " directories ($FZF_POS/$FZF_TOTAL_COUNT)"' --cycle --gutter ' ' --pointer ' ▌' --color 'bg+:-1,pointer:1,info:8,separator:8,scrollbar:0' --prompt '  ' | xargs -r -I [] setsid -f "$(echo $TERM | sed 's/xterm-//g')" -e yazi '[]'
        """

        [[modules]]
        description = "ChatGPT prompt"
        prefix = "gpt"
        with_argument = true
        unbind_proc = true
        cmd = """
        function chat {
          encoded="$(jq -sRr @uri)"
          exec xdg-open "https://chat.openai.com/?prompt=$encoded"
        }
        chat <<'EOF'
        {}
        EOF
        """
      '';
    };
  };
}