Page 1 of 1

cal and repetitive patterns

Posted: Tue Nov 05, 2024 1:48 pm
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


Re: cal and repetitive patterns

Posted: Tue Nov 05, 2024 3:23 pm
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.......


Re: cal and repetitive patterns

Posted: Tue Nov 05, 2024 3:33 pm
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


Re: cal and repetitive patterns

Posted: Tue Nov 05, 2024 5:37 pm
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 110 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; }

Re: cal and repetitive patterns

Posted: Tue Nov 05, 2024 6:10 pm
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.


Re: cal and repetitive patterns

Posted: Tue Nov 05, 2024 6:30 pm
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.


Re: cal and repetitive patterns

Posted: Wed Nov 06, 2024 9:19 am
by proebler