Skip to content

Instantly share code, notes, and snippets.

@atondwal
Last active August 16, 2018 06:15
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 atondwal/700285755a32aa49a48fa4f622a37639 to your computer and use it in GitHub Desktop.
Save atondwal/700285755a32aa49a48fa4f622a37639 to your computer and use it in GitHub Desktop.
#!/usr/bin/env nix-shell
#! nix-shell -i runhaskell -p "haskellPackages.ghcWithPackages (pkgs: [pkgs.shqq])"
{-# LANGUAGE QuasiQuotes #-}
import System.ShQQ (sh)
import System.Environment (getArgs)
parseOp :: Num a => Char -> a -> a -> a
parseOp '+' = (+)
parseOp '-' = (-)
parseOp '=' = flip const
parseOp op = error $ "invalid operation: " ++ [op]
main = do
actBrightness <- read <$> readFile "/sys/class/backlight/intel_backlight/actual_brightness"
maxBrightness <- read <$> readFile "/sys/class/backlight/intel_backlight/max_brightness"
let curBrightness = actBrightness / maxBrightness
args <- concat <$> getArgs
if args == "-get"
then print $ round $ curBrightness * 100
else do
(op:magn) <- [sh| echo ${args} | sed -e 's/-inc/+/g' -e 's/-dec/-/g' -e 's/-set/=/g'|]
let newBrightness = round $ (maxBrightness *) $ parseOp op curBrightness (read magn/100) :: Int
[sh| gksudo -\\- bash -c "echo ${newBrightness} > /sys/class/backlight/intel_backlight/brightness" |]
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment