Skip to content

Instantly share code, notes, and snippets.

@lacostenycoder
Forked from lagerone/screen-brightness
Created December 24, 2021 09:53

Revisions

  1. @lagerone lagerone renamed this gist Aug 25, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion oled-screen-brightness → screen-brightness
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ from typing import Literal
    logging.basicConfig(level=logging.DEBUG)

    FILE_PATH = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), ".oled-screen-brightness"
    os.path.dirname(os.path.realpath(__file__)), ".screen-brightness"
    )


  2. @lagerone lagerone renamed this gist Aug 25, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion screen-brightness → oled-screen-brightness
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ from typing import Literal
    logging.basicConfig(level=logging.DEBUG)

    FILE_PATH = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), ".screen-brightness"
    os.path.dirname(os.path.realpath(__file__)), ".oled-screen-brightness"
    )


  3. @lagerone lagerone created this gist Aug 25, 2021.
    52 changes: 52 additions & 0 deletions screen-brightness
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/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)