katernet / darkmode

Set macOS dark mode and Alfred dark theme at sunset
GNU General Public License v3.0
149 stars 10 forks source link

Ability to provide specific light/dark schedule #6

Closed IdanAdar closed 6 years ago

IdanAdar commented 6 years ago

katernet: Please submit any bugs with 1.9b in this issue.


That is, I'd like Light Mode to start at, for example, 06:00am and Dark Mode to start at, for example, 19:00pm.

If I could supply these values as arguments to the script and this will override the default behavior (change to Dark Mode on sunset) it'd be great!

katernet commented 6 years ago

Thanks for the enhancement request. I'll look into it c:

CodeNea commented 6 years ago

That would be awesome. In the meanwhile, I think it is sufficient to change rows 82 and 83: # Convert times to 24H riseT24=$(date -jf "%I:%M %p" "${riseT}" +"%H:%M" 2> /dev/null) setT24=$(date -jf "%I:%M %p" "${setT}" +"%H:%M" 2> /dev/null)

@katernet Should it be changed to something like this?

riseT24="06:00" setT24="22:00"

katernet commented 6 years ago

@IdanAdar I looked into this and I've been thinking this idea is not feasible for this project as the bulk of darkmode is built around checking and storing changes in solar times. Introducing this feature may require complete rewrite of the the functions, introducing bugs and instability.

Which leads me to..

@CodeNea good pickup as originally I was thinking of a more complicated way of introducing this. It still requires argument calls all throughout the functions. I'm scripting and testing a beta at the moment and we'll see how it goes, I would need to introduce it without affecting the 'vanilla' calls.

IdanAdar commented 6 years ago

@CodeNea's suggestion is just as viable for me... I'm good. :)

CodeNea commented 6 years ago

@katernet In that case if there is any input parameter then simply don't query the Weather API as well. So basically it only stores the set and rise, and doesn't periodically check the weather API. Just as an optimization. 👍

katernet commented 6 years ago

What do you guys think of this framework so far? Can you see any optimizations?

Usage: $ ./darkmode.sh [sunrise 24H] [sunset 24H]

#!/bin/bash

darkdir=~/desktop/darkmode
plistR=~/desktop/local.test.sunrise.plist
plistS=~/desktop/local.test.sunset.plist

#set -x

darkMode() {
    case $1 in
        off) 
            # Dark Mode OFF

            if [[ $2 = static ]]; then
                editPlist add $3 0 "$plistS"
            fi
            ;;
        on)
            # Dark Mode ON

            if [[ $2 = static ]]; then
                editPlist add $4 0 "$plistR"
            fi
            ;;
    esac
}

solar() {
    #mkdir "$darkdir" 2> /dev/null
    if [[ $1 = static ]]; then
        riseT24=$2
        setT24=$3
        getTime static $riseT24 $setT24
    fi
    if [ $# -eq 0 ]; then
        # Vanilla script
        :
    fi
}

launch() {
    /usr/libexec/PlistBuddy -c "Add :Label string io.github.katernet.darkmode.sunrise" "$plistR" 1> /dev/null
    /usr/libexec/PlistBuddy -c "Add :Program string ${darkdir}/darkmode.sh" "$plistR" 
    /usr/libexec/PlistBuddy -c "Add :RunAtLoad bool true" "$plistR" 
    /usr/libexec/PlistBuddy -c "Add :Label string io.github.katernet.darkmode.sunset" "$plistS" 1> /dev/null
    /usr/libexec/PlistBuddy -c "Add :Program string ${darkdir}/darkmode.sh" "$plistS"
}

editPlist() {
    case $1 in
        add)
            /usr/libexec/PlistBuddy -c "Add :StartCalendarInterval:Hour integer $2" "$4"
            /usr/libexec/PlistBuddy -c "Add :StartCalendarInterval:Minute integer $3" "$4"
            ;;
        update)
            # Vanilla script
            ;;
    esac
}

getTime() {
    if [ $# -eq 0 ]; then
        # Vanilla script
        :
    else
        riseH=$2
        riseM=0
        setH=$3
        setM=0
    fi
    timeH=$(date +"%H" | sed 's/^0//')
    timeM=$(date +"%M" | sed 's/^0//')
}

# ---- config ----

if (( $1 >= 0 && $1 <= 24 )) && (( $2 >= 0 && $2 <= 24 )); then
    solar static $1 $2
elif (( $1 < 0 && $1 > 24 )) || (( $2 < 0 && $2 > 24 )); then
    exit 1
else
    solar
fi

if [ ! -f "$plistR" ] || [ ! -f "$plistS" ]; then
    launch
fi

# ---- code -----

echo $1
echo $2

if [[ "$timeH" -ge "$riseH" && "$timeH" -lt "$setH" ]]; then
    if [[ "$timeH" -ge $((riseH+1)) || "$timeM" -ge "$riseM" ]]; then
        if [ $# -eq 0 ]; then
            darkMode off # Vanilla script
        else 
            darkMode off static $1 $2
        fi
    elif [[ "$timeH" -ge "$setH" && "$timeM" -ge "$setM" ]] || [[ "$timeH" -le "$riseH" && "$timeM" -lt "$riseM" ]]; then 
        if [ $# -eq 0 ]; then
            darkMode on # Vanilla script
        else 
            darkMode on static $1 $2
        fi
    fi
elif [[ "$timeH" -ge 0 && "$timeH" -lt "$riseH" ]]; then
    if [ $# -eq 0 ]; then
        darkMode on # Vanilla script
    else 
        darkMode on static $1 $2
    fi
elif [[ "$timeH" -eq "$setH" && "$timeM" -lt "$setM" ]]; then
    if [ $# -eq 0 ]; then
        darkMode on # Vanilla script
    else 
        darkMode on static $1 $2
    fi
else
    if [ $# -eq 0 ]; then
        darkMode on # Vanilla script
    else 
        darkMode on static $1 $2
    fi
fi
CodeNea commented 6 years ago

I'm not the best at bash scripts, otherwise I would have commented less and just done it, but it looks cleaner and more understandable now even for me. 👍

I would rename the comment "# Vanilla script" to "# Solar script" or something like that for someone who comes later on and wants to edit the code and will haven no idea what the vanilla script is.

A small small detail, if I understood correct it seems the script only allows full hours, I don't really mind but to spare yourself of future requests, just add support for minutes as well so you can enter the time in full, i.e HH:mm > for example "21:35".

Otherwise great work!

CodeNea commented 6 years ago

Btw, did you remove the sqlite3 all together? You storing the time in a plist now instead? Feels like a better solution.

katernet commented 6 years ago

This is just a rough proof of concept, with all of the 'vanilla' script removed.

Yes at the moment it's just for hours and not minutes, I tried to script arguments HH:mm and I couldn't get it to work 😄 but I may not be knowledgeable enough.

CodeNea commented 6 years ago

So how do I use it? 😀

katernet commented 6 years ago

I still need to integrate minutes, further optimize and integrate with the master branch without bugs. Once done I'll commit and release. If any of these fail I may not release.

katernet commented 6 years ago

@IdanAdar I have committed to the beta branch and released 1.9b

Please give it a try and post any bugs here.

IdanAdar commented 6 years ago

Any new instructions? What are the changes?

katernet commented 6 years ago

See the description of the beta release. For changes click on the commit link below the tag.

IdanAdar commented 6 years ago

Apologise, but I don't understand from the description if we can now specify arguments for custom theme change or not.

katernet commented 6 years ago

From your original post, you would like Dark Mode to turn off at 06:00 and to turn on at 19:00. So:

$ ./darkmode.sh 0600 1900

It will probably work over the top of your existing darkmode install, however I haven't tested this and so probably better to uninstall first.

IdanAdar commented 6 years ago

I ran the above command with the beta code, and despite the time now (08:13) it changed to dark mode.

Should I actually do darkmode.sh 19:00 0600 (this didn't help either).

From 06:00 to 19:00 it's supposed to be Light Mode, from 19:00 to 06:00 it's supposed to be Dark Mode.

katernet commented 6 years ago

This is exactly why I was wary of implementing this and have not committed this to the master branch, there are too many opportunities for bugs to creep in 😄

This was working in my testing and now I am getting the same results as you. I'll look into it.

You should be using HHMM HHMM

katernet commented 6 years ago

I've pulled the beta release. Use clone or download now from the beta branch.

IdanAdar commented 6 years ago

You should be using HHMM HHMM

My example showed 19:00 but that was just a typo... Right now it seems to work... applied the command and it switched to Light Mode.

katernet commented 6 years ago

Cool! It's weird that I am getting the same results as you were. I'll make a new commit to the beta branch if I find a bug.

katernet commented 6 years ago

This was the bug. Required to set the script as an argument itself, rather than just the static time arguments.

if [ $# -eq 2 ]; then # If static time arguments provided
    /usr/libexec/PlistBuddy -c "Add :ProgramArguments array" "$plistR"
    /usr/libexec/PlistBuddy -c "Add :ProgramArguments:0 string $1" "$plistR"
    /usr/libexec/PlistBuddy -c "Add :ProgramArguments:1 string $2" "$plistR"
    /usr/libexec/PlistBuddy -c "Add :ProgramArguments array" "$plistS"
    /usr/libexec/PlistBuddy -c "Add :ProgramArguments:0 string $1" "$plistS"
    /usr/libexec/PlistBuddy -c "Add :ProgramArguments:1 string $2" "$plistS"
fi

This cause the script to be run again without arguments, which will run the vanilla solar routines. I've committed a fix.

IdanAdar commented 6 years ago

First attempt didn't switch to dark mode so that's a good sign.

katernet commented 6 years ago

Nice! Thanks for testing @IdanAdar

Arghh I forgot to fix the editPlist function arguments - This runs if the times differ, so if you wanted to change the argument times. I've committed another fix.

IdanAdar commented 6 years ago

Awesome. I believe this issue can be resolved?

katernet commented 6 years ago

I'll leave this issue open for a bit for if there others who encounter bugs.

katernet commented 6 years ago

Closed. I'll merge this into the master for a new release once I've sorted out the exiting issues.