Skip to content

Instantly share code, notes, and snippets.

@rhwlo
Created November 16, 2015 02:32
Show Gist options
  • Save rhwlo/3256cfa0370a2f98d72d to your computer and use it in GitHub Desktop.
Save rhwlo/3256cfa0370a2f98d72d to your computer and use it in GitHub Desktop.
configurations for fish!
set -u tmux_session (whoami)
set -u EDITOR /usr/bin/vim
set -u GIT_EDITOR /usr/bin/vim
fish_vi_mode
source $HOME/.config/fish/functions/pasteboard.fish
if test $TERM != "screen"
if tmux list-windows -t $tmux_session
set new_window (tmux new-window -P -d -t $tmux_session "/usr/bin/fish" | cut -d':' -f2 | cut -d'.' -f1)
set new_session $tmux_session-(date +%s)
exec tmux new-session -s $new_session -t $tmux_session \; select-window -t "$new_window"
else
exec tmux new-session -s $tmux_session "/usr/bin/fish"
end
end
set -u orig_path $PATH
function power_prompt --description "print out the power prompt"
# I left this stuff in for debugging purposes only:
# power_info() {
# device="$1"; shift
# prefix=$1; shift
# for line in $(upower -d | awk '/'$device'$/,/^$/ { if (match($1, ":")) { sub(":","",$1); gsub("-","_",$1); printf("%s=%s\n",$1,$2); } }'); do
# eval "${prefix}_${line}";
# done
# }
# power_info battery bat
# power_info line-power ac
function power_info --description "get the power info for a single"
set -l device $argv[1]
set -l key $argv[2]
upower -d | sed -rne '/'$device'$/,/^$/ { /\s+'$key': / { s/^.*'$key':\s*//; p; }; };' | head -n 1
end
set -l ac_online (power_info "line-power" "online")
set -l bat_state (power_info "battery" "state")
set -l bat_percentage (power_info "battery" "percentage")
echo -n (set_color yellow)
if power_info "line-power" "online" | grep -q "yes"
echo -n "⚡"
switch $bat_state
case "discharging"
echo -n "«"
case "fully-charged"
echo -n "⚡"
case "charging"
echo -n "»"
end
else
switch $bat_state
case "discharging"
echo -n "‹"
case "fully-charged"
echo -n " "
case "charging"
echo -n "›"
end
end
echo -n (set_color normal)
if not echo $bat_state | grep -q "fully-charged"
switch (echo "$bat_percentage 24" | sed -e 's@%@ / @' | bc)
case 0
echo -n " "
case 1
echo -n (set_color red)"▂"(set_color normal)
case 2
echo -n (set_color yellow)"▄"(set_color normal)
case 3
echo -n (set_color yellow)"▆"(set_color normal)
case 4
echo -n (set_color green)"█"(set_color normal)
end
else
echo -n (set_color green)"█"(set_color normal)
end
end
function coloredHostname
function getColorFrom --description "get color from hex digit"
switch (printf "ibase=16;%s%%8\n" $argv[1] | bc)
case 0
echo -n "black"
case 1
echo -n "red"
case 2
echo -n "green"
case 3
echo -n "brown"
case 4
echo -n "blue"
case 5
echo -n "purple"
case 6
echo -n "yellow"
case 7
echo -n "cyan"
end
end
set -l colorDigits (hostname | sha1sum | sed -re 's/^.*(..) .*$/\1/' | tr '[a-f]' '[A-F]')
# set -l backgroundColor (getColorFrom (echo -n $colorDigits | cut -c1))
# removed the background color because honestly it's a bit distracting
set -l foregroundColor (getColorFrom (echo -n $colorDigits | cut -c2))
switch (printf "ibase=16;%s/8\n" (echo -n $colorDigits | cut -c2) | bc)
case 0
set -l foregroundBold "-o"
case 1
set -l foregroundBold ""
end
echo -n (set_color $foregroundBold $foregroundColor)(hostname)(set_color normal)
end
function fish_prompt --description "the left portion of the prompt"
# per zsh:
# [blue date +%H:%M:%S] [battery indicator] [PATH modifications] [directory]
# [hostname] %
set -l colored_hrdate (set_color blue)(date +"%H:%M:%S")(set_color normal)
set -l battery_indicator (power_prompt)
set -l path_modifications (diff (echo $PATH | tr " " "\n" | sort | psub) (echo $orig_path | tr " " "\n" | sort | psub) | grep -c '^[<>]')
set -l colored_path_mods
if [ $path_modifications -eq 0 ]
set colored_path_mods (set_color green)"{0}"(set_color normal)
else
set colored_path_mods (set_color red)"{$path_modifications}"(set_color normal)
end
set -l colored_cwd (set_color green)(prompt_pwd)(set_color normal)
set -l colored_percent (set_color yellow)"%"(set_color normal)
echo $colored_hrdate $battery_indicator $colored_path_mods $colored_cwd
echo -s -n (coloredHostname) " " $colored_percent " "
end
function fish_right_prompt --description "the right portion of the prompt"
# per zsh, either:
# [last-status] [git_info]
# or
# [last-status] [blue date +%s]
set -l last_status $status
set -l unixtime (date +"%s")
set -l coloredStatus
if [ $last_status -eq 0 ]
set coloredStatus (set_color green)"+"(set_color normal)
else
set coloredStatus (set_color red)$last_status(set_color normal)
end
if [ -z (__fish_git_prompt) ]
echo -n $coloredStatus (set_color blue)(date +%s)(set_color normal)
else
echo -n $coloredStatus (set_color blue)(__fish_git_prompt)(set_color normal)
end
end
if which xclip >/dev/null ^/dev/null
function pbpaste --description "emulates Apple's pbpaste function if xclip is installed"
xclip -out -selection clipboard
end
function pbcopy --description "emulates Apple's pbcopy function if xclip is installed"
xclip -in -selection clipboard
end
function pbedit --description "pbpastes to a file, opens it in EDITOR, and then copies it after"
set -l pbfile (mktemp)
pbpaste > $pbfile
eval $EDITOR $pbfile
pbcopy < $pbfile
rm $pbfile
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment