Skip to content

Instantly share code, notes, and snippets.

@dzungtran
Last active July 25, 2019 03:03
Show Gist options
  • Save dzungtran/451dd2f4b3a76b7e50713e39e6190674 to your computer and use it in GitHub Desktop.
Save dzungtran/451dd2f4b3a76b7e50713e39e6190674 to your computer and use it in GitHub Desktop.
Custom ys theme zsh
# Clean, simple, compatible and meaningful.
# Tested on Linux, Unix and Windows under ANSI colors.
# It is recommended to use with a dark background.
# Colors: black, red, green, yellow, *blue, magenta, cyan, and white.
#
# Mar 2013 Yad Smood
# VCS
YS_VCS_PROMPT_PREFIX1=" %{$fg[white]%}on%{$reset_color%} "
YS_VCS_PROMPT_PREFIX2=":%{$fg[cyan]%}"
YS_VCS_PROMPT_SUFFIX="%{$reset_color%}"
YS_VCS_PROMPT_DIRTY=" %{$fg[red]%}x"
YS_VCS_PROMPT_CLEAN=" %{$fg[green]%}o"
# Git info
local git_info='$(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="${YS_VCS_PROMPT_PREFIX1}git${YS_VCS_PROMPT_PREFIX2}"
ZSH_THEME_GIT_PROMPT_SUFFIX="$YS_VCS_PROMPT_SUFFIX"
ZSH_THEME_GIT_PROMPT_DIRTY="$YS_VCS_PROMPT_DIRTY"
ZSH_THEME_GIT_PROMPT_CLEAN="$YS_VCS_PROMPT_CLEAN"
# HG info
local hg_info='$(ys_hg_prompt_info)'
ys_hg_prompt_info() {
# make sure this is a hg dir
if [ -d '.hg' ]; then
echo -n "${YS_VCS_PROMPT_PREFIX1}hg${YS_VCS_PROMPT_PREFIX2}"
echo -n $(hg branch 2>/dev/null)
if [ -n "$(hg status 2>/dev/null)" ]; then
echo -n "$YS_VCS_PROMPT_DIRTY"
else
echo -n "$YS_VCS_PROMPT_CLEAN"
fi
echo -n "$YS_VCS_PROMPT_SUFFIX"
fi
}
local exit_code="%(?,,C:%{$fg[red]%}%?%{$reset_color%})"
YS_ZSH=~/.zsh
YS_WEATHER_INFO_FILE="${YS_ZSH}/weather.json"
YS_OPENWEATHER_APP_ID="YS_OPENWEATHER_APP_ID"
YS_OPENWEATHER_LOC_ID="YS_OPENWEATHER_LOC_ID"
ys_load_weather_info() {
# Requires install jq
if [ -x "$(command -v jq)" ]; then
local api_url="https://api.openweathermap.org/data/2.5/weather?id=$YS_OPENWEATHER_LOC_ID&APPID=$YS_OPENWEATHER_APP_ID&units=metric"
weather_info=$(curl -s $api_url)
if [ ! -f $YS_WEATHER_INFO_FILE ]; then
touch $YS_WEATHER_INFO_FILE
fi
cod=$(echo $weather_info | jq '.cod')
if [ $cod == 200 ]; then
echo $weather_info | jq --arg ts $(date +%s) '. + {ts: $ts}' > $YS_WEATHER_INFO_FILE
fi
fi
}
ys_get_weather_emoji() {
case "$1" in
"Thunderstorm")
echo "🌩"
;;
"Drizzle"|"Rain")
echo "🌩"
;;
"Snow")
echo "☃️"
;;
"Mist"|"Haze"|"Fog"|"Ash"|"Smoke")
echo "🌫"
;;
"Dust"|"Sand"|"Squall")
echo "🌵"
;;
"Tornado")
echo "🌪"
;;
"Clouds")
echo "☁️"
;;
"Clear")
echo "☀️"
;;
*)
echo "$1"
esac
}
# weather info
ys_read_weather_info() {
if [ -f $YS_WEATHER_INFO_FILE ]; then
local weather_info=$(cat $YS_WEATHER_INFO_FILE)
if [ -x "$(command -v jq)" ]; then
cod=$(echo ${weather_info} | jq '.cod')
if [ $cod == 200 ]; then
local temp=$(echo ${weather_info} | jq '.main.temp')
local loc=$(echo ${weather_info} | jq '.name' | sed 's/"//g')
local tmain=$(echo ${weather_info} | jq '.weather[0].main' | sed 's/"//g')
echo "$loc $(ys_get_weather_emoji ${tmain}) $temp°C"
else
echo "unavaiable"
fi
else
echo "unavaiable"
fi
else
echo "unavaiable"
fi
}
ys_interval_update_weather_info() {
sleep_time=$(( 15 * 3600 ))
while true;
do
if [ -f $YS_WEATHER_INFO_FILE ]; then
local weather_info=$(cat $YS_WEATHER_INFO_FILE)
if [ -x "$(command -v jq)" ]; then
ts=$(echo ${weather_info} | jq '.ts' | sed 's/"//g')
ts=$(( $ts + $sleep_time ))
if [ $ts > $(date +%s) ]; then
ys_load_weather_info
ys_read_weather_info
fi
fi
fi
# Sleep 15 minutes
sleep $sleep_time;
done
}
# Run update interval every 15 minutes in background
(ys_interval_update_weather_info &>/dev/null &)
weather() {
ys_load_weather_info
ys_read_weather_info
}
local weather_info='$(ys_read_weather_info)'
# Prompt format:
#
# PRIVILEGES USER @ MACHINE in DIRECTORY on git:BRANCH STATE [TIME] C:LAST_EXIT_CODE
# $ COMMAND
#
# For example:
#
# % ys @ ys-mbp in ~/.oh-my-zsh on git:master x [21:47:42] C:0
# $
PROMPT="
%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n) \
%{$fg[white]%}@ \
%{$fg[green]%}$weather_info \
%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
${hg_info}\
${git_info}\
\
%{$fg[white]%}[%*] $exit_code
%{$terminfo[bold]$fg[red]%}$ %{$reset_color%}"
## Origin PROMPT
#PROMPT="
#%{$terminfo[bold]$fg[blue]%}#%{$reset_color%} \
#%(#,%{$bg[yellow]%}%{$fg[black]%}%n%{$reset_color%},%{$fg[cyan]%}%n) \
#%{$fg[white]%}@ \
#%{$fg[green]%}%m \
#%{$fg[white]%}in \
#%{$terminfo[bold]$fg[yellow]%}%~%{$reset_color%}\
#${hg_info}\
#${git_info}\
# \
#%{$fg[white]%}[%*] $exit_code
#%{$terminfo[bold]$fg[red]%}$ %{$reset_color%}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment