├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # brightnessctl 2 | 3 | Small tool for linux to adjust screen brightness. 4 | 5 | ``` 6 | npm install -g brightnessctl 7 | ``` 8 | 9 | Tested on the Dell XPS13 running Arch. 10 | 11 | ## Usage 12 | 13 | To adjust the screen brightness simply run 14 | 15 | ``` 16 | sudo brightnessctl 17 | ``` 18 | 19 | It will print an interactive slider that looks like this: 20 | 21 | ``` 22 | Use and to adjust brightness 23 | [####### ] 15% 24 | ``` 25 | 26 | You can also adjust the brightness with a cli argument like so 27 | 28 | ``` 29 | sudo brightnessctl 50 # sets the brightness to 50% 30 | ``` 31 | 32 | PRs that add support for more distros welcome. 33 | 34 | ## License 35 | 36 | MIT 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | if (process.getuid()) { 4 | console.error('Run as root') 5 | process.exit(1) 6 | } 7 | 8 | var diffy = require('diffy')() 9 | var input = require('diffy/input')() 10 | var fs = require('fs') 11 | var path = require('path') 12 | 13 | var FOLDER = '/sys/class/backlight/intel_backlight' 14 | var BRIGHTNESS_FILE = path.join(FOLDER, 'brightness') 15 | var MAX_BRIGHTNESS_FILE = path.join(FOLDER, 'max_brightness') 16 | var MAX = readInt(MAX_BRIGHTNESS_FILE) 17 | 18 | var pct = Math.floor(100 * readInt(BRIGHTNESS_FILE) / MAX) 19 | var inc = 5 20 | 21 | var ws = fs.createWriteStream(BRIGHTNESS_FILE) 22 | 23 | input.on('right', function () { 24 | pct += inc 25 | if (pct > 100) pct = 100 26 | update() 27 | }) 28 | 29 | input.on('left', function () { 30 | pct -= inc 31 | if (pct < 0) pct = 0 32 | update() 33 | }) 34 | 35 | diffy.render(render) 36 | 37 | if (/^\d+?$/.test(process.argv[2])) { 38 | pct = parseInt(process.argv[2], 10) 39 | update() 40 | ws.end() 41 | ws.on('close', exit) 42 | } 43 | 44 | function exit () { 45 | process.exit(0) 46 | } 47 | 48 | function readInt (file) { 49 | return parseInt(fs.readFileSync(file, 'ascii'), 10) 50 | } 51 | 52 | function update () { 53 | ws.write('' + Math.max(1, Math.floor(pct / 100 * MAX)) + '\n') 54 | diffy.render() 55 | } 56 | 57 | function times (str, n) { 58 | var res = '' 59 | while (n--) res += str 60 | return res 61 | } 62 | 63 | function render () { 64 | var wid = Math.max(0, process.stdout.columns - 8) 65 | var widPct = Math.floor(wid * pct / 100) 66 | var slider = '[' + times('#', widPct) + times(' ', wid - widPct) + ']' 67 | 68 | return 'Use and to adjust brightness\n' + 69 | slider + ' ' + (pct < 10 ? ' ' : (pct < 100 ? ' ' : '')) + pct + '%\n' 70 | } 71 | 72 | function noop () {} 73 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "brightnessctl", 3 | "version": "1.1.0", 4 | "description": "Small tool for linux to adjust screen brightness", 5 | "main": "index.js", 6 | "dependencies": { 7 | "diffy": "^1.0.1" 8 | }, 9 | "devDependencies": { 10 | "standard": "^10.0.3" 11 | }, 12 | "bin": { 13 | "brightnessctl": "./index.js" 14 | }, 15 | "scripts": { 16 | "test": "standard" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mafintosh/brightnessctl.git" 21 | }, 22 | "author": "Mathias Buus (@mafintosh)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/mafintosh/brightnessctl/issues" 26 | }, 27 | "homepage": "https://github.com/mafintosh/brightnessctl" 28 | } 29 | --------------------------------------------------------------------------------