~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
2 2

[PATCH sxmo-utils v3] sxmo_hook_tailtextlog.sh: right align and colors

Details
Message ID
<20230516181558.24760-1-projects@jeffkalsow.com>
DKIM signature
missing
Download raw message
Patch: +208 -26
v1
Working from Aren's Pinephone 1.2 fix, I used awk to implement
right alignment, more colors, and easy to modify
time/date format.

v2
Added 3 modes: right justify, indent, or all left justify.
Added 2 wrap modes: Words or ignore words
Easily adjustable line width and indent length, both percentage
of term cols.
In group messaging, contacts are colored uniquely for better
readability.

Unlikely this will be added as it's not the fastest tailtextlog, but I'm
very much enjoying it, perhaps others may find some use of it.

v3
Cleaned up shellcheck warnings/errors
Cleaned up trailing spaces
Threw in a tail line num variable
---
 .../default_hooks/sxmo_hook_tailtextlog.sh    | 234 ++++++++++++++++--
 1 file changed, 208 insertions(+), 26 deletions(-)

diff --git a/configs/default_hooks/sxmo_hook_tailtextlog.sh b/configs/default_hooks/sxmo_hook_tailtextlog.sh
index 51c2109..39ba66c 100755
--- a/configs/default_hooks/sxmo_hook_tailtextlog.sh
+++ b/configs/default_hooks/sxmo_hook_tailtextlog.sh
@@ -1,4 +1,5 @@
#!/bin/sh
# configversion: 9978740a2faa28d66e2013a837fcf11a
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

@@ -8,42 +9,223 @@
# 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

# If it's already open, switch to it.
if [ "$SXMO_WM" = "sway" ] && [ -z "$SSH_CLIENT" ]; then
	regesc_termname="$(echo "$TERMNAME" | sed 's|+|\\+|g')"
	swaymsg "[title=\"^$regesc_termname\$\"]" focus && exit 0
fi

mkcontactssedcmd() {
mkcontactscmd() {
	clr=1
	pnc find "$LOGDIRNUM" | while read -r NUMBER; do
		CONTACT="$(sxmo_contacts.sh --name-or-number "$NUMBER")"
		if [ ! "$CONTACT" = "$NUMBER" ]; then
			printf %s "s/$NUMBER/$CONTACT/;"
		fi
		clr=$((clr=clr+1))
		printf "%s::%s::%s:::" "$NUMBER" "$CONTACT" "$(tput setaf $clr)"
	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 1

	#######################################
	# Adjust the following to your liking #
	#######################################

	# 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
	MODE="right_justify"

	# WORDWRAP determines if lines are to
	# break with words or ignore words (breaks
	# precisely at determined width)
	# 2 options: 'words' or 'ignorewords'
	WORDWRAP="words"

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

	# To modify date/time, see man
	# strftime for list of fields
	DAY_F="%Y %m %d"
	TIME_F="%H:%M"

	# Colorize Sent and Received lines
	RECEIVED_COLOR="6"
	RECEIVED_HEAD_COLOR="242"
	SENT_COLOR="4"
	SENT_HEAD_COLOR="242"

# TODO
#ALIGNSEDCMD="s/^Sent/<right align>/;s/^Received/<left align>/;"
	# Number of sms.txt lines to process
	TAIL_NUM=9999

# 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)#;"

# Replace phone numbers in the filename with contacts from contact book
CONTACTSSEDCMD="$(mkcontactssedcmd)"
	####################################
	# Do Not modify anything hereafter #
	####################################

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 "sxmo_hook_parselog.sh \"$NUMBER\""
	TODAY="$(date "+${DAY_F}")"
	YESTERDAY="$(date -d "- 1 day" "+${DAY_F}")"
	TWO_DAYS="$(date -d "- 2 day" "+${DAY_F}")"
	TERM_WIDTH=$(tput cols)

	tail -n$TAIL_NUM -f "$SXMO_LOGDIR/$LOGDIRNUM/sms.txt" |
	awk \
	-v mode="$MODE" \
	-v indent_factor=$INDENT_FACTOR \
	-v line_width_factor=$LINE_WIDTH_FACTOR \
	-v wordwrap=wrap_"$WORDWRAP" \
	-v contactscmd="$(mkcontactscmd)" \
	-v day_f="$DAY_F" \
	-v time_f="$TIME_F" \
	-v today="$TODAY" \
	-v yesterday="$YESTERDAY" \
	-v two_days="$TWO_DAYS" \
	-v term_width="$TERM_WIDTH" \
	'
	function wrap_words(nextword_len, buf_len, z) {
		while ($0) {
			nextword_len = match($0, /[[:blank:]]|$/)
			buf_len += nextword_len
			if (buf_len > line_width) {
				if (z) line_filters(z)
				# If a word is larger than line_width
				# break word apart and print lines
				if (nextword_len > line_width){
					while (buf_len > line_width){
						line_filters(substr($0, 1, line_width))
						$0 = substr($0, line_width + 1)
						buf_len -= line_width
					}
					z = $0
					$0 = ""
				}else{
					buf_len = nextword_len - 1
					z = ""
				}
			}else if (z) z = z FS
			z = z substr($0, 1, nextword_len - 1)
			$0 = substr($0, nextword_len + 1)
		}
		line_filters(z)
	}
	function wrap_ignorewords(){
		while ( length >= line_width ) {
			line_filters(substr($0,1,line_width))
			$0=substr($0,line_width+1)
		}
		line_filters($0)
	}
	function line_filters(str){
		if (ishead){
			# Format contacts colors
			# if applies (group messaging)
			for (i=1; i < length(c); i++){
				gsub(c_value[i], c_color[i] c_value[i] tput_op textclr, str)
			}
		}
		# Print the line
		@mode(str)
	}
	function all_left(str){
		indent(str)
	}
	function right_justify(str){
		printf "%s%*s%s%s\n", \
			textclr, \
			(issent) ? line_indent[mode] : 0, \
			str, "", tput_op
	}
	function indent(str){
		printf "%s%*s%s%s\n", \
			textclr, \
			(issent) ? line_indent[mode] : 0, \
			"", str, tput_op
	}
	BEGIN {
		sent_head_color="'"$(tput setaf "$SENT_HEAD_COLOR")"'"
		sent_color="'"$(tput setaf "$SENT_COLOR")"'"
		received_head_color="'"$(tput setaf "$RECEIVED_HEAD_COLOR")"'"
		received_color="'"$(tput setaf "$RECEIVED_COLOR")"'"
		line_width=term_width * line_width_factor
		tput_op="'"$(tput op)"'"
		line_indent["right_justify"] = term_width
		line_indent["indent"] = term_width * indent_factor
		line_indent["all_left"]=0

		# Break apart contacts serial variable
		# into array for once now instead
		# of redunctantly later
		split(contactscmd,c,":::")
		c_len=length(c)
		for (i=1; i < c_len; i++){
			split(c[i],x,"::")
			c_index[i]=x[1]
			c_value[i]=x[2]
			c_color[i]=x[3]
		}
	}
	{
		# Sent / Received variables
		ishead=0
		if(/^.*Sent/){
			issent=1; ishead=1
			textclr=sent_head_color
		}else if(/^.*Received/){
			issent=0; ishead=1
			textclr=received_head_color
		}else if(issent){
			textclr=sent_color
		}else{
			textclr=received_color
		}
		# Modify send/receive line
		if(ishead){

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

			# Format contacts if applies (group messaging)
			# Remove "Received from" if group msg
			if (c_len > 1){
				for (i=1; i < c_len; i++){
					gsub(c_index[i],c_value[i])
				}
				gsub(/^Received from /,"")
			}

			# Format time
			v=substr($0,match($0,/[0-9]{4} [0-9]{1,2} .*[0-9]{1,2}/))
			t=mktime(v)
			gsub(/[0-9]{4} [0-9]{1,2} .*[0-9]{1,2}/,strftime(day_f " " time_f, t))
			gsub(today,"today")
			gsub(yesterday,"yesterday")
			gsub(two_days,"two days ago")
		}
		# Send line through the wrap function
		@wordwrap()
	}'
}

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

# If it's already open, switch to it.
if [ "$SXMO_WM" = "sway" ] && [ -z "$SSH_CLIENT" ]; then
	regesc_termname="$(echo "$TERMNAME" | sed 's|+|\\+|g')"
	swaymsg "[title=\"^$regesc_termname\$\"]" focus && exit 0
fi
sxmo_terminal.sh sh -c "$0 format $*"
-- 
2.40.1

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

builds.sr.ht <builds@sr.ht>
Details
Message ID
<CSNWR3Y7IKAD.1YA2SXVUYMISQ@cirno2>
In-Reply-To
<20230516181558.24760-1-projects@jeffkalsow.com> (view parent)
DKIM signature
missing
Download raw message
sxmo-utils/patches/test.yml: SUCCESS in 1m39s

[sxmo_hook_tailtextlog.sh: right align and colors][0] v3 from [gehn][1]

[0]: https://lists.sr.ht/~mil/sxmo-devel/patches/41191
[1]: projects@jeffkalsow.com

✓ #990915 SUCCESS sxmo-utils/patches/test.yml https://builds.sr.ht/~mil/job/990915
Details
Message ID
<CT4OUCVVLBF7.3QDZ28HAVPOJU@yellow-orcess>
In-Reply-To
<20230516181558.24760-1-projects@jeffkalsow.com> (view parent)
DKIM signature
missing
Download raw message
Marked as superseded !
Reply to thread Export thread (mbox)