add night mode for themes

This commit is contained in:
elwld 2022-08-13 15:29:37 +05:00
parent 2c73e0e6a5
commit 2f6f6ff725
2 changed files with 114 additions and 42 deletions

View File

@ -3,35 +3,82 @@
from wallpyperFuncs import *
def main():
theme = ""
if len(sys.argv) == 2 and sys.argv[1] in ("help","--help","-h"):
printHelpInfo()
else:
if len(sys.argv) == 1: theme = None
elif len(sys.argv) == 2 and sys.argv[1] in ("quit", "stop"): killLastSession(); sys.exit(0)
elif len(sys.argv) >= 2 and sys.argv[1] == "set":
if len(sys.argv) == 2:
themes = getThemes()
theme = ""
if len(sys.argv) == 1: theme = None
elif len(sys.argv) == 2 and sys.argv[1] in ("quit", "stop"): killLastSession(); sys.exit(0)
elif len(sys.argv) >= 2 and sys.argv[1] == "set":
if len(sys.argv) == 2:
themes = getThemes()
try:
import dmenu
theme = dmenu.show(themes, prompt='Themes:')
except ModuleNotFoundError:
try:
import dmenu
from rofi import Rofi
theme = dmenu.show(themes, prompt='Themes:')
except ModuleNotFoundError:
try:
from rofi import Rofi
r = Rofi()
theme = themes[r.select('Themes:', themes)[0]]
except ModuleNotFoundError:
printHelpInfo()
exit(0)
elif len(sys.argv) == 3:
theme = sys.argv[2]
else: printHelpInfo()
r = Rofi()
theme = themes[r.select('Themes:', themes)[0]]
except ModuleNotFoundError: printHelpInfo()
elif len(sys.argv) == 3:
theme = sys.argv[2]
else: printHelpInfo()
else: printHelpInfo()
setTheme(theme)
config = getConfig()
if theme == None:
theme = config["default_theme"]
if theme not in config:
print(f'There is no "{theme}" in config')
exit(0)
killLastSession()
if "night" in config[theme]:
nightTime = None
if "night_time" in config[theme]:
nightTime = config[theme]["night_time"].split("-")
elif "default_night_time" in config:
nightTime = config["default_night_time"].split("-")
if nightTime != None:
nightMode = False
currentTime = datetime.now().strftime("%H:%M:%S")
if timeDiff(currentTime, nightTime[0]+":0") > timeDiff(currentTime, nightTime[1]+":0"):
nightMode = True
while True:
threadIsAlive = True
mode = Thread(target=setTheme, args=(theme, nightMode), daemon = True)
mode.start()
currentTime = datetime.now().strftime("%H:%M:%S")
if nightMode:
sleep(timeDiff(currentTime, nightTime[1]+":0"))
else:
sleep(timeDiff(currentTime, nightTime[0]+":0"))
nightMode = not nightMode
threadIsAlive = False
mode.join()
else:
setTheme(theme)
else:
setTheme(theme)
if __name__ == '__main__':
main()
try:
main()
except KeyboardInterrupt:
pass

View File

@ -4,6 +4,7 @@ import random
import sys
from time import sleep
from threading import Thread
from datetime import datetime
def printHelpInfo():
@ -12,6 +13,7 @@ Commands:
quit, stop shutdown wallpyper
set <theme> set <theme> theme
help, --help, -h print this information""")
sys.exit(0)
def runCommand(commandName, output=False):
if output:
@ -66,22 +68,13 @@ def setRandomWallpaper(path):
setWallpaper(wallpaperName)
def setDynamicWallpaper(path, delay):
while True:
setRandomWallpaper(path)
sleep(delay)
def setTheme(themeName = None):
killLastSession()
def setTheme(themeName = None, nightMode = False):
config = getConfig()
if themeName == None:
themeName = config["default_theme"]
theme = config[themeName]
if nightMode:
theme = config[themeName]["night"]
else:
theme = config[themeName]
if theme["type"] == "static":
try:
@ -98,12 +91,13 @@ def setTheme(themeName = None):
delay = delay[0] + delay[1]*60 + delay[2]*3600
wallpyperThread = Thread(target=setDynamicWallpaper, args=(theme["path"],delay,))
wallpyperThread.start()
global threadIsAlive
while threadIsAlive:
setRandomWallpaper(theme["path"])
sleep(delay)
else:
printHelpInfo()
sys.exit(0)
def getThemes():
@ -115,4 +109,35 @@ def getThemes():
if type(config[e]) == dict:
themes.append(e)
return themes
return themes
def timeDiff(time1, time2):
time1 = list(map(int,time1.split(":")))
time2 = list(map(int,time2.split(":")))
seconds = 0
diff = 0
while (time1[2]+diff)%60 != time2[2]:
diff+=1
seconds+=diff
if (diff + time1[2] > 60): time1[1]+=1
diff = 0
while (time1[1]+diff)%60 != time2[1]:
diff+=1
seconds+=diff*60
if (diff + time1[1] > 60): time1[0]+=1
diff = 0
while (time1[0]+diff)%24 != time2[0]:
diff+=1
seconds+=diff*60*60
return seconds