Skip to content

Instantly share code, notes, and snippets.

@py7hon
Created September 23, 2021 16:39
Show Gist options
  • Save py7hon/4808d54064974649868b74187f40ae2b to your computer and use it in GitHub Desktop.
Save py7hon/4808d54064974649868b74187f40ae2b to your computer and use it in GitHub Desktop.
Kecerahan (Brightness control) Bash version
#!/bin/bash
# SPDX-License-Identifier: MIT
# Copyright © 2019-present Iqbal Rifai
# ------------------------------------------------------------------
# MIT License
# -----------
#
# Copyright (c) 2019-present Iqbal Rifai (https://iqbalrifai.eu.org)
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------
# --------------------------------------------
# Usage kecerahan [options....] <value>
# -------------------------------------
#
# options:
# -h, --help print this help
#
# -b, --brightness set level brightness
#
# <value> range:
# 0.1-1.0 : 1%-100%
# 1.0-5.0 : 100%-500%
# --------------------------------------------
RED=`tput setaf 1`
WHITE=`tput setaf 7`
GREEN=`tput setaf 2`
NC=`tput sgr0`
if [[ $EUID -ne 0 ]]; then
echo "${RED}Error${NC}${WHITE}:${NC} ${WHITE}This script must be run as root${NC}"
exit 1
fi
bright(){
isnumber='^[0-9]+(\.[0-9]+)$'
if [[ $brightness =~ $isnumber ]]; then
xrandr --listmonitors | grep "^ " | cut -f 6 -d' ' | \
xargs --replace=MONITOR xrandr --output MONITOR --brightness $brightness
echo "${GREEN}Success${NC} ${WHITE}set brightness to $brightness${NC}"
else
echo "${RED}Error${NC}${WHITE}:${NC} ${WHITE}Only Number is allowed${NC}" >&2;
exit 1
fi
}
helpm() {
name="$(basename $0)"
echo "Usage: $name [options....] <value>"
echo
echo "options:"
echo " -h, --help print this help"
echo
echo " -b, --brightness set level brightness"
echo
echo " <value> range:"
echo " 0.1-1.0 : 1%-100%"
echo " 1.0-5.0 : 100%-500%"
echo
exit 1
}
case "$1" in
-b | --brightness)
brightness="$2"
bright
;;
-h | --help)
helpm
exit 1
;;
--)
echo "${RED}Error${NC}${WHITE}:${NC} ${WHITE}Unknown option: $1${NC}" >&2
exit 1
;;
-*)
echo "${RED}Error${NC}${WHITE}:${NC} ${WHITE}Unknown option: $1${NC}" >&2
exit 1
;;
*)
helpm
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment