Skip to content

Instantly share code, notes, and snippets.

@Piqlet
Created September 20, 2022 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Piqlet/b6187a1b8ac2dcaa6d20000fc72ae94f to your computer and use it in GitHub Desktop.
Save Piqlet/b6187a1b8ac2dcaa6d20000fc72ae94f to your computer and use it in GitHub Desktop.
Command menu (bash) .
.bashrc :
# folder,export PATH : Private path where the "type -p" command will find your own files.
# source : Short .bashrc entry to make the original .bashrc more transparent. Custom settings and files in a separate directory. Easily portable, i.e. modular architecture.
folder=~/Downloads/bash && export PATH="$folder:$PATH" && source bash_settings
~/Downloads/bash/bash_settings :
(or "$folder"/bash_settings :
# Command menu with multiple tabs.
#
# fake readline (up and delete line) source :
# https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
# https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
#
# fzf insert = edit source :
# https://github.com/junegunn/fzf/wiki/examples#opening-files
#
# neccesary packages :
# coreutils : head, tail
# ncurses : tput
# List text files in the location specified in $folder. These can be moved from right to left.
files=("cmd.txt" "pacman.txt" "backup.txt" "hdd.txt" "swap.txt")
i=0
# The "while, do, continue, done" is just a replacement for goto. Instead of goto, continue jumps to the beginning.
# Source : https://stackoverflow.com/questions/9639103/is-there-a-goto-statement-in-bash
# https://www.cyberciti.biz/faq/bash-infinite-loop/
#
# Fzf --no-clear to avoid flicker.
function m {
while :
do
line=$(< $(type -p ${files[i]}) fzf --reverse --exact --no-color --no-info --expect=insert,right,left --no-clear)
# fzf escape or ctrl-c
if [ "$line" = "" ]
then
# restore screen (fzf --no-clear) and fake readline (up and delete line)
tput rmcup && tput cuu1 && tput el
return
fi
key=$(head -1 <<< "$line")
if [ "$key" = right ]
then
i=$(( i+1 ))
if [ $i -gt 4 ]
then
i=0
fi
continue
fi
if [ "$key" = left ]
then
i=$(( i-1 ))
if [ $i -lt 0 ]
then
i=4
fi
continue
fi
cmd=$(head -2 <<< "$line" | tail -1)
if [ "$key" = insert ]
then
# restore screen (fzf --no-clear) and fake readline (up and delete line)
tput rmcup && tput cuu1 && tput el
read -p "${PS1@P}" -eri "$cmd" cmd
fi
history -s "m"
history -s $cmd
# restore screen (fzf --no-clear) and fake readline (up and delete line)
tput rmcup && tput cuu1 && tput el
echo "${PS1@P}"$cmd
eval $cmd
return
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment