From cc681d0f59c8859ba778d2c6c9315e024c759405 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 6 Jun 2023 11:11:46 +0200 Subject: [sway] add --unique flag to wallpaper script to not cycle through the same wallpaper twice per available files --- .config/sway/bin/wallpaper.py | 44 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to '.config/sway/bin') diff --git a/.config/sway/bin/wallpaper.py b/.config/sway/bin/wallpaper.py index 1dad045..449e5f3 100755 --- a/.config/sway/bin/wallpaper.py +++ b/.config/sway/bin/wallpaper.py @@ -26,6 +26,11 @@ parser.add_argument( default=600 ) +parser.add_argument( + '--unique', + default=False +) + args = parser.parse_args() config = json.loads(args.config) @@ -35,6 +40,14 @@ config = json.loads(args.config) " Program """ +# set variables for --unique flag +already_selected_file_paths = {} +if args.unique: + for weight, config_item in config.items(): + config[weight]['id'] = hash(json.dumps(config_item, sort_keys=True)) + already_selected_file_paths[config_item['id']] = [] + +# run the loop running_bg_process = None while True: probability_number = random.randrange(start=1, stop=100, step=1) @@ -43,29 +56,50 @@ while True: # see https://stackoverflow.com/questions/16489449/select-element-from-array-with-probability-proportional-to-its-value/16490300#16490300 selected_config = {} accumulative = 0 - for weight, wallpaper_path in config.items(): + for weight, config_item in config.items(): lower_bound = accumulative accumulative += int(weight) upper_bound = accumulative if lower_bound <= probability_number and probability_number <= upper_bound: - selected_config = wallpaper_path + selected_config = config_item break # build list of file paths to choose from found_file_paths = [] - for dirpath, dirnames, filenames in os.walk(os.path.expandvars(wallpaper_path['find'])): + for dirpath, dirnames, filenames in os.walk(os.path.expandvars(selected_config['find'])): for filename in filenames: found_file_paths.append(f'{dirpath}/{filename}') if 'exclude' in selected_config.keys(): found_file_paths = list(filter( - lambda file_path: (not re.match(os.path.expandvars(wallpaper_path['exclude']), file_path)), + lambda file_path: (not re.match(os.path.expandvars(selected_config['exclude']), file_path)), found_file_paths )) + if args.unique: + # filter file paths from already selected paths + filtered_file_paths = [ + item + for item in found_file_paths + if item not in already_selected_file_paths[selected_config['id']] + ] + + # if we cycled through all of them, repeat + if not filtered_file_paths: + filtered_file_paths = found_file_paths + already_selected_file_paths[selected_config['id']] = [] + + found_file_paths = filtered_file_paths + + # select file + selected_file_path = random.choice(found_file_paths) + + if args.unique: + already_selected_file_paths[selected_config['id']].append(selected_file_path) + # run swaybg - swaybg_cmd = ['swaybg', '-i', random.choice(found_file_paths), '-m', 'fill'] + swaybg_cmd = ['swaybg', '-i', selected_file_path, '-m', 'fill'] replacing_process = subprocess.Popen(swaybg_cmd) # wait until new instance of swaybg has booted up properly -- cgit v1.2.3