Provide functions to toggle commandline prefix/suffix

This introduces two functions to
- toggle a process prefix, used for adding "sudo"
- add a job suffix, used for adding "&| less"

Not sure if they are very useful; we'll see.

Closes #7905
This commit is contained in:
Johannes Altmanninger
2021-06-23 08:55:33 +02:00
parent be0b451207
commit 7c2dd694e0
7 changed files with 41 additions and 30 deletions

View File

@@ -1,16 +1,8 @@
function __fish_paginate -d "Paginate the current command using the users default pager"
set -l cmd less
if set -q PAGER
echo $PAGER | read -at cmd
end
if test -z (commandline -j | string join '')
commandline -i $history[1]
end
if commandline -j | not string match -q -r "$cmd *\$"
commandline -aj " &| $cmd"
end
fish_commandline_append " &| $cmd"
end

View File

@@ -1,19 +1,3 @@
function __fish_prepend_sudo -d "Prepend 'sudo ' to the beginning of the current commandline"
# If there is no commandline, insert the last item from history
# and *then* toggle
if not commandline | string length -q
commandline -r "$history[1]"
end
set -l cmd (commandline -po)
set -l cursor (commandline -C)
if test "$cmd[1]" != sudo
commandline -C 0
commandline -i "sudo "
commandline -C (math $cursor + 5)
else
commandline -r (string sub --start=6 (commandline -p))
commandline -C -- (math $cursor - 5)
end
function __fish_prepend_sudo -d " DEPRECATED: use fish_commandline_prepend instead. Prepend 'sudo ' to the beginning of the current commandline"
fish_commandline_prepend sudo
end

View File

@@ -98,8 +98,7 @@ function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mod
bind --preset $argv \ed 'set -l cmd (commandline); if test -z "$cmd"; echo; dirh; commandline -f repaint; else; commandline -f kill-word; end'
bind --preset $argv \cd delete-or-exit
# Prepend 'sudo ' to the current commandline
bind --preset $argv \es __fish_prepend_sudo
bind --preset $argv \es "fish_commandline_prepend sudo"
# Allow reading manpages by pressing F1 (many GUI applications) or Alt+h (like in zsh).
bind --preset $argv -k f1 __fish_man_page

View File

@@ -0,0 +1,15 @@
function fish_commandline_append --description "Append the given string to the command-line, or remove the suffix if already there"
if not commandline | string length -q
commandline -r $history[1]
end
set -l escaped (string escape --style=regex -- $argv[1])
set -l job (commandline -j | string collect)
if set job (string replace -r -- $escaped\$ "" $job)
set -l cursor (commandline -C)
commandline -rj -- $job
commandline -C $cursor
else
commandline -aj -- $argv[1]
end
end

View File

@@ -0,0 +1,16 @@
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 escaped (string escape --style=regex -- $argv[1])
set -l process (commandline -p | string collect)
if set process (string replace -r -- "^$escaped " "" $process)
commandline --replace -p -- $process
else
set -l cursor (commandline -C)
commandline -C 0 -p
commandline -i -- "$argv[1] "
commandline -C (math $cursor + (string length -- "$argv[1] "))
end
end