Просмотр исходного кода

fix powerline custom left/right status, add platform detection

Weiyi Lou 13 лет назад
Родитель
Сommit
400ca448a2

+ 17 - 0
tmux/tmux-powerline-custom/config.sh

@@ -0,0 +1,17 @@
+#!/bin/sh
+# Cofigurations for tmux-powerline.
+
+if [ -z "$PLATFORM" ]; then
+  UNAME=$(uname)
+  if [ $UNAME == 'Linux' ]; then
+    export PLATFORM="linux"
+  fi
+  if [ $UNAME == 'Darwin' ]; then
+    export PLATFORM="mac"
+  fi
+fi
+
+if [ -z "$USE_PATCHED_FONT" ]; then
+	# Useage of patched font for symbols. true or false.
+	export USE_PATCHED_FONT="true"
+fi

+ 125 - 0
tmux/tmux-powerline-custom/segments/weather.sh

@@ -0,0 +1,125 @@
+#!/bin/bash
+# Prints the current weather in Celsius, Fahrenheits or lord Kelvins. The forecast is cached and updated with a period of $update_period.
+
+# You location. Find a string that works for you by Googling on "weather in <location-string>"
+location='Melbourne, Australia'
+
+# Can be any of {c,f,k}.
+unit="c"
+
+tmp_file="/tmp/tmux-powerline_weather.txt"
+
+get_condition_symbol() {
+	local conditions=$(echo "$1" | tr '[:upper:]' '[:lower:]')
+	case "$conditions" in
+	sunny | "partly sunny" | "mostly sunny")
+		hour=$(date +%H)
+		if [ "$hour" -ge "22" -o "$hour" -le "5" ]; then
+			#echo "☽"
+			echo "☾"
+		else
+			#echo "☀"
+			echo "☼"
+		fi
+		;;
+	"rain and snow" | "chance of rain" | "light rain" | rain | "heavy rain" | "freezing drizzle" | flurries | showers | "scattered showers" | drizzle | "rain showers")
+		#echo "☂"
+		echo "☔"
+		;;
+	snow | "light snow" | "scattered snow showers" | icy | ice/snow | "chance of snow" | "snow showers" | sleet)
+		#echo "☃"
+		echo "❅"
+		;;
+	"partly cloudy" | "mostly cloudy" | cloudy | overcast)
+		echo "☁"
+		;;
+	"chance of storm" | thunderstorm | "chance of tstorm" | storm | "scattered thunderstorms")
+		#echo "⚡"
+		echo "☈"
+		;;
+	dust | fog | smoke | haze | mist)
+		echo "♨"
+		;;
+	windy)
+		echo "⚑"
+		#echo "⚐"
+		;;
+	clear)
+		#echo "☐"
+		echo "✈"	# So clear you can see the aeroplanes! TODO what symbol does best represent a clear sky?
+		;;
+	*)
+		echo "?"
+		;;
+	esac
+}
+
+read_tmp_file() {
+	if [ ! -f "$tmp_file" ]; then
+		return
+	fi
+	IFS_bak="$IFS"
+	IFS=$'\n'
+	lines=($(cat ${tmp_file}))
+	IFS="$IFS_bak"
+	degrees="${lines[0]}"
+	conditions="${lines[1]}"
+}
+
+degrees=""
+if [ -f "$tmp_file" ]; then
+	if [ "$PLATFORM" == "mac" ]; then
+		last_update=$(stat -f "%m" ${tmp_file})
+	else
+		last_update=$(stat -c "%Y" ${tmp_file})
+	fi
+	time_now=$(date +%s)
+	update_period=600
+
+	up_to_date=$(echo "(${time_now}-${last_update}) < ${update_period}" | bc)
+	if [ "$up_to_date" -eq 1 ]; then
+		read_tmp_file
+	fi
+fi
+
+if [ -z "$degrees" ]; then
+	if [ "$unit" == "k" ]; then
+		search_unit="c"
+	else
+		search_unit="$unit"
+	fi
+	# Convert spaces before using this in the URL.
+	if [ "$PLATFORM" == "mac" ]; then
+		search_location=$(echo "$location" | sed -e 's/[ ]/%20/g')
+	else
+		search_location=$(echo "$location" | sed -e 's/\s/%20/g')
+	fi
+
+	weather_data=$(curl --max-time 4 -s "http://www.google.com/ig/api?weather=${search_location}")
+	if [ "$?" -eq "0" ]; then
+		error=$(echo "$weather_data" | grep "problem_cause\|DOCTYPE");
+		if [ -n "$error" ]; then
+			echo "error"
+			exit 1
+		fi
+		degrees=$(echo "$weather_data" | sed "s|.*<temp_${search_unit} data=\"\([^\"]*\)\"/>.*|\1|")
+		if [ "$PLATFORM" == "mac" ]; then
+			conditions=$(echo $weather_data | xpath //current_conditions/condition/@data 2> /dev/null | grep -oe '".*"' | sed "s/\"//g")
+		else
+			conditions=$(echo "$weather_data" | grep -PZo "<current_conditions>(\\n|.)*</current_conditions>" | grep -PZo "(?<=<condition\sdata=\")([^\"]*)")
+		fi
+		echo "$degrees" > $tmp_file
+		echo "$conditions" >> $tmp_file
+	elif [ -f "$tmp_file" ]; then
+		read_tmp_file
+	fi
+fi
+
+if [ -n "$degrees" ]; then
+	if [ "$unit" == "k" ]; then
+		degrees=$(echo "${degrees} + 273.15" | bc)
+	fi
+	unit_upper=$(echo "$unit" | tr '[cfk]' '[CFK]')
+	condition_symbol=$(get_condition_symbol "$conditions")
+	echo "${condition_symbol} ${degrees}°${unit_upper}"
+fi

+ 9 - 11
tmux/tmux-powerline-custom/status-left.sh

@@ -1,16 +1,21 @@
 #!/usr/bin/env bash
 #Print the status-left for tmux.
-#
-# The powerline root directory.
-cwd="/Users/acerlouw/dotfiles/tmux/tmux-powerline"
+
+# custom left/right status and segments
+custom_dir=$(dirname $0)
+
+# The powerline root directory. TODO any other neater way of getting fullpath?
+cd ~/dotfiles/tmux/tmux-powerline
+cwd=$(pwd)
 
 # Source global configurations.
-source "${cwd}/config.sh"
+source "${custom_dir}/config.sh"
 
 # Source lib functions.
 source "${cwd}/lib.sh"
 
 segments_path="${cwd}/${segments_dir}"
+custom_segments_path="${custom_dir}/${segments_dir}"
 
 # Mute this statusbar?
 mute_status_check "left"
@@ -47,13 +52,6 @@ wan_ip+=(["separator"]="${separator_right_thin}")
 wan_ip+=(["separator_fg"]="white")
 register_segment "wan_ip"
 
-declare -A vcs_branch
-vcs_branch+=(["script"]="${segments_path}/vcs_branch.sh")
-vcs_branch+=(["foreground"]="colour88")
-vcs_branch+=(["background"]="colour29")
-vcs_branch+=(["separator"]="${separator_right_bold}")
-register_segment "vcs_branch"
-
 # Print the status line in the order of registration above.
 print_status_line_left
 

+ 10 - 73
tmux/tmux-powerline-custom/status-right.sh

@@ -1,16 +1,21 @@
 #!/usr/bin/env bash
 # This script prints a string will be evaluated for text attributes (but not shell commands) by tmux. It consists of a bunch of segments that are simple shell scripts/programs that output the information to show. For each segment the desired foreground and background color can be specified as well as what separator to use. The script the glues together these segments dynamically so that if one script suddenly does not output anything (= nothing should be shown) the separator colors will be nicely handled.
 
-# The powerline root directory.
-cwd="/Users/acerlouw/dotfiles/tmux/tmux-powerline"
+# custom left/right status and segments
+custom_dir=$(dirname $0)
+
+# The powerline root directory. TODO any other neater way of getting fullpath?
+cd ~/dotfiles/tmux/tmux-powerline
+cwd=$(pwd)
 
 # Source global configurations.
-source "${cwd}/config.sh"
+source "${custom_dir}/config.sh"
 
 # Source lib functions.
 source "${cwd}/lib.sh"
 
 segments_path="${cwd}/${segments_dir}"
+custom_segments_path="${custom_dir}/${segments_dir}"
 
 # Mute this statusbar?
 mute_status_check "right"
@@ -18,80 +23,12 @@ mute_status_check "right"
 # Segment
 # Comment/uncomment the register function call to enable or disable a segment.
 
-declare -A pwd
-pwd+=(["script"]="${segments_path}/pwd.sh")
-pwd+=(["foreground"]="colour211")
-pwd+=(["background"]="colour89")
-pwd+=(["separator"]="${separator_left_bold}")
-#register_segment "pwd"
-
-declare -A mail_count
-mail_count+=(["script"]="${segments_path}/maildir_count.sh")
-#mail_count+=(["script"]="${segments_path}/apple_mail_count.sh")
-mail_count+=(["foreground"]="white")
-mail_count+=(["background"]="red")
-mail_count+=(["separator"]="${separator_left_bold}")
-register_segment "mail_count"
-
-declare -A now_playing
-if [ "$PLATFORM" == "linux" ]; then
-	now_playing+=(["script"]="${segments_path}/np_mpd.sh")
-	#now_playing+=(["script"]="${segments_path}/np_mocp.sh")
-	#now_playing+=(["script"]="${segments_path}/np_spotify_linux_wine.sh")
-	#now_playing+=(["script"]="${segments_path}/np_spotify_linux_native.sh")
-	#now_playing+=(["script"]="${segments_path}/np_rhythmbox.sh")
-	#now_playing+=(["script"]="${segments_path}/np_banshee.sh")
-	#now_playing+=(["script"]="${segments_path}/np_audacious.sh")
-elif [ "$PLATFORM" == "mac" ]; then
-	now_playing+=(["script"]="${segments_path}/np_itunes_mac.sh")
-fi
-if [[ ${now_playing["script"]} ]]; then
-	now_playing+=(["foreground"]="colour37")
-	now_playing+=(["background"]="colour234")
-	now_playing+=(["separator"]="${separator_left_bold}")
-	register_segment "now_playing"
-fi
-
-declare -A cpu
-cpu+=(["script"]="${segments_path}/cpu.sh")
-cpu+=(["foreground"]="colour136")
-cpu+=(["background"]="colour240")
-cpu+=(["separator"]="${separator_left_bold}")
-#register_segment "cpu"
-
-declare -A load
-load+=(["script"]="${segments_path}/load.sh")
-load+=(["foreground"]="colour167")
-load+=(["background"]="colour237")
-load+=(["separator"]="${separator_left_bold}")
-register_segment "load"
-
-declare -A battery
-if [ "$PLATFORM" == "mac" ]; then
-	battery+=(["script"]="${segments_path}/battery_mac.sh")
-else
-	battery+=(["script"]="${segments_path}/battery.sh")
-fi
-battery+=(["foreground"]="colour127")
-battery+=(["background"]="colour137")
-battery+=(["separator"]="${separator_left_bold}")
-#register_segment "battery"
-
 declare -A weather
-weather+=(["script"]="${segments_path}/weather.sh")
+weather+=(["script"]="${custom_segments_path}/weather.sh")
 weather+=(["foreground"]="colour255")
 weather+=(["background"]="colour37")
 weather+=(["separator"]="${separator_left_bold}")
-register_segment "weather"
-
-declare -A xkb_layout
-if [ "$PLATFORM" == "linux" ]; then
-	xkb_layout+=(["script"]="${segments_path}/xkb_layout.sh")
-	xkb_layout+=(["foreground"]="colour117")
-	xkb_layout+=(["background"]="colour125")
-	xkb_layout+=(["separator"]="${separator_left_bold}")
-fi
-#register_segment "xkb_layout"
+#register_segment "weather"
 
 declare -A date_day
 date_day+=(["script"]="${segments_path}/date_day.sh")