Created
June 19, 2019 02:26
-
-
Save FreeFull/1e5873f0b13ec291158176db9d7c071c to your computer and use it in GitHub Desktop.
HN backlight code
This file contains 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/python3 | |
import sys | |
with open('/sys/class/backlight/intel_backlight/max_brightness') as max_brightness_file: | |
max_brightness = int(max_brightness_file.readline()) | |
with open('/sys/class/backlight/intel_backlight/brightness', 'r') as brightness_file: | |
brightness = int(brightness_file.readline()) | |
default_increment = max_brightness / 50 | |
with open('/sys/class/backlight/intel_backlight/brightness', 'w') as brightness_file: | |
if len(sys.argv) == 1: | |
print('{} / {}: {:%}'.format(brightness, max_brightness, brightness / max_brightness)) | |
elif len(sys.argv) == 2: | |
arg = sys.argv[1] | |
if arg == '+': | |
if brightness <= 30: | |
brightness += 1 | |
else: | |
brightness += default_increment | |
elif arg == '-': | |
if brightness <= 30: | |
brightness -= 1 | |
else: | |
brightness -= default_increment | |
elif arg.isdigit(): | |
brightness = int(arg) | |
brightness = round(brightness) | |
brightness = max(1, brightness) | |
brightness = min(max_brightness, brightness) | |
brightness_file.write(str(brightness)) |
This file contains 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/python | |
from sys import argv | |
if len(argv) != 2: | |
exit(1) | |
arg = argv[1] | |
brightness = 0 | |
max_brightness = 0 | |
with open('/sys/class/backlight/intel_backlight/brightness') as f: | |
brightness = int(f.read().strip()) | |
with open('/sys/class/backlight/intel_backlight/max_brightness') as f: | |
max_brightness = int(f.read().strip()) | |
def clamp(x): | |
if x < 1: | |
return 1 | |
if x > max_brightness: | |
return max_brightness | |
return x | |
if arg == "+": | |
if brightness < 40: | |
brightness += 1 | |
else: | |
brightness += 10 | |
elif arg == "-": | |
if brightness < 40: | |
brightness -= 1 | |
else: | |
brightness -= 10 | |
else: | |
brightness = int(arg) | |
brightness = clamp(brightness) | |
with open('/sys/class/backlight/intel_backlight/brightness', 'w') as f: | |
f.write(str(brightness)) |
This file contains 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
use std::env; | |
use std::fs::{File, OpenOptions}; | |
use std::io::{Read, Write, Seek, SeekFrom}; | |
use std::cmp::{min, max}; | |
fn main() -> Result<(), Box<std::error::Error>> { | |
if let Some(command) = env::args().nth(1) { | |
let max_brightness = { | |
let mut file = File::open("/sys/class/backlight/intel_backlight/max_brightness")?; | |
let mut string = String::new(); | |
file.read_to_string(&mut string)?; | |
string.trim().parse::<i32>()? | |
}; | |
let mut brightness_file = OpenOptions::new() | |
.read(true) | |
.write(true) | |
.open("/sys/class/backlight/intel_backlight/brightness")?; | |
let mut brightness = { | |
let mut string = String::new(); | |
brightness_file.read_to_string(&mut string)?; | |
brightness_file.seek(SeekFrom::Start(0))?; | |
string.trim().parse::<i32>()? | |
}; | |
match &*command { | |
"+" => { | |
if brightness < 40 { | |
brightness += 1; | |
} else { | |
brightness += 10; | |
} | |
} | |
"-" => { | |
if brightness < 40 { | |
brightness -= 1; | |
} else { | |
brightness -= 10; | |
} | |
} | |
_ => { | |
brightness = command.trim().parse::<i32>()?; | |
} | |
} | |
brightness = max(1, brightness); | |
brightness = min(max_brightness, brightness); | |
write!(brightness_file, "{}", brightness); | |
} else { | |
std::process::exit(1); | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment