Skip to content

Instantly share code, notes, and snippets.

@QiangF
Last active October 4, 2016 03:36
Show Gist options
  • Save QiangF/565102ba3b6123942b9bf6b897c05f87 to your computer and use it in GitHub Desktop.
Save QiangF/565102ba3b6123942b9bf6b897c05f87 to your computer and use it in GitHub Desktop.
# all variables start with a double underscore to avoid collisions with cheat variables
# (which are placed into the current environment as local variables)
function cheat {
local IFS;
local __description __line __command;
IFS=' ';
if [[ -f "$HOME/.cheats/$*" ]]; then
__run_cheat "$HOME/.cheats/$*";
history -s cheat $*;
else
__list_match "$*";
fi
}
function sucheat {
local IFS;
local __description __line __command;
IFS=' ';
if [[ -f "$HOME/.cheats/$*" ]]; then
__su_run_cheat "$HOME/.cheats/$*";
history -s sucheat $*;
else
__list_match "$*";
fi
}
function __run_cheat {
__read_command "$*";
eval "$__command";
}
function __su_run_cheat {
__read_command "$*";
eval "sudo $__command";
}
function __read_command {
local __newline __lines __input __newstr __tmp;
exec 3< "$*";
__newline=$'\n';
# description
while IFS= read <&3 -r __line && [ "$__line" != '####' ]; do
echo "$__line";
__description="${__description} ${__newline} ${__line}";
done
# command
__lines=() # array
while IFS= read <&3 -r __line && [ "$__line" != '####' ]; do
echo "$__line";
__lines+=("$__line");
done
# variables
while IFS= read <&3 -r __line && [ "$__line" != '####' ]; do
if [[ -z "$__line" || ${__line:0:1} == '#' ]]; then
# blank line or comment line
continue;
fi
# name is ${__line%%:*}, prompt is ${__line##*:}
read -e -p "${__line#*:}$PS2" __input;
local ${__line%%:*}="$__input";
history -s $__input;
__tmp=${__line%%:*}
# echo ${!__tmp}
done
# variable substitution
__command=""
for i in "${__lines[@]}"; do
__tmp=$(eval "echo \"${i}\"");
if [[ ! -z $__command ]]; then
__command="${__command}${__newline}${__tmp}";
else
__command="${__tmp}";
fi
done
# echo -e "$__command"; # check variable substitution
__print_separator_line;
}
function __list_match {
local IFS __line;
local visited="false";
for file in ~/.cheats/"$*"*; do
! [[ -f "$file" ]] && continue; # skip things that aren't files
if [[ "$visited" = "true" ]]; then
__print_separator_line;
fi
tput bold; # print filename in bold
tput setaf 1
printf '%s\n' "${file##*/}" # print filename without other path parts
tput setaf 9
tput sgr0; # reset
{ # print the first two lines: description and command
# description
while IFS= read -r __line && [ "$__line" != '####' ]; do
echo "$__line"
done
__print_separator_line;
# command
while IFS= read -r __line && [ "$__line" != '####' ]; do
echo "$__line"
done
} < "$file"
visited="true";
done
if [[ $visited = "false" ]]; then
echo "No cheats with prefix \"$*\" found in ~/.cheats";
fi
}
function __print_separator_line {
local cols=${COLUMNS:-$(tput cols)};
tput setaf 1
for ((i=0; i<cols; i++)); do printf -; done # print a separator line with the width of the console
printf '\n'
tput setaf 9
}
if shopt -q progcomp 2> /dev/null; then
# bash completion
# to understand how this function manipulates variables,
# I recommend reading the sections "Parameter Expansion" and "Arrays" of the bash manpage.
function _cheats {
if [[ ! -d ~/.cheats ]]; then
return 0;
fi
local IFS;
IFS=' '; local compInput="${COMP_WORDS[*]:1}"; # strip away "cheats" command
local compInputArray=( $compInput );
IFS=$'\n'; local allCheats=(~/.cheats/*);
j=0;
shopt -q nullglob; hadNullglob=$?
shopt -s nullglob
for cheat in ~/.cheats/"$compInput"*; do
# cheat matches completion input
# we now need to translate
# "git re" ($compInput) and
# "~/.cheats/git rebase 1" ($cheat)
# into "rebase 1"
IFS=' ';
local currentCheatArray=( ${cheat#~/.cheats/} );
local firstDiffIndex=0;
while [[ ${currentCheatArray[$firstDiffIndex]} == ${compInputArray[$firstDiffIndex]}
&& $firstDiffIndex < ${#currentCheatArray} ]]; do
((firstDiffIndex++));
done
if [[ $COMP_CWORD == $firstDiffIndex ]]; then
((firstDiffIndex--)); # don’t override the current word; see #3
fi
COMPREPLY[$((j++))]="${currentCheatArray[*]:$firstDiffIndex}";
done
if ((hadNullglob==0)); then shopt -u nullglob; fi
}
complete -F _cheats cheats;
fi
nested X screen
####
export DISPLAY=:0 && Xephyr :1 -fullscreen -reset -terminate&
export DISPLAY=:1
####
####
Force (unreachable NFS) Lazy unmount
Detach the filesystem from the filesystem hierarchy now, and cleanup all
references to the filesystem as soon as it is not busy anymore.
####
umount -f -l "$mounting_point"
####
mounting_point: mounting point
####
@QiangF
Copy link
Author

QiangF commented Sep 29, 2016

This is a improved version of the original cheats.
https://github.com/lucaswerkmeister/cheats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment