~mil/sxmo-devel

sxmo_hook_tailtextlog.sh: right align and colors v1 APPLIED

gehn: 1
 sxmo_hook_tailtextlog.sh: right align and colors

 1 files changed, 85 insertions(+), 26 deletions(-)



          
          
          
          
Next



          
          
          
          
Next
I like this idea a lot. Will work on this.
Next
A couple further observations:

1. It is pretty slow on big sms.txt files.  Might look to see if you can 
optimize.

2. A couple lines in the patch have extra tabs.  Please remove those :)

3. If Group Chat it only moves me to the right.  I'd have to check what 
other UIs do with group chats, but I'd expect it alternates right / left 
every other participant?

4. One idea for the alignment problem discussed above: Can we assume 
we'll always have 80 characters?  Or can we check that?  If < 80 chars
then revert back to a "basic" view without alignment.  If 80 chars, then 
split at 40 chars: sender align left breaking at 40chars and then 
receive align left starting at 40 chars and breaking at 80chars.  Maybe?

So:

Hi this is an important message.

				 I see.


--
sic dicit magister P
https://phartman.sites.luc.edu/
GPG keyID 0xE0DBD3D6 (CAE6 3A6F 755F 7BC3 36CA  330D B3E6 39C6 E0DB D3D6)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~mil/sxmo-devel/patches/40827/mbox | git am -3
Learn more about email & git

[PATCH] sxmo_hook_tailtextlog.sh: right align and colors Export this patch

Working from Aren's Pinephone 1.2 fix, I used awk to implement
right alignment(ish), more colors, and easy to modify
time/date format.
---
 .../default_hooks/sxmo_hook_tailtextlog.sh    | 111 ++++++++++++++----
 1 file changed, 85 insertions(+), 26 deletions(-)

diff --git a/configs/default_hooks/sxmo_hook_tailtextlog.sh b/configs/default_hooks/sxmo_hook_tailtextlog.sh
index 51c2109..436035b 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,100 @@
# 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() {
	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
		printf "%s" "$NUMBER::$CONTACT:::"
	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
	# Replace certain dates with human readable versions
	DAY_F="%Y %m %d"
	TIME_F="%H:%M"
	TODAY="$(date "+${DAY_F}")"
	YESTERDAY="$(date -d "- 1 day" "+${DAY_F}")"
	TWO_DAYS="$(date -d "- 2 day" "+${DAY_F}")"

# TODO
#ALIGNSEDCMD="s/^Sent/<right align>/;s/^Received/<left align>/;"
	# Colorize Sent and Received lines
	RECEIVED_COLOR="6"
	RECEIVED_HEAD_COLOR="242"
	SENT_COLOR="4"
	SENT_HEAD_COLOR="242"
	
	tail -n9999 -f "$SXMO_LOGDIR/$LOGDIRNUM/sms.txt" | 
		awk \
		-v contactscmd="$(mkcontactscmd)" \
		-v day_f="$DAY_F" \
		-v time_f="$TIME_F" \
		-v today="$TODAY" \
		-v yesterday="$YESTERDAY" \
		-v two_days="$TWO_DAYS" \
		'
		function print_text(clr,cols){
			printf "%s%*s%s\n",\
			clr,\
			cols,\
			$0,\
			"'`tput op`'";
		}
		{
			# Format send/receive line
			if(/^.*Sent/ || /^.*Received/){
				
				# Remove some bits
				gsub(/(-[0-9]{4}:)|(at )/,"")
				gsub(/[-T:]/," ")

# 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)#;"
				# Format contacts if applies (group messaging)
				split(contactscmd,c,":::")
				for (i=1; i < length(c); i++){
					split(c[i],cc,"::")
					gsub(cc[1],cc[2])
				}

# Replace phone numbers in the filename with contacts from contact book
CONTACTSSEDCMD="$(mkcontactssedcmd)"
				# 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")
			}
			
			if(/^.*Sent/){
				right_align=1
				print_text("'`tput setaf "$SENT_HEAD_COLOR"`'",'`tput cols`')
			}else if(/^.*Received/){
				right_align=0
				print_text("'`tput setaf "$RECEIVED_HEAD_COLOR"`'")
			}else if(right_align > 0){
				print_text("'`tput setaf "$SENT_COLOR"`'",'`tput cols`')
			}else{
				print_text("'`tput setaf "$RECEIVED_COLOR"`'",0)
			}
		}'  
}

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 "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\""
sxmo_terminal.sh sh -c "$0 format $@"
-- 
2.40.1




Thanks!

To git@git.sr.ht:~anjan/sxmo.org
   2ed126e..5118136  main -> main
This is awesome.  Haven't tested it yet, but I noticed:
Thanks, testing locally.  Note sure we'd go with this as a default 
(resizing windows and edge cases have caused problems with alignments 
before), but we might have a 'commented out' version in the hook for 
users to try out if they want... or maybe an SXMO_SMS_ALIGNMENT=1?