cal and repetitive patterns

interpretive language scripts


Moderator: Forum moderators

Post Reply
darksun
Posts: 124
Joined: Tue Dec 19, 2023 10:12 am
Location: sitting in front of my terminal
Has thanked: 52 times
Been thanked: 37 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: 6551
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2757 times
Been thanked: 2627 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: 124
Joined: Tue Dec 19, 2023 10:12 am
Location: sitting in front of my terminal
Has thanked: 52 times
Been thanked: 37 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: 357
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 126 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 589 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: 124
Joined: Tue Dec 19, 2023 10:12 am
Location: sitting in front of my terminal
Has thanked: 52 times
Been thanked: 37 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: 357
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 18 times
Been thanked: 126 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 »

User avatar
MochiMoppel
Posts: 1239
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: cal and repetitive patterns

Post by MochiMoppel »

HerrBert wrote: Tue Nov 05, 2024 5:37 pm

I have written a bash script using urxvt and cal command to display three different kinds of event, based on code by @MochiMoppel

By me? There used to be a related thread in the Murga forum (https://oldforum.puppylinux.com/viewtopic.php?t=108481) but it seems to be broken. Maybe @rockedge knows what is going on there. I can access the first and the third page, but not the second.

Whatever the trigger for your script was, I use a similar script which pops up a gtkdialog calendar when clicking the JWM clock. It's based on the same idea of formatting the cal output and shows the current day in green and any event in red. One of my goals was to make the calendar as small as possible.

I use this calendar mainly for birthdays or other anniversaries and for one-time events with a specific date in the future. However for doing what the OP asks for it's probably not suitable.

calendar.png
calendar.png (22.2 KiB) Viewed 471 times
User avatar
rockedge
Site Admin
Posts: 6551
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2757 times
Been thanked: 2627 times
Contact:

Re: cal and repetitive patterns

Post by rockedge »

@MochiMoppel Thank you for the information about the non-functioning page.

This happens since the many database conversions done to save the murga forum's data. Seems that there are occasional problems with the custom html tags that need to be defined in javascript that cause errors during page parsing and generation, causing the blank page we are seeing.

I am working on it.

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

Re: cal and repetitive patterns

Post by rockedge »

@MochiMoppel, @fredx181,

I fixed the problem with the blank page on the original topic on the old forum. It's caused by a missing closing custom tag </r> in the source html for post #28. I modified it in the database directly by adding the closing tag. It does appear that post #28 had it's text corrupted during the conversion process.

Fixed post -> https://oldforum.puppylinux.com/viewtop ... 7#p1013517

Actually good to know how to fix this, since there are other blank pages lurking, probably for the same reason, that I can now more than likely fix.

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

Re: cal and repetitive patterns

Post by HerrBert »

MochiMoppel wrote: Thu Nov 07, 2024 7:26 am

...
By me? There used to be a related thread in the Murga forum (https://oldforum.puppylinux.com/viewtopic.php?t=108481) ...

Whatever the trigger for your script was, I use a similar script which pops up a gtkdialog calendar when clicking the JWM clock. It's based on the same idea of formatting the cal output and shows the current day in green and any event in red. One of my goals was to make the calendar as small as possible.
...

The trigger was this post: https://oldforum.puppylinux.com/viewtop ... 4#p1012054, so credits also belong to @Burunduk

User avatar
MochiMoppel
Posts: 1239
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 21 times
Been thanked: 439 times

Re: cal and repetitive patterns

Post by MochiMoppel »

rockedge wrote: Fri Nov 08, 2024 2:16 am

I fixed the problem with the blank page on the original topic on the old forum

Works now, thanks :thumbup:

HerrBert wrote: Fri Nov 08, 2024 1:24 pm

The trigger was this post: https://oldforum.puppylinux.com/viewtop ... 4#p1012054, so credits also belong to @Burunduk

Yes, that's the one I was looking for. I also found that I posted an early and simplified version of my current calendar here. Completely forgot all this stuff.

proebler wrote: Wed Nov 06, 2024 9:19 am

Perhaps this provides a solution: https://www.vertex42.com/calendars/shif ... endar.html

This should exactly do what the OP aims for. It claims that it requires Excel 10 (which I don't have), but it loads into Gnumeric and I see no reason for it not to work in Gnumeric - after a bit of tweaking. It uses only conditional formatting but no macros, which would be a no-go.

User avatar
Trapster
Posts: 182
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 54 times

Re: cal and repetitive patterns

Post by Trapster »

darksun
Posts: 124
Joined: Tue Dec 19, 2023 10:12 am
Location: sitting in front of my terminal
Has thanked: 52 times
Been thanked: 37 times

Re: cal and repetitive patterns

Post by darksun »

thanks @Trapster

User avatar
Trapster
Posts: 182
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 54 times

Re: cal and repetitive patterns

Post by Trapster »

From ChatGPT:

Code: Select all

#!/bin/bash

# Define start date for the schedule (yyyy-mm-dd format)
start_date="2024-11-10"

# Number of days to generate the schedule for
days_to_generate=30

# Loop through the days and create the schedule
current_date="$start_date"
for ((i=0; i<$days_to_generate; i++)); do
    # Get the current weekday (0=Sunday, 1=Monday, ..., 6=Saturday)
    day_of_week=$(date -d "$current_date" '+%u')
    
    # Determine if it's a work day or an off day
    if ((i % 6 < 4)); then
        schedule="Work"
    else
        schedule="Off"
    fi

    # Print the current date and schedule
    echo "$current_date - $schedule"

    # Increment the date by one day
    current_date=$(date -I -d "$current_date + 1 day")
done

Result:

Code: Select all

root@puppy:/mnt/sdb1$ ./cal.sh
2024-11-10 - Work
2024-11-11 - Work
2024-11-12 - Work
2024-11-13 - Work
2024-11-14 - Off
2024-11-15 - Off
2024-11-16 - Work
2024-11-17 - Work
2024-11-18 - Work
2024-11-19 - Work
2024-11-20 - Off
2024-11-21 - Off
2024-11-22 - Work
2024-11-23 - Work
2024-11-24 - Work
2024-11-25 - Work
2024-11-26 - Off
2024-11-27 - Off
2024-11-28 - Work
2024-11-29 - Work
2024-11-30 - Work
2024-12-01 - Work
2024-12-02 - Off
2024-12-03 - Off
2024-12-04 - Work
2024-12-05 - Work
2024-12-06 - Work
2024-12-07 - Work
2024-12-08 - Off
2024-12-09 - Off
root@puppy:/mnt/sdb1$ 
Post Reply

Return to “Scripts”