File: fish_commandline_prepend.fish

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (26 lines) | stat: -rw-r--r-- 867 bytes parent folder | download | duplicates (4)
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
function fish_commandline_prepend --description "Prepend the given string to the command-line, or remove the prefix if already there"
    if not commandline | string length -q
        commandline -r $history[1]
    end

    set -l process (commandline -p | string collect)

    set -l to_prepend "$argv[1] "
    if string match -qr '^ ' "$process"
        set to_prepend " $argv[1]"
    end

    set -l length_diff (string length -- "$to_prepend")
    set -l cursor_location (commandline -pC)

    set -l escaped (string escape --style=regex -- $to_prepend)
    if set process (string replace -r -- "^$escaped" "" $process)
        commandline --replace -p -- $process
        set length_diff "-$length_diff"
    else
        commandline -pC 0
        commandline -pi -- "$to_prepend"
    end

    commandline -pC (math "max 0,($cursor_location + $length_diff)")
end