diff options
author | Cody Hiar <chiar@hybridforge.com> | 2015-08-12 12:06:23 -0600 |
---|---|---|
committer | Cody Hiar <chiar@hybridforge.com> | 2015-08-12 12:06:23 -0600 |
commit | 07fc2644a237187dd4c5680e88f4adadbf533603 (patch) | |
tree | bb6335e180df48c5a6f0b893312857d36ad470f7 /lib |
Initial commit of the working files
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arg_processing.sh | 9 | ||||
-rw-r--r-- | lib/formatting.sh | 5 | ||||
-rw-r--r-- | lib/muting.sh | 20 | ||||
-rw-r--r-- | lib/powerline.sh | 142 | ||||
-rw-r--r-- | lib/rcfile.sh | 68 | ||||
-rw-r--r-- | lib/text_roll.sh | 63 | ||||
-rw-r--r-- | lib/tmux_adapter.sh | 12 |
7 files changed, 319 insertions, 0 deletions
diff --git a/lib/arg_processing.sh b/lib/arg_processing.sh new file mode 100644 index 0000000..d8fb92c --- /dev/null +++ b/lib/arg_processing.sh @@ -0,0 +1,9 @@ +#! Check script arguments. + +check_arg_side() { + local side="$1" + if ! [ "$side" == "left" -o "$side" == "right" ]; then + echo "Argument must be must be the side to handle {left, right} and not \"${side}\"." + exit 1 + fi +} diff --git a/lib/formatting.sh b/lib/formatting.sh new file mode 100644 index 0000000..d891863 --- /dev/null +++ b/lib/formatting.sh @@ -0,0 +1,5 @@ +__print_colored_content() { + echo -n "#[fg=colour$3, bg=colour$2]" + echo -n "$1" + echo -n "#[default]" +} diff --git a/lib/muting.sh b/lib/muting.sh new file mode 100644 index 0000000..595ba87 --- /dev/null +++ b/lib/muting.sh @@ -0,0 +1,20 @@ +# Muting Logic +# In all cases $1 is the side to be muted (eg left/right). + +powerline_muted() { + [ -e "$(__powerline_mute_file $1)" ]; +} + +toggle_powerline_mute_status() { + if powerline_muted $1; then + rm "$(__powerline_mute_file $1)" + else + touch "$(__powerline_mute_file $1)" + fi +} + +__powerline_mute_file() { + local tmux_session=$(tmux display -p "#S") + + echo -n "${TMUX_POWERLINE_DIR_TEMPORARY}/mute_${tmux_session}_$1" +} diff --git a/lib/powerline.sh b/lib/powerline.sh new file mode 100644 index 0000000..3a3fb68 --- /dev/null +++ b/lib/powerline.sh @@ -0,0 +1,142 @@ +# Library functions + +print_powerline() { + local side="$1" + local upper_side=$(echo "$1" | tr '[:lower:]' '[:upper:]') + eval "local input_segments=(\"\${TMUX_POWERLINE_${upper_side}_STATUS_SEGMENTS[@]}\")" + local powerline_segments=() + local powerline_segment_contents=() + + __check_platform + + __process_segment_defaults + __process_scripts + __process_colors + + __process_powerline +} + +__process_segment_defaults() { + for segment_index in "${!input_segments[@]}"; do + local input_segment=(${input_segments[$segment_index]}) + eval "local default_separator=\$TMUX_POWERLINE_DEFAULT_${upper_side}SIDE_SEPARATOR" + + powerline_segment_with_defaults=( + ${input_segment[0]:-"no_script"} \ + ${input_segment[1]:-$TMUX_POWERLINE_DEFAULT_BACKGROUND_COLOR} \ + ${input_segment[2]:-$TMUX_POWERLINE_DEFAULT_FOREGROUND_COLOR} \ + ${input_segment[3]:-$default_separator} \ + ) + + powerline_segments[$segment_index]="${powerline_segment_with_defaults[@]}" + done +} + +__process_scripts() { + for segment_index in "${!powerline_segments[@]}"; do + local powerline_segment=(${powerline_segments[$segment_index]}) + + if [ -n "$TMUX_POWERLINE_DIR_USER_SEGMENTS" ] && [ -f "$TMUX_POWERLINE_DIR_USER_SEGMENTS/${powerline_segment[0]}.sh" ] ; then + local script="$TMUX_POWERLINE_DIR_USER_SEGMENTS/${powerline_segment[0]}.sh" + else + local script="$TMUX_POWERLINE_DIR_SEGMENTS/${powerline_segment[0]}.sh" + fi + + export TMUX_POWERLINE_CUR_SEGMENT_BG="${powerline_segment[1]}" + export TMUX_POWERLINE_CUR_SEGMENT_FG="${powerline_segment[2]}" + source "$script" + local output + output=$(run_segment) + local exit_code="$?" + unset -f run_segment + + if [ "$exit_code" -ne 0 ] && debug_mode_enabled ; then + local seg_name="${script##*/}" + echo "Segment '${seg_name}' exited with code ${exit_code}. Aborting." + exit 1 + fi + + if [ -n "$output" ]; then + powerline_segment_contents[$segment_index]=" $output " + else + unset -v powerline_segments[$segment_index] + fi + done +} + +__process_colors() { + for segment_index in "${!powerline_segments[@]}"; do + local powerline_segment=(${powerline_segments[$segment_index]}) + # Find the next segment that produces content (i.e. skip empty segments). + for next_segment_index in $(eval echo {$(($segment_index + 1))..${#powerline_segments}}) ; do + [[ -n ${powerline_segments[next_segment_index]} ]] && break + done + local next_segment=(${powerline_segments[$next_segment_index]}) + + if [ $side == 'left' ]; then + powerline_segment[4]=${next_segment[1]:-$TMUX_POWERLINE_DEFAULT_BACKGROUND_COLOR} + elif [ $side == 'right' ]; then + powerline_segment[4]=${previous_background_color:-$TMUX_POWERLINE_DEFAULT_BACKGROUND_COLOR} + fi + + if __segment_separator_is_thin; then + powerline_segment[5]=${powerline_segment[2]} + else + powerline_segment[5]=${powerline_segment[1]} + fi + + local previous_background_color=${powerline_segment[1]} + + powerline_segments[$segment_index]="${powerline_segment[@]}" + done +} + +__process_powerline() { + for segment_index in "${!powerline_segments[@]}"; do + local powerline_segment=(${powerline_segments[$segment_index]}) + + local background_color=${powerline_segment[1]} + local foreground_color=${powerline_segment[2]} + local separator=${powerline_segment[3]} + local separator_background_color=${powerline_segment[4]} + local separator_foreground_color=${powerline_segment[5]} + + eval "__print_${side}_segment ${segment_index} ${background_color} ${foreground_color} ${separator} ${separator_background_color} ${separator_foreground_color}" + done +} + +__print_left_segment() { + local content=${powerline_segment_contents[$1]} + local content_background_color=$2 + local content_foreground_color=$3 + local separator=$4 + local separator_background_color=$5 + local separator_foreground_color=$6 + + __print_colored_content "$content" $content_background_color $content_foreground_color + __print_colored_content $separator $separator_background_color $separator_foreground_color +} + +__print_right_segment() { + local content=${powerline_segment_contents[$1]} + local content_background_color=$2 + local content_foreground_color=$3 + local separator=$4 + local separator_background_color=$5 + local separator_foreground_color=$6 + + __print_colored_content $separator $separator_background_color $separator_foreground_color + __print_colored_content "$content" $content_background_color $content_foreground_color +} + +__segment_separator_is_thin() { + [[ ${powerline_segment[3]} == $TMUX_POWERLINE_SEPARATOR_LEFT_THIN || \ + ${powerline_segment[3]} == $TMUX_POWERLINE_SEPARATOR_RIGHT_THIN ]]; +} + +__check_platform() { + if [ "$SHELL_PLATFORM" == "unknown" ] && debug_mode_enabled; then + echo "Unknown platform; modify config/shell.sh" &1>&2 + fi +} + diff --git a/lib/rcfile.sh b/lib/rcfile.sh new file mode 100644 index 0000000..db88198 --- /dev/null +++ b/lib/rcfile.sh @@ -0,0 +1,68 @@ +# Read user rc file. + +process_settings() { + __read_rcfile + + if [ -z "$TMUX_POWERLINE_DEBUG_MODE_ENABLED" ]; then + export TMUX_POWERLINE_DEBUG_MODE_ENABLED="${TMUX_POWERLINE_DEBUG_MODE_ENABLED_DEFAULT}" + fi + + if [ -z "$TMUX_POWERLINE_PATCHED_FONT_IN_USE" ]; then + export TMUX_POWERLINE_PATCHED_FONT_IN_USE="${TMUX_POWERLINE_PATCHED_FONT_IN_USE_DEFAULT}" + fi + + if [ -z "$TMUX_POWERLINE_THEME" ]; then + export TMUX_POWERLINE_THEME="${TMUX_POWERLINE_THEME_DEFAULT}" + fi + + eval TMUX_POWERLINE_DIR_USER_SEGMENTS="$TMUX_POWERLINE_DIR_USER_SEGMENTS" + eval TMUX_POWERLINE_DIR_USER_THEMES="$TMUX_POWERLINE_DIR_USER_THEMES" + if [ -n "$TMUX_POWERLINE_DIR_USER_THEMES" ] && [ -f "${TMUX_POWERLINE_DIR_USER_THEMES}/${TMUX_POWERLINE_THEME}.sh" ]; then + source "${TMUX_POWERLINE_DIR_USER_THEMES}/${TMUX_POWERLINE_THEME}.sh" + else + source "${TMUX_POWERLINE_DIR_THEMES}/${TMUX_POWERLINE_THEME}.sh" + fi + +} + +generate_default_rc() { + read -d '' rccontents << EORC +# Default configuration file for tmux-powerline. +# Modeline { +# vi: foldmarker={,} foldmethod=marker foldlevel=0 tabstop=4 filetype=sh +# } + +# General { + # Show which segment fails and its exit code. + export TMUX_POWERLINE_DEBUG_MODE_ENABLED="${TMUX_POWERLINE_DEBUG_MODE_ENABLED_DEFAULT}" + # Use patched font symbols. + export TMUX_POWERLINE_PATCHED_FONT_IN_USE="${TMUX_POWERLINE_PATCHED_FONT_IN_USE_DEFAULT}" + # The theme to use. + export TMUX_POWERLINE_THEME="${TMUX_POWERLINE_THEME_DEFAULT}" + # Overlay dirctory to look for themes. There you can put your own themes outside the repo. Fallback will still be the "themes" directory in the repo. + export TMUX_POWERLINE_DIR_USER_THEMES="" + # Overlay dirctory to look for segments. There you can put your own segments outside the repo. Fallback will still be the "segments" directory in the repo. + export TMUX_POWERLINE_DIR_USER_SEGMENTS="" +# } +EORC + + for segment in ${TMUX_POWERLINE_DIR_SEGMENTS}/*.sh; do + source "$segment" + if declare -f generate_segmentrc >/dev/null; then + segmentrc=$(generate_segmentrc | sed -e 's/^/\\t/g') + unset -f generate_segmentrc + local seg_name="${segment##*/}" + rccontents="${rccontents}\n\n# ${seg_name} {\n${segmentrc}\n# }" + fi + done + + echo -e "$rccontents" > "$TMUX_POWERLINE_RCFILE_DEFAULT" + echo "Default configuration file generated to: ${TMUX_POWERLINE_RCFILE_DEFAULT}" + echo "Copy/move it to \"${TMUX_POWERLINE_RCFILE}\" and make your changes." +} + +__read_rcfile() { + if [ -f "$TMUX_POWERLINE_RCFILE" ]; then + source "$TMUX_POWERLINE_RCFILE" + fi +} diff --git a/lib/text_roll.sh b/lib/text_roll.sh new file mode 100644 index 0000000..e53247f --- /dev/null +++ b/lib/text_roll.sh @@ -0,0 +1,63 @@ +# Rolling anything what you want. +# arg1: text to roll. +# arg2: max length to display. +# arg3: roll speed in characters per second. +roll_text() { + local text="$1" # Text to print + + if [ -z "$text" ]; then + return; + fi + + local max_len="10" # Default max length. + + if [ -n "$2" ]; then + max_len="$2" + fi + + local speed="1" # Default roll speed in chars per second. + + if [ -n "$3" ]; then + speed="$3" + fi + + # Skip rolling if the output is less than max_len. + if [ "${#text}" -le "$max_len" ]; then + echo "$text" + return + fi + + # Anything starting with 0 is an Octal number in Shell,C or Perl, + # so we must explicitly state the base of a number using base#number + local offset=$((10#$(date +%s) * ${speed} % ${#text})) + + # Truncate text. + text=${text:offset} + + local char # Character. + local bytes # The bytes of one character. + local index + + for ((index=0; index < max_len; index++)); do + char=${text:index:1} + bytes=$(echo -n $char | wc -c) + # The character will takes twice space + # of an alphabet if (bytes > 1). + if ((bytes > 1)); then + max_len=$((max_len - 1)) + fi + done + + text=${text:0:max_len} + + #echo "index=${index} max=${max_len} len=${#text}" + # How many spaces we need to fill to keep + # the length of text that will be shown? + local fill_count=$((${index} - ${#text})) + + for ((index=0; index < fill_count; index++)); do + text="${text} " + done + + echo "${text}" +} diff --git a/lib/tmux_adapter.sh b/lib/tmux_adapter.sh new file mode 100644 index 0000000..17674e4 --- /dev/null +++ b/lib/tmux_adapter.sh @@ -0,0 +1,12 @@ +# Get the current path in the segment. +get_tmux_cwd() { + local env_name=$(tmux display -p "TMUXPWD_#D" | tr -d %) + local env_val=$(tmux show-environment | grep --color=never "$env_name") + # The version below is still quite new for tmux. Uncomment this in the future :-) + #local env_val=$(tmux show-environment "$env_name" 2>&1) + + if [[ ! $env_val =~ "unknown variable" ]]; then + local tmux_pwd=$(echo "$env_val" | sed 's/^.*=//') + echo "$tmux_pwd" + fi +} |