Differences in fixed character length? Or is 95 a magic number?

I think I'm having optical illusions...

# |           aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccc - 95                 | #
# |           dddddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffgggggggggggggggggggg - 95       | #

This is the console input:

[~/prjs/SWARM] 130 $ . ./runtime
........[~/prjs/SWARM] 0 $ title aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffgggggggggggggggggggg

Am I doing something wrong here?

# Before we attempt to print anything, lets check about the length
local len_strings_total=$(( $lenEvalLeft + $lenEvalMiddle + $lenEvalRight ))

if [[ ${len_strings_total:-0} -gt $COLUMNS ]]
then	case "$MODE" in
	"basic")
		echo TODO $MODE width
		;;
	"title")
		#header "${#1} -- $1"

		local tmp_half=$(( ${#1} / 2 ))
		local tmp_first="${1:0:$tmp_half}"
		local tmp_second="${1:$tmp_half}"

		title "$tmp_first - ${#tmp_first}"
		title "$tmp_second - ${#tmp_second}"
		return 0
		;;
	"header")
		echo TODO $MODE width
		;;
	esac
fi

My biggest confusion here right now is that I expect both, tmp_first and tmp_second to be 50% of the passed argument, and they seem to be, at least the string length says so, but it does not look like it..
Hence my confusion.

Can anyone shed some light on this please?
Thank you in advance!

Well, I got my output solved, but I still dont understand the output I had above...

Anyhow, I solved it by making an extra function which I call from the case statement as simple as:

swarm.str.split "$1" | title --

While the function looks like:

swarm.str.split() { # "STRING"
# Splittes passed string in half at the nearest word
# and prints each part as a new line
	[[ -z "$1" ]] && return 1
	local tmp_count=$(( $(builtin echo "${1}" | $WC -c) / 2 + 1))
	builtin echo "$1" | fold -sw $tmp_count
}

${#var} is length in bytes like wc -b , while wc -c is number of characters that differs if there are multibyte characters - can occur in UTF locales.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.