LoginSignup
1
0

More than 5 years have passed since last update.

バックライト輝度を変えるfishコマンド

Last updated at Posted at 2018-01-10

なぜ

ノートPCの輝度調節用キーが反応しなくて困ってた(acpi_listenしてみたら認識されてない)

なに

fish shellでfunction(コマンド)を作成して気軽に輝度を変えたい
キーボード・ショートカットにも登録できるようになる

これ

adjust_backlight.fish
function adjust_backlight --argument direction percentage
    # echo 400 | sudo tee /sys/class/backlight/intel_backlight/brightness をしたい

    set -l now (cat "/sys/class/backlight/intel_backlight/brightness")
    set -l max (cat "/sys/class/backlight/intel_backlight/max_brightness")
    set -l min 0
    set -l cent (math -s2 (math $max - $min) / 100.0)

    if string match -ar "[0-9]+" $percentage >/dev/null

        set -l new_p $now
        switch $direction
            case "INC" "Inc" "inc" "Increment" "increment" "UP" "Up" "up" "--inc" "-i" "-I" "--up" "-u" "-U"
                set -l temp (math -s0 "($now + ($cent * $percentage) / 1) / 1")
                if test (math $temp - $max) -gt 0
                    set temp $max
                end
                set new_p $temp
            case "DEC" "Dec" "dec" "Decrement" "decrement" "DOWN" "Down" "down" "--dec" "--down" "-d" "-D"
                set -l temp (math -s0 "($now - ($cent * $percentage) / 1) / 1")
                if test (math $temp - $min) -le 0
                    set temp $min
                end
                set new_p $temp
            case "SET" "Set" "set" "--set" "-s"
                set new_p (math -s0 "$cent * $percentage / 1")
            case *
                set new_p $now
        end

        echo "set to $new_p"
        echo $new_p | sudo tee "/sys/class/backlight/intel_backlight/brightness" >/dev/null
    else
        echo "max: $max"
        echo "now: $now"
        echo "usage: adjust_backlight [inc | dec] PERCENTAGE"
    end
end

おんなじようなことを fishのshebangつけたシェルスクリプトにしてNOPASSWDにすればキーボードショートカットとかから叩ける

所感

function--arguments が値入れなくてもエラー出さないし便利
あと変な記号少なくて頭に優しい

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0