-
-
Save lacostenycoder/cb94bf11cc59815fa8dc9124cfc9cbc3 to your computer and use it in GitHub Desktop.
A script for adjusting screen brightness in Dell laptops with OLED screens in Ubuntu. It requires xrandr to be installed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import logging | |
import os | |
import subprocess | |
import sys | |
from typing import Literal | |
logging.basicConfig(level=logging.DEBUG) | |
FILE_PATH = os.path.join( | |
os.path.dirname(os.path.realpath(__file__)), ".screen-brightness" | |
) | |
def read_current_level() -> float: | |
if not os.path.isfile(FILE_PATH): | |
return 1 | |
with open( | |
file=FILE_PATH, | |
mode="r", | |
encoding="utf-8", | |
) as file: | |
current_level = file.readline().strip() | |
return float(current_level) | |
def save_level(level: float) -> None: | |
with open( | |
file=FILE_PATH, | |
mode="w", | |
encoding="utf-8", | |
) as file: | |
file.write(str(level)) | |
def adjust_level(method: Literal["up", "down"]) -> None: | |
adjuster = 0.05 if method == "up" else -0.05 | |
current_level = read_current_level() | |
adjusted_level = current_level + adjuster | |
if adjusted_level > 1: | |
adjusted_level = 1 | |
if adjusted_level < 0.2: | |
adjusted_level = 0.2 | |
logging.debug(f"Setting screen brightness to {adjusted_level}.") | |
subprocess.run(["xrandr", "--output", "eDP-1", "--brightness", str(adjusted_level)]) | |
save_level(level=adjusted_level) | |
if __name__ == "__main__": | |
METHOD = sys.argv[1] if len(sys.argv) > 1 else "up" | |
adjust_level(method=METHOD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment