Skip to content

Instantly share code, notes, and snippets.

@Dinir
Last active September 29, 2016 14:16
Show Gist options
  • Save Dinir/a344affcad551dcbb248 to your computer and use it in GitHub Desktop.
Save Dinir/a344affcad551dcbb248 to your computer and use it in GitHub Desktop.
My Fish Prompt
# I made these shortcuts to make it comfortable to change colors later.
# Also you can just put set_color manually instead.
set blk (set_color black)
set red (set_color red)
set grn (set_color green)
set yel (set_color yellow)
set blu (set_color blue)
set mgt (set_color magenta)
set cyn (set_color cyan)
set lgry (set_color white)
set gry (set_color black --bold)
set lred (set_color red --bold)
set lgrn (set_color green --bold)
set lyel (set_color yellow --bold)
set lblu (set_color blue --bold)
set lmgt (set_color magenta --bold)
set lcyn (set_color cyan --bold)
set wht (set_color white --bold)
set nrm (set_color normal)
# These are variables indicating the battery's remaining time.
# If there are no battery, these will be left as `$nrm`, which has no color set.
# Think these as a 'default' color value for the prompt and change them as you feel like.
set -g batColor $nrm
set -g batColorSub $nrm
function prompt_section -d "prints a section with a following comma"
if [ $argv[1] != "" ]
printf $argv[1]
printf "$batColorSub, $nrm"
end
end
function color_section -d "prints a section covered in a desired color"
printf $argv[1]
printf $argv[2]
printf $nrm
end
function prompt_battery -d "shows battery status"
# Store the data of battery status, percentage, estimated time left.
set -l str (acpi | grep -oP 'Full|Charging|Discharging|\d+%|\d+:\d+' | tr --truncate-set1 '\n' ' ' | read batStatus batPercent batTime)
# acpi doesn't show time left around the moment of plugging/unplugging AC adapter. Handle this case.
if [ $batTime = "" ]
set batTime "===="
end
if [ $batStatus = "Charging" ]
# if charging, show percentage.
set batColor $lblu
set batColorSub $blu
color_section $batColor (printf '%3s' (echo $batPercent | sed "s/%//"))
color_section $batColorSub "%%%%"
else if [ $batStatus = "Full" ]
# if full, show it's full.
set batColor $lcyn
set batColorSub $cyn
#set batTime $batColor"FULL"$nrm
color_section $batColor "FULL"
else
# if discharging, set color according to how much the time left.
if [ $batTime = "====" ]
# paint it with a color when the time is not stored.
set batColor $lgry
set batColorSub $lgry
color_section $batColorSub "===="
else
# if time presents, convert it in minutes.
set -l batTimeSeg (echo $batTime | sed "s/^0*//" | sed "s/:/\n/" | tr -d ' ')
if [ $batTimeSeg[1] = "" ]
set batTimeSeg[1] 0
end
set -l timeMin (math "$batTimeSeg[1]*60+$batTimeSeg[2]")
if [ $timeMin -le "10" ]
# less than 10 minutes left. Color it red.
set batColor $lred
set batColorSub $red
else if [ $timeMin -le "30" ]
# less than 30 minutes left. Color it yellow.
set batColor $lyel
set batColorSub $yel
else
# more than 30 minutes left. Color it green.
set batColor $lgrn
set batColorSub $grn
end
if [ $timeMin -lt "300" ]
# only show the time when acpi calculation is stabilized.
# estimated maximum battery time for my device is 5 hours, or 300 minutes.
color_section $batColor $batTimeSeg[1]
color_section $batColorSub ":"
color_section $batColor $batTimeSeg[2]
else
# if the calculation is not stabled, show this placeholder instead.
color_section $batColorSub "===="
end
end
end
end
function prompt_load -d "shows loadavg"
# It's known that the load being high can affect battery time left.
# So let it show this information only when it's high enough to affect the battery.
set loadMin (echo (uptime | grep -o '[0-9]\+\.[0-9]\+' | head -n1))
# Threshold I set is 0.60. 1.00 would works.
if [ (math $loadMin \* 100 /1) -gt 60 ]
# color the information according to the battery level.
if [ $batColor = $lred ]
color_section (set_color -b red; set_color black) $loadMin
else if [ $batColor = $lyel ]
color_section $red $loadMin
else
color_section $yel $loadMin
end
else
# or don't show anything if it's not high enough.
echo ""
end
end
function prompt_time -d "prints the time"
date "+$batColorSub%H:%M:%S$nrm"
end
function prompt_dir -d "prints the current directory"
set -l slashes $red
set -l upperdirs $yel
set -l lastdir $lred
if [ $batColor != $lred ]
# use different color when battery is not low.
set slashes $batColorSub
set upperdirs $cyn
set lastdir $lcyn
else
end
printf $upperdirs(prompt_pwd | sed "s/\//$slashes\/$upperdirs/g" | sed "s/\(.*\)\/[^m]*m/\1\/$lastdir/")$nrm
end
function prompt_gitstatus -d "show git status"
set -l branch (git rev-parse --abbrev-ref HEAD ^ /dev/null)
if [ $branch ]
# when the directory is a git repository, show the branch name.
set -l dirties (git status --porcelain | wc -l)
color_section $gry ":"
color_section $lgry $branch
if [ $dirties != 0 ]
# when there is any modified file, show the amount as well.
color_section $gry ":"
color_section $gry $dirties
end
else
# or don't show anything if it's not a git repository.
echo -n ""
end
end
function prompt_proctime -d "show the time taken for the last command"
if [ $CMD_DURATION ] # I think this line is unnecessary, but my computer emits an error without it.
if [ $CMD_DURATION -ne 0 ] # show the time only when there was actual last command.
# show the time in ms.
set -l lastproctime $CMD_DURATION"ms"
if [ $CMD_DURATION -ge 1000 ]
# if it is longer than a single second, show the time in seconds.
set lastproctime (math $CMD_DURATION/1000)"s"
end
color_section $nrm " "
color_section $batColorSub $lastproctime
end
end # this line is unnecessary too.
end
function fish_prompt
# the `if command` line checks if a command exists. Will not execute if it doesn't exist.
# If it's not a laptop there won't be `acpi`. If it's a fresh install `git` won't be as well.
if command -v acpi >/dev/null 2>&1
prompt_section (prompt_battery)
end
prompt_section (prompt_load)
prompt_section (prompt_time)
prompt_dir
if command -v git >/dev/null 2>&1
prompt_gitstatus
end
prompt_proctime
printf \n$nrm\$" "
end
@Dinir
Copy link
Author

Dinir commented Apr 6, 2016

newfishprompt

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