summaryrefslogtreecommitdiff
path: root/ar/.local/bin/monitorbright
blob: 2de2e5d00afbb59754df3f561b802104b6ff045d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/sh

monitor=$(xrandr --query | grep -i '\sconnected' | grep '[0-9]x[0-9]' | grep -i 'primary' | cut -d ' ' -f1)
[ -z "$monitor" ] && monitor=$(xrandr --query | grep -i '\sconnected' | grep '[0-9]x[0-9]' | cut -d ' ' -f1)
case "$monitor" in
*DP* | *HDMI*)
  current_brightness=$(xrandr --verbose | grep -i "^$monitor connected" -A5 | grep -i "Brightness:" | cut -d ' ' -f2)
  [ -z "$current_brightness" ] && exit 1
  if [ "$#" -eq 2 ]; then
    scale_change=$(echo "$2 / 100" | bc -l)
    case "$1" in
    "-inc") new_brightness=$(echo "$current_brightness + $scale_change" | bc -l) ;;
    "-dec") new_brightness=$(echo "$current_brightness - $scale_change" | bc -l) ;;
    *) echo "Invalid argument $1. Use -inc or -dec." && exit 1 ;;
    esac
    new_brightness=$(echo "if ($new_brightness > 1) 1 else if ($new_brightness < 0) 0 else $new_brightness" | bc -l)
    xrandr --output "$monitor" --brightness "$new_brightness"
    current_brightness=$(echo "$new_brightness * 100" | bc -l)
  else
    current_brightness=$(echo "$current_brightness * 100" | bc -l)
  fi
  printf "🪟%.0f%%\n" "$current_brightness"
  ;;
esac