Skip to content

Instantly share code, notes, and snippets.

@zimolzak
Last active February 22, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimolzak/ad9d978ded7156c9a3cc19e113cc234c to your computer and use it in GitHub Desktop.
Save zimolzak/ad9d978ded7156c9a3cc19e113cc234c to your computer and use it in GitHub Desktop.
Control Linux screen brightness using keys in a Curses terminal
#!/usr/bin/env python3
import curses
max_b = int(open('/sys/class/backlight/radeon_bl0/max_brightness', 'r').read())
min_b = 40
columns = 70
def main(stdscr):
stdscr.clear()
k = 'x'
stdscr.addstr(1, 0, 'q to quit')
while k not in 'qQ':
brightness_file = open('/sys/class/backlight/radeon_bl0/brightness', 'w+')
current = brightness_file.read()
stdscr.addstr(3, 0, current)
baseline = int(min_b / max_b * columns)
filled = int(int(current) / max_b * columns) - baseline
empty = columns - filled - baseline
stdscr.addstr(5, 0, '|' * baseline + '*' * filled + '-' * empty)
k = stdscr.getkey()
if k in 'uUkKpP' or k == 'KEY_UP':
new_brightness = int(int(current) * 1.1)
elif k in 'dDjJnN' or k == 'KEY_DOWN':
new_brightness = int(int(current) * (10.0/11))
stdscr.refresh()
if new_brightness < min_b:
new_brightness = min_b
if new_brightness > max_b:
new_brightness = max_b
brightness_file.write(str(new_brightness) + '\n')
brightness_file.close()
curses.wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment