#!/usr/bin/fish # QShell # Simple error logger library for Fish-Shell # Repository # Install: # fish qshell.fish function qshell_debug_message_help -a error exp if contains -- '--help' $exp; or contains -- '-h' $exp echo "QShell $error message function" echo " Format $error message" echo "Using:" echo " $error AUTHOR MESSAGE" echo " $error --help" echo " $error -h" return 1 end return 0 end function qshell_message_level -a author message level color exp if not set -q __logger_file qshell_debug_message_help success $exp && \ printf "\033[1m$author: \033[$(printf $color)m$level: \033[0m$message\n" else if not set -q __logger_file_max_size; or not echo $__logger_file_max_size | grep '[0-9]' &> /dev/null set -g __logger_file_max_size $(math "1024 * 128") end set -l size $(stat -c %s $__logger_file) if test $size -ge $__logger_file_max_size cat $__logger_file | tail -c $(math "$__logger_file_max_size / 2") > $__logger_file end qshell_debug_message_help success $exp && \ printf "\033[1m$author: \033[$(printf $color)m$level: \033[0m$message\n" \ >> $__logger_file end end function error -a author message qshell_message_level $author $message "error" "31" $argv end function warning -a author message qshell_message_level $author $message "warning" "35" $argv end function debug -a author message qshell_message_level $author $message "debug" "33" $argv end function info -a author message qshell_message_level $author $message "info" "34" $argv end function success -a author message qshell_message_level $author $message "success" "32" $argv end function logfile -a file max_size if contains -- '--help' $argv; or contains -- '-h' $argv echo "QShell logfile function" echo " Set file for logging" echo "Using:" echo " logfile FILE_NAME MAX_BYTES_SIZE" echo " logfile --help" echo " logfile -h" return end if set -q max_size set -g __logger_file_max_size $(echo $max_size | tr -cd '[:digit:]') end set -g __logger_file "$file" end function logreset if contains -- '--help' $argv; or contains -- '-h' $argv echo "QShell logreset function" echo " Reset logging variables" echo "Using:" echo " logreset" echo " logreset --help" echo " logreset -h" return end set -e __logger_file set -e __logger_file_max_size end function logview if contains -- '--help' $argv; or contains -- '-h' $argv echo "QShell logview function" echo " View logs" echo "Using:" echo " logview" echo " logview --help" echo " logview -h" return end if not set -q __logger_file return end cat $__logger_file end set -l functions \ 'qshell_debug_message_help' \ 'qshell_message_level' \ 'error' \ 'warning' \ 'info' \ 'debug' \ 'success' \ 'logfile' \ 'logreset' \ 'logview' set -l user_catalog $(pwd) if not set -q __fish_config_dir printf "\033[1mqshell: \033[31merror: \033[0mcannot find variable __fish_config_dir\n" exit 1 end cd $__fish_config_dir if not test -f config.fish printf "\033[1mqshell: \033[35mwarning: \033[0mcannot find file config.fish\n" touch config.fish end set -l file_end_char $(cat config.fish | tail -c 1) if test "$file_end_char" != "$(printf "\n")" echo >> config.fish end if cat config.fish | grep "# Auto generated by QShell\.fish" &> /dev/null printf "\033[1mqshell: \033[35mwarning: \033[0mRewriting old function in config file\n" end cat config.fish | \ sed "s/# Auto generated by QShell\.fish.*# End auto generated by QShell\.fish//g" \ > config.fish echo "# Auto generated by QShell.fish" >> config.fish for name in $functions functions $name >> config.fish echo >> config.fish end echo "# End auto generated by QShell.fish" >> config.fish info "qshell" "QShell success installed!" cd $user_catalog