Created
January 11, 2017 15:41
-
-
Save senior/54d2592513afaf24464c917cbf13f980 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/guile \ | |
-e main -s | |
!# | |
(use-modules ((ice-9 rdelim) | |
#:prefix dlm:)) | |
(use-modules ((ice-9 getopt-long) | |
#:prefix gl:)) | |
(define (current-max) | |
(call-with-input-file "/sys/class/backlight/intel_backlight/max_brightness" | |
(lambda (x) | |
(string->number (dlm:read-line x))))) | |
(define (current-brightness) | |
(call-with-input-file "/sys/class/backlight/intel_backlight/brightness" | |
(lambda (x) | |
(string->number (dlm:read-line x))))) | |
(define (step-increment pct max-brightness) | |
(round (/ (current-max) pct))) | |
(define (compute-change change-pct) | |
(let* ((max (current-max)) | |
(brightness (current-brightness)) | |
(maybe-increase-amount (+ brightness (step-increment change-pct max)))) | |
(cond | |
((>= brightness max) | |
0) | |
((>= maybe-increase-amount max) | |
max) | |
((<= maybe-increase-amount 50) | |
50) | |
(else maybe-increase-amount)))) | |
(define (apply-brightness-change change-value) | |
(call-with-output-file "/sys/class/backlight/intel_backlight/brightness" | |
(lambda (out) | |
(write change-value out)))) | |
(define options-spec | |
'((help (single-char #\h) (value #f)) | |
(get (single-char #\g) (value #f)) | |
(dec (single-char #\d) (value #t)) | |
(inc (single-char #\i) (value #t)))) | |
(define (print-help) | |
(display "\ | |
gbacklight [options] | |
-h, --help Display this help | |
-g, --get Display the current brightness | |
-i, --inc <percentage> Increment by percentage | |
-d, --dec <percentage> Decrement by percentage | |
")) | |
(define (print-current) | |
(display | |
(string-append | |
(number->string | |
(round (* 100 (/ (current-brightness) (current-max))))) | |
"\n"))) | |
(define (main args) | |
(let* ((options (gl:getopt-long (command-line) options-spec)) | |
(help? (gl:option-ref options 'help #f)) | |
(get? (gl:option-ref options 'get #f)) | |
(inc-pct (gl:option-ref options 'inc #f)) | |
(dec-pct (gl:option-ref options 'dec #f))) | |
(cond | |
(help? | |
(print-help)) | |
(get? | |
(print-current)) | |
(inc-pct | |
(apply-brightness-change (compute-change (string->number inc-pct))) | |
(print-current)) | |
(dec-pct | |
(apply-brightness-change (compute-change (- (string->number dec-pct)))) | |
(print-current))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment