Skip to content

Instantly share code, notes, and snippets.

@atondwal
Created April 14, 2021 20:24
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/1fb835b9d5d577853f56f4e95d764fc8 to your computer and use it in GitHub Desktop.
Save atondwal/1fb835b9d5d577853f56f4e95d764fc8 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" |]
writeFile "/sys/class/backlight/intel_backlight/brightness" (show newBrightness)
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment