cal and repetitive patterns

interpretive language scripts


Moderator: Forum moderators

Post Reply
darksun
Posts: 115
Joined: Tue Dec 19, 2023 10:12 am
Location: Italy
Has thanked: 51 times
Been thanked: 35 times

cal and repetitive patterns

Post by darksun »

Hi,

I am looking for a way to create a calendar with repetitive patters.

For example:
4 days on
2 days off

A friend of mine works a shift job and asked me to have a simple way to eg color the days red when he works and leave it uncolored when he has the days off. He works 4 days on and then 2 days off, every time.

I love simple, spartan and lightweight things so I was thinking to have a bash script to color the output of the command "cal" in that way: chosen a starting day, 4 days of a color (eg red) and 2 days leave it uncolored.
I have searched online and asked tgpt but cannot come up with a way to do so.

Thank you

User avatar
rockedge
Site Admin
Posts: 6507
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2716 times
Been thanked: 2604 times
Contact:

Re: cal and repetitive patterns

Post by rockedge »

@MochiMoppel is the person to ask for advice....he's a master of shell scripting......if anyone can color highlight 4 days on and 2 days off with the cal command as the base.......he'd be the guy I'd bet on.

Meanwhile since this already sounds like a very useful thing, I'll be looking for a solution as well.......

darksun
Posts: 115
Joined: Tue Dec 19, 2023 10:12 am
Location: Italy
Has thanked: 51 times
Been thanked: 35 times

Re: cal and repetitive patterns

Post by darksun »

thank you @rockedge .

The top would be to create a calendar event (calendar software eg mobile phone's calendar app ) but I cannot come up with an idea to do so with the available options under recurrent events. Maybe it does not exist a way to do so.

As second option is the one I mentioned in my first post, then a "print screen" of the terminal output and export it as image :thumbup2:

EDIT: silly me, as I was thinking about the idea of the calendar event on my android calendar app it was easy to implement, creating a event lasting 4 days and repeating it every 6 days

Last edited by darksun on Tue Nov 05, 2024 6:08 pm, edited 1 time in total.
HerrBert
Posts: 355
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 125 times

Re: cal and repetitive patterns

Post by HerrBert »

@darksun
Be prepared for crazy s**t!
I have written a bash script using urxvt and cal command to display three different kinds of event, based on code by @MochiMoppel
ATM i don't have the time to explain, but to give an impression of what can be done please see screenshot and code below:

urxvt-Übersicht Oktober.jpg
urxvt-Übersicht Oktober.jpg (32.62 KiB) Viewed 106 times

Code: Select all

#!/bin/bash
cd "$(dirname "$(readlink -f "$0")")" # feiertage.sh ist hier

[ "$1" = "--goto" ] && {
	GOTO="$(Xdialog --stdout --title "PIM Übersicht" --no-cancel --inputbox $'Gehe zu Monat [optional: Datum]\nFormat: [dd] mm [yyyy]' x "$(date "+%-m %Y")")"
	[ "$GOTO" ] && exec $0 $GOTO
}

# bestimmtes Datum testen:
[ $# -eq 3 ] && {
	read TODAY0 MONTH YEAR <<< "$@"
	shift
} || {
	read TODAY0 MONTH YEAR <<< "$(date "+%d %m %Y")"
}
TODAY="${TODAY0/#0/ }" # read schluckt führendes Leerzeichen von date +%_d

# Monat (und Jahr) als Parameter, sonst aktuelles Datum
ARG="$@"
[ "$2" ] || ARG="$1 $YEAR" # nur Monat als Parameter
[ "$1" ] || ARG="$MONTH $YEAR" # ohne Parameter gestartet
[ "$(echo "$ARG" | tr -d "[:digit:]")" != " " ] && exit 1
[ "$ARG" != "$MONTH $YEAR" ] && TODAY=99
[ ${#ARG} -eq 6 ] && ARG="0$ARG" # wenn Monat einstellig

TITLE="Übersicht $(date --date="${ARG##* }-${ARG% *}-01" +%B)" # voller Monatsname
RET=/tmp/pim_query.$$ # Rückgabe aus urxvt

# DAYS in der Form " 5|16"
#-------------------^ Leerzeichen wenn einstellig!

WIDTH=26 # minimale Breite
HEIGHT=11 # minimale Höhe: 8 cal + 1 Leer + 1 Unterstrich + 1 Navi

HOLIDAY="$(./feiertage.sh ${ARG##* } | grep "\.${ARG%% *}\.")"
BIRTHDAY="$(grep "\.${ARG%% *}\." /mnt/sda3/Dateien/Kontakte/geburtstage.txt | grep -v '#')"
APPOINT="$(grep ^${ARG: -2}${ARG%% *}.. /mnt/sda3/Dateien/Termine/termine.txt | sed 's/\(..\)\(..\)\(..\)/\3\.\2\.20\1/;s/ -c[0-9]\+$//;s/00:00 //;s/# *//')" # yymmdd -> dd.mm.20yy; cronjob, 00:00 und # entfernen

ITEMS="$(echo -e "$HOLIDAY\n$APPOINT\n$BIRTHDAY" | grep . | sort -t \. -k 1,1 -n)"

if [ "$ITEMS" ];then
		ITEMS="$(sed "s/^/ /;s/$/ /" <<< "$ITEMS")"
		read LINES WIDTH <<< "$(wc -l -L <<< "$ITEMS")"
		HEIGHT=$((HEIGHT+LINES))
		[ $WIDTH -lt 26 ] && WIDTH=26
fi

ITEMS="${ITEMS:+$ITEMS
}$(printf "%${WIDTH}s" | tr \  \-)" # Trennlinie

[ $TODAY -ne 99 ] && {
	MOON="$(./mondphase)"
	SUN="$(< /tmp/sonnenlauf_$(date +%j))"
	SA="$(date --date="@${SUN%:*}" +%H:%M)"
	SU="$(date --date="@${SUN##*:}" +%H:%M)"
	ITEMS="$ITEMS
 SA $SA SU $SU ${MOON: -1}"
	((HEIGHT++))
}

# unterschiedliche Farben
# sed einmal aufgedröselt
ADAYS="$(sed '
{			# Loop starten
s/^\(..\).*/\1|/	# erste zwei Zeichen ausschneiden und | anhängen
s/^0/ /			# führende 0 mit Leerzeichen ersetzen
H			# an holdspace anhängen
$!d			# patternspace nicht löschen, wenn Ende erreicht ist
}			# Loop beenden, wenn patternspace gelöscht wird
x			# holdspace und patternspace tauschen
s/\n//g			# newlines entfernen
s/|$//			# letztes | entfernen
' <<< "$APPOINT")"
BDAYS="$(sed '{s/^\(..\).*/\1|/;s/^0/ /;H;$!d};x;s/\n//g;s/|$//' <<< "$BIRTHDAY")"
HDAYS="$(sed '{s/^\(..\).*/\1|/;s/^0/ /;H;$!d};x;s/\n//g;s/|$//' <<< "$HOLIDAY")"

# Ereignis+heute=weiss/rot sonst heute invertiert (weiss/dgrau)
[ "$(echo "${ADAYS}|${BDAYS}|${HDAYS}" | grep -o "$TODAY")" ] && COLOR="\x1b\[1;47;31m" || COLOR="\x1b[7m"

ITEMS="$(sed -r 's/('$TODAY0.$MONTH.'.*)($)/'"$COLOR"'\1\x1b\[0m\2/g' <<< "$ITEMS")"

# cal util-linux 2.37.4
# KW einfügen und mit zusätzlichem Leerzeichen splitten
SPLITCAL="$(cal -m -w --iso ${ARG/#0} | sed '2 s/^../KW/;s/.\{3\}/ &XX\n/')"

# sed mit -r (extended regular expression)
TINTCAL="$(sed -r '
6,$ {/XX/!s/('"$TODAY"')( |$)/'"$COLOR"'\1\x1b\[0m\2/;}	# heute
6,$ {/XX/!s/('"${BDAYS:-99}"')( |$)/\x1b[42;30m\1\x1b\[0m\2/g;}	# Geburtstage
6,$ {/XX/!s/('"${HDAYS:-99}"')( |$)/\x1b[41;37m\1\x1b\[0m\2/g;}	# Feiertage
6,$ {/XX/!s/('"${ADAYS:-99}"')( |$)/\x1b[43;30m\1\x1b\[0m\2/g;}	# Termine
' <<< "$SPLITCAL")"

# Splitten rückgängig, Leerzeichen einfügen und Spalte KW kursiv
CAL="$(sed '$!N;s/XX\n/ /;3,$ s/^../\x1b\[3;37m&\x1b\[0m/' <<< "$TINTCAL")"

urxvt -title "$TITLE" -icon /mnt/sda3/temp/icons/working.svg -geometry ${WIDTH}x${HEIGHT}-20+68 -tr -sh 50 +sb +bc -e sh -c "echo \"$CAL\"
	echo
	echo \"$ITEMS\"
	read -e -n 1 -p \"\$(printf '%$(((WIDTH/2)-8))s< - [d]atum + >')\" OP
	case \$OP in +|-) echo \$(date --date=\"${ARG##* }-${ARG% *}-01 \$OP 1 month\" \"+%m %Y\") > $RET;; d) echo \"--goto\" > $RET;; esac"

[ -f $RET ] && { NEWARG="$(< $RET)"; rm $RET; exec "$0" $NEWARG; }
darksun
Posts: 115
Joined: Tue Dec 19, 2023 10:12 am
Location: Italy
Has thanked: 51 times
Been thanked: 35 times

Re: cal and repetitive patterns

Post by darksun »

it looks cool and promising, but I can see references in ?Dutch? and as well as files (txt, images) present on your storage media.

HerrBert
Posts: 355
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 125 times

Re: cal and repetitive patterns

Post by HerrBert »

As i said, it's just to give an impression of what can be done...
Comments and references are in German. Image is only for the window icon of urxvt.

Last edited by rockedge on Tue Nov 05, 2024 7:17 pm, edited 2 times in total.
Reason: fixed german to German
proebler
Posts: 87
Joined: Sun Aug 23, 2020 6:48 am
Location: AU-TAS
Been thanked: 23 times

Re: cal and repetitive patterns

Post by proebler »

Post Reply

Return to “Scripts”