From da80403b66fb9e84fad0ba99878a5d8a0d5d2fe8 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Tue, 3 Aug 2021 08:03:13 -0600 Subject: [PATCH] Restore brightness --- dotfiles/lib/bin/brightness.sh | 3 ++ dotfiles/lib/bin/brightness_manager.py | 75 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 dotfiles/lib/bin/brightness.sh create mode 100755 dotfiles/lib/bin/brightness_manager.py diff --git a/dotfiles/lib/bin/brightness.sh b/dotfiles/lib/bin/brightness.sh new file mode 100755 index 00000000..2974f818 --- /dev/null +++ b/dotfiles/lib/bin/brightness.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +volnoti-show $(sudo brightness_manager.py -c "$@" -p) diff --git a/dotfiles/lib/bin/brightness_manager.py b/dotfiles/lib/bin/brightness_manager.py new file mode 100755 index 00000000..0efd77ba --- /dev/null +++ b/dotfiles/lib/bin/brightness_manager.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +import argparse +import os +import sys + + +class BrightnessManager(object): + + @classmethod + def from_path(cls, path): + return cls( + set_brightness_filepath=os.path.join(path, "brightness"), + actual_brightness_filepath=os.path.join(path, "actual_brightness"), + max_brightness_filepath=os.path.join(path, "max_brightness"), + ) + + def __init__( + self, set_brightness_filepath, max_brightness_filepath, actual_brightness_filepath + ): + self.set_brightness_filepath = set_brightness_filepath + self.max_brightness_filepath = max_brightness_filepath + self.actual_brightness_filepath = actual_brightness_filepath + + @property + def current_brightness(self): + with open(self.actual_brightness_filepath) as fd: + return int(fd.read()) + + @property + def max_brightness(self): + with open(self.max_brightness_filepath) as fd: + return int(fd.read()) + + @current_brightness.setter + def current_brightness(self, brightness): + with open(self.set_brightness_filepath, 'w') as fd: + fd.write(str(brightness)) + + def increment_by_proportion(self, proportion): + new_brightness = self.current_brightness + int(self.max_brightness * proportion) + new_brightness = min(new_brightness, self.max_brightness) + self.current_brightness = new_brightness + + @property + def current_proportion(self): + return float(self.current_brightness) / self.max_brightness + + +IntelBrightnessManager = BrightnessManager.from_path( + "/sys/class/backlight/intel_backlight", +) + + +def build_parser(): + parser = argparse.ArgumentParser( + description='Interact with macbook brightness', + ) + parser.add_argument( + "--change", "-c", + help="Change volume by the given percentage", + default=0 + ) + parser.add_argument( + "--print", "-p", dest="do_print", + action='store_true', + default=False + ) + return parser + + +if __name__ == '__main__': + args = build_parser().parse_args() + IntelBrightnessManager.increment_by_proportion(float(args.change) / 100) + if args.do_print: + print(int(IntelBrightnessManager.current_proportion * 100))