~mil/sxmo-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 3

[PATCH sxmo-utils v5] sxmo_hook_tailtextlog.sh with colors/formatting

Details
Message ID
<20241201055036.5636-1-projects@watertao.xyz>
DKIM signature
pass
Download raw message
Patch: +241 -21
A major refactoring of my previous sxmo_hook_tailtextlog.sh. Performs
roughly twice as fast as its predecessor. Added environment variables
to the mix for easy customization.
---

Still slower than the default tailtextlog, but this one affords more
formatting.

This patch supersedes the previous version, which was v4 I believe, from
quite some time ago. I share this only for folks who may find this type
of thing useful. I've been running the older version of this on the
pinephone 1.2 1+ year now, with the default settings, and works well for
my texting needs.

 .../default_hooks/sxmo_hook_tailtextlog.sh    | 262 ++++++++++++++++--
 1 file changed, 241 insertions(+), 21 deletions(-)

diff --git a/configs/default_hooks/sxmo_hook_tailtextlog.sh b/configs/default_hooks/sxmo_hook_tailtextlog.sh
index 1e030b7..a22469f 100755
--- a/configs/default_hooks/sxmo_hook_tailtextlog.sh
+++ b/configs/default_hooks/sxmo_hook_tailtextlog.sh
@@ -2,12 +2,34 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

# This hook displays the sms log for a numbers passed as $1
# This hook displays the sms log for numbers passed as $1

# Environment variables (for values, see format function comments)

# General settings:
# SXMO_TXTLOG_MODE
# SXMO_TXTLOG_WRAP
# SXMO_TXTLOG_INDENT
# SXMO_TXTLOG_LINE_WIDTH
# SXMO_TXTLOG_DAY
# SXMO_TXTLOG_TIME
# SXMO_TXTLOG_LINES

# Colors:
# SXMO_TXTLOG_RECEIVED
# SXMO_TXTLOG_RECEIVED_HEAD
# SXMO_TXTLOG_SENT
# SXMO_TXTLOG_SENT_HEAD

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh

if [ "$1" = format ]; then
	format=true
	shift
fi

LOGDIRNUM="$1"
TERMNAME="$LOGDIRNUM SMS"
export TERMNAME
@@ -20,31 +42,229 @@ if [ "$SXMO_WM" = "sway" ] && [ -z "$SSH_CLIENT" ]; then
	fi
fi

mkcontactssedcmd() {
	pnc find "$LOGDIRNUM" | while read -r NUMBER; do
mkcontactscmd() {
	i=1; j=1
	pnc find "$LOGDIRNUM" | while IFS= read -r NUMBER; do
		while
			clr=$(echo "$C_C" | cut -d " " -f $i)
			i=$((i=i+1))
			[ -z "$clr" ]
		do i=1; done
		CONTACT="$(sxmo_contacts.sh --name-or-number "$NUMBER")"
		if [ ! "$CONTACT" = "$NUMBER" ]; then
			printf %s "s/$NUMBER/$CONTACT/;"
		fi
		[ $j -gt 1 ] && printf ":::"
		printf "%s::%s::%s" "$NUMBER" "$CONTACT" "$(tput setaf "$clr")"
		j=$((j=j+1))
	done
}

# Replace certain dates with human readable versions
TODAY="$(date +%F)"
YESTERDAY="$(date -d "- 1 day" +%F)"
TWO_DAYS="$(date -d "- 2 day" +%F)"
DATESEDCMD="s/ at ${TODAY}T/today at /; s/ at ${YESTERDAY}T/yesterday at /; s/ at ${TWO_DAYS}T/two days ago at /; s/[-+][0-9][0-9][0-9][0-9]://; s/\(-[0-9][0-9]\)T\([0-9][0-9]\)/\1 \2/;"
format() {
	sleep 0.5

	#######################################
	# Adjust the following to your liking #
	# here or using environment variables #
	#######################################

	# 3 Mode options:
	# "right_justify":
	#   Sent messages right justified
	# "indent"
	#   Sent msgs are indented by factor
	#   of terminal width.
	# "all_left"
	#   All msgs left justified

	M="${SXMO_TXTLOG_MODE:-"indent"}"

	# WORDWRAP 2 options: 'word' or 'ignoreword'

	W="${SXMO_TXTLOG_WRAP:-"word"}"

	# Be sure the following is true:
	# IDENT_FACTOR + LINE_WIDTH_FACTOR <= 1

	I="${SXMO_TXTLOG_INDENT:-"0.2"}"
	L="${SXMO_TXTLOG_LINE_WIDTH:-"0.8"}"

	# To modify date/time, see man
	# strftime for format fields

	D="${SXMO_TXTLOG_DAY:-"%Y-%m-%d"}"
	T="${SXMO_TXTLOG_TIME:-"%H:%M"}"

	# Colorize Sent and Received lines

	R_C="${SXMO_TXTLOG_RECEIVED:-6}"
	R_H_C="${SXMO_TXTLOG_RECEIVED_HEAD:-242}"
	S_C="${SXMO_TXTLOG_SENT:-4}"
	S_H_C="${SXMO_TXTLOG_SENT_HEAD:-242}"

	# Number of sms.txt lines to process

	N="${SXMO_TXTLOG_LINES:-1999}"

	# In group sms, contacts are cyclicly colored
	# These are their colors (space delim)

	C_C="${SXMO_TXTLOG_CONTACT_COLORS:-"39 40 201 51 118 214"}"

	####################################
	# Do Not modify anything hereafter #
	####################################

# TODO
#ALIGNSEDCMD="s/^Sent/<right align>/;s/^Received/<left align>/;"
	tail -n $N -f "$SXMO_LOGDIR/$LOGDIRNUM/sms.txt" |
	awk \
	-v m=$M \
	-v indent_factor=$I \
	-v line_width_factor=$L \
	-v w="w_$W" \
	-v contactscmd="$(mkcontactscmd)" \
	-v date_time_f="$D $T" \
	-v today="$(date "+${D}")" \
	-v yesterday="$(date -d "- 1 day" "+${D}")" \
	-v two_days="$(date -d "- 2 day" "+${D}")" \
	-v term_width="$(tput cols)" \
	-v shc="$(tput setaf "$S_H_C")" \
	-v sc="$(tput setaf "$S_C")" \
	-v rhc="$(tput setaf "$R_H_C")" \
	-v rc="$(tput setaf "$R_C")" \
	-v tput_op="$(tput op)" '

# Colorize Sent and Received lines
RECEIVED_COLOR="2"
SENT_COLOR="3"
COLORSEDCMD="s#^\(Sent.*\)#$(tput setaf "$SENT_COLOR")\1$(tput op)#;s#^\(Received.*\)#$(tput setaf "$RECEIVED_COLOR")\1$(tput op)#;"
	function w_word(){
		z = 0
		nff = 1
		o = ""
		for ( i=1; i<=NF; i++){
			z += length($i)
			if ( z >= lw_e ){

# Replace phone numbers in the filename with contacts from contact book
CONTACTSSEDCMD="$(mkcontactssedcmd)"
				# If first word bigger than lw (eg url),
				# skip the extra line pr and just pr
				# the word. Otherwise, normal behavior
				# occurs here.

				if (nff != 1){
					printf( pf, tc, s_indent, o, tput_op )
					nff = 1; o = ""
					z = length($i)
				}
			}else{
				z += fsl
			}
			if (nff == 1){
				o = $i
			}else{
				o = sprintf( "%s%s%s", o, OFS, $i )
			}
			nff++
		}
		printf( pf, tc, s_indent, o, tput_op )
	}

	function w_ignoreword(){
		while( length > lw_e ){
			sub( /^[[:space:]]/, "" )
			printf( pf, tc, s_indent, substr( $0, 1, lw_e ), tput_op )
			$0 = substr( $0, lw_e + 1 )
		}
		sub( /^[[:space:]]/, "" )
		printf( pf, tc, s_indent, $0, tput_op )
	}

	BEGIN {
		m = substr( m, 1, 1 )
		isr = m == "r"
		isa = m == "a"
		lw = term_width * line_width_factor
		hd = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}-[0-9]{4}:$"
		sent_hd = sprintf("%s %s", "^Sent at", hd)
		recv_hd = sprintf("%s %s", "^Received( from [0-9+]+)? at", hd)

		if ( isr ){
			li = term_width
			pf = "%s%*s%s\n"
		}else if ( m == "i" ){
			li = term_width * indent_factor
			pf = "%s%s%s%s\n"
		}else{
			li = 0
			pf = "%s%s%s%s\n"
		}

		# Indent string used for modes indent or all left
		# If right_justify, keep int value

		istr = isr ? li : sprintf( "%*s", li, "" )

		fsl = length(OFS)

		# Group msgs - serialed contact colors

		split(contactscmd, c, ":::")
		c_len = length(c)
		for (ii = 1; ii <= c_len; ii++){
			split(c[ii],x,"::")
			c_index[ii] = x[1]
			c_value[ii] = x[2]
			c_color[ii] = x[3] x[2] tput_op rhc
			c_length[ii] = length( x[3] ) + length( tput_op ) + length( rhc )
		}
	}
	{
		if (NF == 0) { printf "\n"; next }
		ishead = 0
		esc_l = 0

		if( $0 ~ sent_hd ){
			issent = 1; ishead = 1
			tc = shc
		}else if( $0 ~ recv_hd ){
			issent = 0; ishead = 1
			tc = rhc
		}else if( issent ){
			tc = sc
		}else{
			tc = rc
		}

		# For double space between msgs, if you so choose
		# if ( ishead && NR > 1 ) { printf "\n" }

		s_indent = issent || isa ? istr : ""

		if( ishead ){

			# Remove some bits
			sub( /(-[0-9]{4}:)|(at )/, "" )
			gsub( /[-T:]/, " " )

			# Format time
			v = substr( $0, match( $0, /[0-9]{4} [0-9]{1,2} .*[0-9]{1,2}/ ))
			t = mktime(v)
			sub( /[0-9]{4} [0-9]{1,2} .*[0-9]{1,2}[[:space:]]*/, strftime(date_time_f, t ))
			sub( today, "today" )
			sub( yesterday, "yesterday" )
			sub( two_days, "two days ago" )

			# Format contacts if applies (group messaging)
			# Remove "Received from" if group msg

			if ( ! issent && c_len > 1 ){
				for (ii = 1; ii <= c_len; ii++){
					sub( c_index[ii], c_color[ii], $0 )
					esc_l += c_length[ii]
					sub( /^Received from /, "" )
				}
			}
		}
		lw_e = lw + esc_l
		@w()
	}'
}

if [ -n "$format" ]; then
	format
	exit 0
fi

sxmo_terminal.sh sh -c "tail -n9999 -f \"$SXMO_LOGDIR/$LOGDIRNUM/sms.txt\" |\
	sed -e \"$CONTACTSSEDCMD\" -e \"$DATESEDCMD\" -e \"$COLORSEDCMD\""
sxmo_terminal.sh sh -c "$0 format $*"
-- 
2.47.1

[sxmo-utils/patches/test.yml] build failed

builds.sr.ht <builds@sr.ht>
Details
Message ID
<D604PJ4EW63J.WZB7JRJWI9M@fra01>
In-Reply-To
<20241201055036.5636-1-projects@watertao.xyz> (view parent)
DKIM signature
missing
Download raw message
sxmo-utils/patches/test.yml: FAILED in 26s

[sxmo_hook_tailtextlog.sh with colors/formatting][0] v5 from [gehn][1]

[0]: https://lists.sr.ht/~mil/sxmo-devel/patches/56194
[1]: projects@watertao.xyz

✗ #1378743 FAILED sxmo-utils/patches/test.yml https://builds.sr.ht/~mil/job/1378743
Details
Message ID
<D619HOC9UB77.1KG3KCP64Y7IJ@anaproy.nl>
In-Reply-To
<20241201055036.5636-1-projects@watertao.xyz> (view parent)
DKIM signature
pass
Download raw message
Thanks! This is quite comprehensive! We'll need to take a closer look
and decide whether this makes for a good default.

Marking this v5 as superseded since you sent a better v6 already.

-- 

Maarten van Gompel (proycon)

web: https://proycon.anaproy.nl
gpg: 0x39FE11201A31555C
Details
Message ID
<Z04q9dU-LwMfbxHY@watertao.xyz>
In-Reply-To
<D619HOC9UB77.1KG3KCP64Y7IJ@anaproy.nl> (view parent)
DKIM signature
pass
Download raw message
> Thanks! This is quite comprehensive! We'll need to take a closer look
> and decide whether this makes for a good default.

Sounds good. Would love to hear how this breaks in other user
environments. Has been tested only in english on the pinephone.

And as for a good default, I'm working on the current default's
'TODO' sed expression for not necessarily right alignment, but rather
indented sent messages. I know it's possible using sed branching and
conditionals. If that works out, it may be a better, faster default
for some folks than this more robusto awk program.

> 
> Marking this v5 as superseded since you sent a better v6 already.
> 
> -- 
> 
> Maarten van Gompel (proycon)
> 
> web: https://proycon.anaproy.nl
> gpg: 0x39FE11201A31555C
Reply to thread Export thread (mbox)