diff --git a/bashtop b/bashtop index 156549d..0bf853a 100755 --- a/bashtop +++ b/bashtop @@ -125,6 +125,12 @@ menu_quit=( "│─┼┐│ │ │ │ " "└─┘└└─┘ ┴ ┴ ") +menu_update=( +"┬ ┬┌─┐┌┬┐┌─┐┌┬┐┌─┐" +"│ │├─┘ ││├─┤ │ ├┤ " +"└─┘┴ ─┴┘┴ ┴ ┴ └─┘" +) + menu_options_selected=( "╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗" "║ ║╠═╝ ║ ║║ ║║║║╚═╗" @@ -136,8 +142,15 @@ menu_help_selected=( menu_quit_selected=( "╔═╗ ╦ ╦ ╦╔╦╗ " "║═╬╗║ ║ ║ ║ " -"╚═╝╚╚═╝ ╩ ╩ ") +"╚═╝╚╚═╝ ╩ ╩ ") +menu_update_selected=( +"╦ ╦╔═╗╔╦╗╔═╗╔╦╗╔═╗" +"║ ║╠═╝ ║║╠═╣ ║ ║╣ " +"╚═╝╩ ═╩╝╩ ╩ ╩ ╚═╝" +) + +declare -r remote_src="https://raw.githubusercontent.com/aristocratos/bashtop/master/bashtop" declare -A cpu mem swap proc net box theme disks declare -a cpu_usage cpu_graph_a cpu_graph_b color_meter color_temp_graph color_cpu color_cpu_graph cpu_history color_mem_graph color_swap_graph declare -a mem_history swap_history net_history_download net_history_upload mem_graph swap_graph proc_array download_graph upload_graph trace_array @@ -151,6 +164,8 @@ declare time_left timestamp_start timestamp_end timestamp_input_start timestamp_ declare no_epoch proc_det proc_misc2 sleeping=0 detail_mem_graph proc_det2 proc_out curled git_version has_iostat declare esc_character tab backspace sleepy late_update skip_process_draw winches quitting theme_int notifier declare -a disks_free disks_total disks_name disks_free_percent disks_io saved_key themes +declare -a menus=("options" "help" "quit") color + printf -v esc_character "\u1b" printf -v tab "\u09" printf -v backspace "\u7F" @@ -207,6 +222,12 @@ else } fi + +has_command () { #? check the avaibility of the command + command -v $1 > /dev/null +} + + init_() { #? Collect needed information and set options before startig main loop local i #* Set terminal options, save and clear screen @@ -262,7 +283,7 @@ init_() { #? Collect needed information and set options before startig main loop #* Check if newer version of bashtop is available from https://github.com/aristocratos/bashtop if [[ -n $curled ]]; then - if ! get_value -v git_version -ss "$(curl -m 2 --raw -r 0-3500 https://raw.githubusercontent.com/aristocratos/bashtop/master/bashtop 2>/dev/null)" -k "version=" -r "[^0-9.]"; then unset git_version; fi + if ! get_value -v git_version -ss "$(curl -m 2 --raw -r 0-3500 ${remote_src} 2>/dev/null)" -k "version=" -r "[^0-9.]"; then unset git_version; fi fi #* Draw banner to banner array and notify about new updates @@ -284,6 +305,7 @@ init_() { #? Collect needed information and set options before startig main loop done print -v banner_out[y] -rs -fg cc -b "← esc" if [[ -n $git_version && $git_version != "$version" ]]; then + menus+=("update") print -v banner_out[y] -rs -fg "#80cc80" -r 15 "[${git_version} available!]" -r $((9-${#git_version})) if [[ -n $notifier ]]; then notify-send -u normal\ @@ -450,6 +472,96 @@ quit_() { #? Clean exit exit 0 } +update_ () { + + local -a root_dirs=("/bin/" "/usr/bin/") + local btop_exec=${remote_src##*/} + local btop_path + + for xbin in ${root_dirs[@]} ; do + if [[ -x ${xbin}${btop_exec} ]] ; then + btop_path=${xbin}${btop_exec} + fi + done + + # the update will only work if bashtop has been installed in the system. + if [[ -z ${btop_path} ]] ; then + if [[ -n ${notifier} ]] ; then notify-send "install bashtop in your system first" ;fi + return + fi + + declare -A pkg_distro_manager=( + [apt-get]="--only-upgrade install bashtop -y" # debian based + [dnf]="upgrade bashtop" # fedora + [yum]="update bashtop" # redhat ? + [zypper]="update bashtop" # open suse + [pacman]="-S bashtop" # arch + ) + + # get dynamicly the related package manager on the current distro + local local_pkgman related_opt + for pkg in ${!pkg_distro_manager[@]} ; do + if has_command ${pkg} ; then + local_pkgman=${pkg} + related_opt=${pkg_distro_manager[$pkg]} + fi + done + + # check if the new release version are available via package manager + read -a pkgman_version <<< $(sudo ${local_pkgman} ${related_opt} | grep -Po '(\d+\.)+\d+') + + # otherwise download directly from remote repository + if [[ -z ${pkgman_version[0]} ]] || [[ ${pkgman_version[0]} != ${git_version} ]] ; then + $(curl ${remote_src} -o ${btop_path}) + if [[ $? -eq 0 ]] ; then + if [[ -n ${notifier} ]] ;then notify-send "$btop_exec updated to v${git_version}" ; fi + + if [[ 'menus[${#menus[@]}-1]' == "update" ]] ;then unset 'menus[${#menus[@]}-1]' ; fi # unset update label + else + if [[ -n ${notifier} ]] ;then notify-send "UPDATE FAILED : require root user" -i face-sad ; fi + fi + fi +} + +[[ "ARGS::PARSE" ]] +{ +usage() { + +cat << EOF + +usage : bashtop [ OPT ] + + --update : upgrade bastop to the new version release + --version | -v : get version of bashtop + --help | -h : help usage + +EOF +} + + +if [[ -n ${1} ]] ; then + case ${1} in + "--update") + if [[ $UID -ne 0 ]] ; then + read -p "require root user : " -s -t 15 + if [[ -n ${REPLY} ]] ; then + sudo -S <<< ${REPLY} $0 --update + fi + else + sleep 1 + update_ + exit + fi + ;; + "--version"|"-v") + echo -e "Bashtop v.${version}" ; exit 0 ;; + "--help"|"-h") + usage ; exit 0 ;; + esac + fi +} + + sleep_() { #? Restore terminal options, stop and send to background if caught SIGTSTP (ctrl+z) tput rmcup stty echo @@ -2672,8 +2784,6 @@ unpause_() { #? Unpause menu_() { #? Shows the main menu overlay local menu i count keypress selected_int=0 selected up local_rez d_banner=1 menu_out bannerd skipped menu_pause out_out wait_string trans - local -a menus=("options" "help" "quit") color - until false; do #* Put program to sleep if caught ctrl-z @@ -2740,6 +2850,7 @@ menu_() { #? Shows the main menu overlay options) options_ ;; help) help_ ;; quit) quit_ ;; + update)update_;; esac ;; m|M|escape|backspace) break ;;