Skip to content

Instantly share code, notes, and snippets.

@Vindaar
Last active October 15, 2018 12:59
Show Gist options
  • Save Vindaar/e161bc24db4a286e49297cce81c9a20c to your computer and use it in GitHub Desktop.
Save Vindaar/e161bc24db4a286e49297cce81c9a20c to your computer and use it in GitHub Desktop.
import strutils
proc parseFile(filename: string): int =
result = filename.staticRead.strip.parseInt
# define the brightness file
const file = "/sys/class/backlight/intel_backlight/brightness"
# define thresholds for on and off
const brightnessOff = 0
const offThreshold = 20
const brightnessDefault = 12000
const oldBrightnessFile = "/home/basti/scripts/old_brightness"
# read the current brightness
let brightness = parseFile file
# get the input argument. Either "on" or "off"
let args = paramCount()
let toOn = paramStr(args)
if toOn notin @["on", "off", "toggle"]:
echo toOn
quit()
elif toOn == "on":
# write brightness from current_brightness
let oldBrightness = parseFile oldBrightnessFile
elif toOn == "toggle":
# write the current `brightness` to `old_brightness` if
# it's different from 0, then write 0. Otherwise read
# `old_brightness` and write that
if brightness < offThreshold:
# display is off -> read current_brightness
let oldBrightness = parseFile oldBrightnessFile
if oldBrightness > offThreshold:
# simply write `oldBrightness`
echo "Writing oldBrightness ", oldBrightness, " to file ", file
writeFile(file, $oldBrightness)
else:
# write default brightness
echo "Writing brightnessDefault ", brightnessDefault, " to file ", file
writeFile(file, $brightnessDefault)
elif brightness > offThreshold:
# means we turn off display
# first write current brightness file
echo "Writing brightness ", brightness, " to oldBrightnessFile ", oldBrightnessFile
writeFile(oldBrightnessFile, $brightness)
# now write brightness zero
echo "Writing brightnessOff ", brightnessOff, " to file ", file
writeFile(file, $brightnessOff & "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment