Skip to content

Commit

Permalink
Improve roll_text from text_roll lib (#391)
Browse files Browse the repository at this point in the history
* add new args 4 & 5, "fill_mode" and "repeat_sep"
* construct new repeat variable
* use length of repeat variable for offset calculation
  in case of fill_mode=repeat
* use repeat variable to fill free space
* remove byte calculation section, as real used "space"
  depends on font and leads to incorrect behaviour on
  some characters (e.g. french accent chars)
* smaller cleanup to comply with shellcheck
* use new mode as default in now_playing segment (but
  we don't change the default of the trim method)

fix #390
  • Loading branch information
xx4h authored Apr 6, 2024
1 parent 0145501 commit d52005a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
59 changes: 37 additions & 22 deletions lib/text_roll.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# shellcheck shell=bash
# Rolling anything what you want.
# arg1: text to roll.
# arg2: max length to display.
# arg3: roll speed in characters per second.
# arg4: mode to fill {"space", "repeat"}
# arg5: repeat separator
roll_text() {
local text="$1" # Text to print

Expand All @@ -21,6 +24,26 @@ roll_text() {
speed="$3"
fi

local fill_mode="space" # Default fill mode

if [ -n "$4" ]; then
if [ "$4" = "repeat" ]; then
fill_mode="repeat"
elif [ "$4" = "space" ]; then
fill_mode="space"
else
echo "Not a valid fill_mode: {\"space\", \"repeat\"}: $4" >&2
fi
fi

local repeat_sep=" ** " # Default repeat separator

if [ -n "$5" ]; then
repeat_sep="$5"
fi

local repeat="${repeat_sep}${text}"

# Skip rolling if the output is less than max_len.
if [ "${#text}" -le "$max_len" ]; then
echo "$text"
Expand All @@ -29,34 +52,26 @@ roll_text() {

# 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.
if [ "$fill_mode" = "repeat" ]; then
local offset=$((10#$(date +%s) * speed % ${#repeat}))
elif [ "$fill_mode" = "space" ] || :; then
local offset=$((10#$(date +%s) * speed % ${#text}))
fi
# Truncate text on time-based offset
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

# Ensure text is not longer than max_len
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}))
# Get fill count by substracting length of current text from max_len
local fill_count=$((max_len - ${#text}))

for ((index=0; index < fill_count; index++)); do
text="${text} "
if [ "$fill_mode" = "repeat" ]; then
text="${text}${repeat:index:1}"
elif [ "$fill_mode" = "space" ] || :; then
text="${text} "
fi
done

echo "${text}"
Expand Down
10 changes: 8 additions & 2 deletions segments/now_playing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source "${TMUX_POWERLINE_DIR_LIB}/text_roll.sh"
TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN_DEFAULT="40"
TMUX_POWERLINE_SEG_NOW_PLAYING_TRIM_METHOD_DEFAULT="trim"
TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED_DEFAULT="2"
TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE:-repeat}"
TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR:-  }"
TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_HOST_DEFAULT="localhost"
TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_PORT_DEFAULT="6600"
TMUX_POWERLINE_SEG_NOW_PLAYING_LASTFM_UPDATE_PERIOD_DEFAULT="30"
Expand All @@ -25,8 +27,12 @@ export TMUX_POWERLINE_SEG_NOW_PLAYING_FILE_NAME=""
export TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN="${TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN_DEFAULT}"
# How to handle too long strings. Can be {trim, roll}.
export TMUX_POWERLINE_SEG_NOW_PLAYING_TRIM_METHOD="${TMUX_POWERLINE_SEG_NOW_PLAYING_TRIM_METHOD_DEFAULT}"
# Charcters per second to roll if rolling trim method is used.
# Characters per second to roll if rolling trim method is used.
export TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED_DEFAULT}"
# Mode of roll text {"space", "repeat"}. space: fill up with empty space; repeat: repeat text from beginning
# export TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE}"
# Separator for "repeat" roll mode
# export TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR="${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR}"
# Hostname for MPD server in the format "[password@]host"
export TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_HOST="${TMUX_POWERLINE_SEG_NOW_PLAYING_MPD_HOST_DEFAULT}"
Expand Down Expand Up @@ -113,7 +119,7 @@ run_segment() {
if [ -n "$np" ]; then
case "$TMUX_POWERLINE_SEG_NOW_PLAYING_TRIM_METHOD" in
"roll")
np=$(roll_text "${np}" ${TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN} ${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED_DEFAULT})
np=$(roll_text "${np}" ${TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN} ${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SPEED_DEFAULT} ${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_MODE} "${TMUX_POWERLINE_SEG_NOW_PLAYING_ROLL_SEPARATOR}")
;;
"trim")
np=${np:0:TMUX_POWERLINE_SEG_NOW_PLAYING_MAX_LEN}
Expand Down

0 comments on commit d52005a

Please sign in to comment.