代码之家  ›  专栏  ›  技术社区  ›  esaruoho

根据电池状态、时间和百分比,在外壳提示中动态使用颜色

  •  1
  • esaruoho  · 技术社区  · 7 年前

    我试图创建一个动态提示,使用颜色编码提供电池情况的视觉提示。

    我将使用 pmset -g batt 获取所有这些信息。

    1. 电池百分比
    2. 蓄电池充电、放电、充电-状态
    3. 收费或 出院(=剩余时间)

    数字1可以通过 pmset -g batt | egrep '([0-9]+\%).*' -o --colour=auto | cut -f1 -d';' 100% (嗯,目前,因为我的电池已充满电)。

    pmset -g batt | awk '/charging|discharging|charged/ {print $4}' | cut -f1 -d';' ,但要使其在提示中正常工作,您必须将其修改为 pmset -g batt | awk '/charging|discharging|charged/ {print $4}' | cut -f2 -d';' " charged" 而不是 "charged" , " discharging" 而不是 "discharging" " charging" "charging" . 那很好,我想,但我想这可以再进一步。

    pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]' -但是,如果你的电池充满电,你会得到一个凉爽的天气 0:00

    好的,我们已经确定这种信息可以提取出来,并且至少可以在终端本身中显示,那么动态提示问题呢?


    好吧,这是我现在的情况:

    [100%] [] [ charged] [Sat Nov 10 21:24:34] [~] >>

    这并不完美。例如 [] 根本不需要显示,考虑到它将显示的信息是 . 这个 [ charged] 也有点不必要。

    下面是我想问的问题:

    1. 当电池已满时,只需显示百分比即可,否“ [time left / time to charge] [收费]
    2. [time left] (颜色百分比可能为.)。。修改?)。
    3. [时间还剩]
    4. 当电池处于临界状态时(比如15%)(百分比可能为红色)

    现在,我做到了这一点:

    export PS1="[\$(pmset -g batt | egrep '([0-9]+\%).*' -o --colour=auto | cut -f1 -d';')] [\$(pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')] [\$(pmset -g batt | awk '/charging|discharging|charged/ {print $4}' | cut -f2 -d';')] [\d \t]"

    这让我 [100%] [0:00] [ charged] [Sat Nov 10 21:32:38]

    cut -f1 cut -f2 终端和提示符之间的差异。如果我把 切割-f1 export PS1 提示,我得到这个:

    [100%] [0:00] [ -InternalBattery-0 (id=4194403) 100%] [Sat Nov 10 21:34:44]

    不管怎么说,我想一定有办法“哦,0:00?根本不显示提示的这一部分”-但我不知道该怎么做。

    所以,总而言之,我尝试使用“充电、充电、放电”来设置电池百分比颜色,充电=常规颜色,充电=黄色,放电=绿色?,小于20%=红色。5%眨眼?:D

    我还尝试动态地在提示符中显示“剩余充电时间”和“剩余充电时间”。

    pmset-g电池 导致 (no estimate) 关于剩余充电时间

    p、 p.s.额外加分#2分,用于确定如何处理(即,这是一个不存在的状态 (无估计) 但有时会发生。

    Now drawing from 'AC Power' -InternalBattery-0 (id=4194403) 89%; AC attached; not charging present: true

    1 回复  |  直到 7 年前
        1
  •  2
  •   New Alexandria    7 年前

    您的问题实际上是关于如何处理复杂的提示。 这里实际复杂的问题是如何在提示中处理ANSI颜色的有条件使用。

    in my dotfiles 在里面 profile functions_colors.sh .

    你真正想要的是

    export PS1="[\$(__batt_pct)]\$(__batt_time) [\$(__batt_state)] [\d \t] "
    

    然后

    yellow=$(tput setaf 184)
    green=$(tput setaf 120)
    red=$(tput setaf 160)
    reset=$(tput init)
    
    function __batt_pct() {
      bpct=$(pmset -g batt | egrep '([0-9]+)%.*' -o | cut -f1 -d';')
      bpct=${bpct%?} # remove last char (%)
      case 1 in
        $(($bpct <= 15))) echo "$red$bpct%$reset" ;;
        $(($bpct <= 65))) echo "$yellow$bpct%$reset" ;;
                       *) echo "$green$bpct%$reset" ;;
      esac
    }
    
    # now, as a function, we can easily handle a conditional
    function __batt_time() {
      btime=$(pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')
      if [[ "$btime" == "0:00" ]]; then echo ''; else echo " [$btime]"; fi
    }
    
    # I need to cut field-1 for this to trim evenly
    function __batt_state() {
      bstate=$(pmset -g batt | awk '/charging|discharging|charged/ {print $4}' | cut -f1 -d';')
    }
    

    我们使用 __ 因为对于不需要在CLI上手动使用的函数名,您需要遵循unix约定。

    至于颜色,只要 follow any ANSI color guide ,例如,我在dot文件中链接的文件。那就改变这个 __batt_state

    # I need to cut field-1 for this to trim evenly
    function __batt_state() {
      bstate=$(pmset -g batt | awk '/charging|discharging|charged/ {print $4}' | cut -f1 -d';')
      case "$bstate" in
        charged)
          echo "$green$bstate$reset"
          ;;
        charging)
          echo "$yellow$bstate$reset"
          ;;
        discharging)
          echo "$red$bstate$reset"
          ;;
        *)
          echo $bstate
          exit
      esac
    }
    

    I decided to drop all of this in a repo ,这样我可以更容易地使用它。