From 2f6f6ff7256ec954acf20664349d404dcc273d5d Mon Sep 17 00:00:00 2001 From: elwld Date: Sat, 13 Aug 2022 15:29:37 +0500 Subject: [PATCH] add night mode for themes --- src/wallpyper.py | 95 ++++++++++++++++++++++++++++++++----------- src/wallpyperFuncs.py | 61 +++++++++++++++++++-------- 2 files changed, 114 insertions(+), 42 deletions(-) diff --git a/src/wallpyper.py b/src/wallpyper.py index 6dc9cd3..9ea31bd 100755 --- a/src/wallpyper.py +++ b/src/wallpyper.py @@ -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 diff --git a/src/wallpyperFuncs.py b/src/wallpyperFuncs.py index 2b7f386..e5a46f4 100644 --- a/src/wallpyperFuncs.py +++ b/src/wallpyperFuncs.py @@ -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 set 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 \ No newline at end of file + 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 \ No newline at end of file