From anonymous, 5 Months ago, written in Python.
Embed
  1. #!/usr/bin/python
  2.  
  3. import subprocess
  4. import argparse
  5. import sys
  6.  
  7.  
  8. STEP = 0.05
  9.  
  10.  
  11. def get_brightness():
  12.     proc = subprocess.run(['xrandr', '--verbose'], universal_newlines=True,
  13.                           check=True, stdout=subprocess.PIPE)
  14.     for line in proc.stdout.splitlines():
  15.         fields = line.split()
  16.         if fields and fields[0] == 'Brightness:':
  17.             return float(fields[1])
  18.  
  19.  
  20. def get_output():
  21.     proc = subprocess.run(['xrandr', '--listmonitors'],
  22.                           universal_newlines=True, check=True,
  23.                           stdout=subprocess.PIPE)
  24.     for line in proc.stdout.splitlines():
  25.         if line.startswith(' '):
  26.             name = line.split()[3]
  27.             if name.startswith('eDP'):
  28.                 return name
  29.     raise Exception("Output not found")
  30.  
  31.  
  32. def validate_arg(arg):
  33.     if arg in ['up', 'down']:
  34.         return arg
  35.     return float(arg)
  36.  
  37.  
  38. def main():
  39.     parser = argparse.ArgumentParser()
  40.     parser.add_argument('action', type=validate_arg)
  41.     action = parser.parse_args().action
  42.  
  43.     output = get_output()
  44.  
  45.     brightness = get_brightness()
  46.     if action == 'up':
  47.         if brightness == 1.0:
  48.             sys.exit(0)
  49.         brightness += STEP
  50.     elif action == 'down':
  51.         if brightness == 0:
  52.             sys.exit(0)
  53.         brightness -= STEP
  54.     else:
  55.         brightness = action
  56.  
  57.     subprocess.run(['xrandr', '--output', output, '--brightness',
  58.                     str(brightness)], check=True)
  59.  
  60.  
  61. if __name__ == '__main__':
  62.     main()
  63.