Compare commits

..

6 Commits

Author SHA1 Message Date
bretello 0c7d0ec24f
zsh: cleanup bat configuration 2024-03-08 12:59:28 +01:00
bretello b05fde87c2
zsh: simplify retry() 2024-03-01 11:25:12 +01:00
bretello 04bfe043ab
vim: use <leader>N,P for :cnext,:cprevious 2024-03-01 11:25:12 +01:00
bretello 35575755ef
vim: improve Man command display 2024-03-01 11:25:12 +01:00
bretello 10c79f1bad
zsh: add watch_file command
uses inotifywait to watch writes on a file and executes a specific
action whenever this is changed (killing the previous process)
2024-03-01 11:25:12 +01:00
bretello 25037bde1f
zsh: cleanup colors/color functions 2024-03-01 11:25:11 +01:00
6 changed files with 95 additions and 63 deletions

View File

@ -84,23 +84,24 @@ export ZSH_AUTOSUGGEST_COMPLETION_IGNORE='* \#*' # prevent completion for commen
export LESS='-xRF-j12' # -j12: displays 12 lines before search results with / and ? export LESS='-xRF-j12' # -j12: displays 12 lines before search results with / and ?
if [[ "$(command -v bat)" || "$(command -v batcat)" ]]; then if [[ "$(command -v bat)" || "$(command -v batcat)" ]]; then
if [[ "$(command -v bat)" ]]; then if [[ "$(command -v bat)" ]]; then
alias cat=bat cmd=bat
export PAGER=bat
else else
alias cat=batcat cmd=batcat
export PAGER=batcat
fi fi
alias cat=$cmd
export PAGER=$cmd
export BAT_PAGER="less $LESS"
if [[ -n $THEME_PREFER_LIGHT ]]; then if [[ -n $THEME_PREFER_LIGHT ]]; then
export BAT_THEME="gruvbox-light" export BAT_THEME="gruvbox-light"
else else
export BAT_THEME="gruvbox-dark" export BAT_THEME="gruvbox-dark"
fi fi
export BAT_PAGER="less $LESS"
export PAGER=bat
## this breaks symlink testing with [ -h ], best not to activate it ## this breaks symlink testing with [ -h ], best not to activate it
# alias -g -- -h='-h 2>&1 | bat --language=help --style=plain' # alias -g -- -h='-h 2>&1 | bat --language=help --style=plain'
alias bathelp='bat --language=help --style=plain' alias bathelp="$cmd --language=help --style=plain"
alias -g -- --help='--help 2>&1 | bathelp ' alias -g -- --help='--help 2>&1 | bathelp '
fi fi

View File

@ -94,3 +94,34 @@ LIGHTCYAN="\e[36m"; LIGHTRED="\033[31m"; LIGHTPURPLE="\033[35m";
YELLOW="\e[33m"; WHITE="\033[37m" YELLOW="\e[33m"; WHITE="\033[37m"
BACKGROUND_BLACK="\e[40"; BACKGROUND_RED="\033[41"; BACKGROUND_BLACK="\e[40"; BACKGROUND_RED="\033[41";
BACKGROUND_GREEN="\e[42"; BACKGROUND_YELLOW="\033[43m" BACKGROUND_GREEN="\e[42"; BACKGROUND_YELLOW="\033[43m"
## Color string with given color. Usage: `color $NAME "string"`, available colors below
function color
{
local color=$1
shift 1
echo -e "${color}$@${CLEAR}"
}
## These functions return a colored version of the input string. Usage: red "string"
function red
{
echo -e "$Red$@$CLEAR"
}
function green
{
echo -e "$Green$@$CLEAR"
}
function yellow
{
echo -e "$Yellow$@$CLEAR"
}
function blue
{
echo -e "$Blue$@$CLEAR"
}
function bold
{
echo -e "$BOLD$@$CLEAR"
}

View File

@ -1,15 +1,3 @@
function warning {
echo -e "$(color $YELLOW Warning:) $@" >&2
}
function error {
echo -e "$(color $RED Error:) $@" >&2
}
function info {
echo -e "$(color $GREEN Info:) $@"
}
function switch-asciinema-user function switch-asciinema-user
{ {
local conf_dir="$HOME/.config/asciinema" local conf_dir="$HOME/.config/asciinema"
@ -34,22 +22,22 @@ function switch-asciinema-user
fi fi
new_id=$(echo $ids| fzf --height=$(($(wc -l <<< $ids)+2)) ${query} ) new_id=$(echo $ids| fzf --height=$(($(wc -l <<< $ids)+2)) ${query} )
if [[ -z $new_id ]]; then if [[ -z $new_id ]]; then
warning "Keeping curent id ($(color $UNDERLINE ${current_id}))" yellow "Keeping curent id ($(color $UNDERLINE ${current_id}))"
return return
fi fi
if [[ "$new_id" == "$current_id" ]]; then if [[ "$new_id" == "$current_id" ]]; then
warning "id $new_id is already set." yellow "id $new_id is already set."
return return
fi fi
local id_file="${conf_dir}/${new_id}" local id_file="${conf_dir}/${new_id}"
if [[ ! -f ${id_file} ]]; then if [[ ! -f ${id_file} ]]; then
error "${id_file} does not exist" red "${id_file} does not exist"
return return
fi fi
ln -sf "${id_file}" "${conf_dir}/install-id" && \ ln -sf "${id_file}" "${conf_dir}/install-id" && \
info "Set \"$new_id\"." || \ green "Set \"$new_id\"." || \
error "Could not set id \"$new_id\". (run with -l for a list of available profiles)" red "Could not set id \"$new_id\". (run with -l for a list of available profiles)"
} }

View File

@ -50,33 +50,20 @@ function ppath
echo "$PWD/$1" echo "$PWD/$1"
} }
## Color string with given color. Usage: `color $NAME "string"`, available colors in `colors.sh`
function color function warning {
{ echo -e "${Yellow}Warning:$CLEAR $@" >&2
local color=$1
shift 1
echo -e "${color}$@${CLEAR}"
} }
function error {
echo -e "${Red}Error:$CLEAR $@" >&2
}
## These functions return a colored version of the input string. Usage: red "string" function info {
function red echo -e "${Green}Info:$CLEAR $@"
{
echo -e "$Red$@$CLEAR"
}
function green
{
echo -e "$Green$@$CLEAR"
}
function blue
{
echo -e "$Blue$@$CLEAR"
}
function bold
{
echo -e "$BOLD$@$CLEAR"
} }
## Flashes the screen until user presses a key ## Flashes the screen until user presses a key
function flasher function flasher
{ {
@ -215,9 +202,7 @@ fvim() {
# retry command until it succeeds (waiting one second or $RETRY_INTERVAL) # retry command until it succeeds (waiting one second or $RETRY_INTERVAL)
function retry() { function retry() {
local _retry_interval until $@; do sleep ${RETRY_INTERVAL:-1}; done
if [[ -z "$RETRY_INTERVAL" ]]; then _retry_interval=1; else _retry_interval=$RETRY_INTERVAL; fi
until $@; do sleep $_retry_interval; done
} }
@ -291,15 +276,43 @@ function mangrep() {
# watches the given file and executes the given action whenever the file is changed. Usage: watch_file <file> <command> # watches the given file and executes the given action whenever the file is changed. Usage: watch_file <file> <command>
function watch_file() { function watch_file() {
if ! which inotifywait &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi if ! which inotifywait 2>&1 &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi
[[ -z "$1" ]] && "Usage: $0 <file> <action>" && return 1
local file [[ -z "$1" ]] && echo "Usage: $0 <file> <action>" && return 1
file="$1" local file=$1
shift shift
[[ -z "$@" ]] && "Usage: $0 <file> <action>" && return 1 [[ -z "$@" ]] && echo "Usage: $0 <file> <action>" && return 1
local action local action="$@"
action="$@" local _bg_job_pid
while inotifywait -e close_write "$file"; do zsh -c "$action"; done
local TRAPINT(){
if [[ -n ${_bg_job_pid} ]]; then
kill -9 ${_bg_job_pid}
fi
yellow '\nQuitting.'
break;
}
{
set +m # disable job control messages
while true; do
zsh -c "$action" &
_bg_job_pid=$!
disown
echo -e "$Cyan ==> Running \"$action\" with pid=${_bg_job_pid}$CLEAR"
# block until the file has been written to
inotifywait -e close_write "$file" &>/dev/null
# the job might have failed, ignore errors
kill -9 ${_bg_job_pid} &>/dev/null || true
done
} always {
# remove ctrl-c trap
unset -f TRAPINT
set -m # restore job control messages
}
} }

View File

@ -129,13 +129,12 @@ command! -nargs=1 -complete=command -bar -range Redir silent call Redir(<q-args>
function! Man(cmd) function! Man(cmd)
vnew vnew
let w:scratch=1 let w:scratch=1
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile filetype=man setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile filetype=man nonumber norelativenumber
setlocal nonumber norelativenumber let output = system('env COLUMNS=80 man ' . a:cmd)
let output = system('man ' . a:cmd)
call setline(1, split(output, '\n')) call setline(1, split(output, '\n'))
endfunction endfunction
command! -nargs=1 -complete=shellcmd Man silent call Man(<q-args>) command! -nargs=1 -complete=shellcmd Vert Man silent call Man(<q-args>)
" Execute the given command and print it in a scratch buffer " Execute the given command and print it in a scratch buffer
function! Cmd(cmd) function! Cmd(cmd)

View File

@ -46,9 +46,9 @@ nmap <leader>ll :botright lope<CR>
nmap <leader>cx :hide<CR> nmap <leader>cx :hide<CR>
nmap <leader>co ggVGy:tabnew<CR>:set syntax=qf<CR>pgg nmap <leader>co ggVGy:tabnew<CR>:set syntax=qf<CR>pgg
" next cope error " next cope error
map <leader>cn :cn<CR> map <leader>N :cnext<CR>
" previous cope error " previous cope error
map <leader>cp :cp<CR> map <leader>P :cprevious<CR>
"disable highlighting for current word "disable highlighting for current word
map <silent> <leader><CR> :noh<CR> map <silent> <leader><CR> :noh<CR>