add script to change power profile

This commit is contained in:
Adithya 2023-03-16 23:36:17 +05:30
parent 2d84bd3201
commit 27a77c6ddd
No known key found for this signature in database
GPG key ID: 0C7F35F4F821290F
3 changed files with 120 additions and 62 deletions

View file

@ -1,67 +1,114 @@
{ pkgs, ... }:
{ osConfig, pkgs, ... }:
{
xdg.configFile."scripts/power_menu.sh" = {
text = ''
#!/bin/sh
xdg.configFile = {
"scripts/power_menu.sh" = {
executable = true;
text = ''
#!/bin/sh
set -eu
set -eu
chpower() {
case "$1" in
"")
;;
Shutdown)
exec systemctl poweroff
;;
Reboot)
exec systemctl reboot
;;
Hibernate)
exec systemctl hibernate
chpower() {
case "$1" in
"")
;;
Shutdown)
exec systemctl poweroff
;;
Reboot)
exec systemctl reboot
;;
Hibernate)
exec systemctl hibernate
;;
Logout)
swaymsg exit
;;
*)
${pkgs.libnotify}/bin/notify-send -t 1500 -u low "Invalid Option"
;;
esac
}
OPTIONS="Shutdown\nReboot\nHibernate\nLogout"
chpower "$(printf "%b" "$OPTIONS" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Power Menu")"
'';
};
"scripts/volume_up.sh" = {
executable = true;
text = ''
#!/bin/sh
set -eu
${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SINK@ 0
[ $(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk -F': ' '{print $2}' | sed 's/\.//') -lt 100 ] && ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+
'';
};
"scripts/tmux_sessions.sh" = {
executable = true;
text = ''
#!/bin/sh
set -eu
SESSION="$(${pkgs.tmux}/bin/tmux list-sessions -F "(#{session_attached}) #S [#{pane_current_command} in #{pane_current_path}] #{pane_title}" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Running TMUX Sessions" | awk '{print $2}')"
case "$SESSION" in
"")
;;
*)
${pkgs.kitty}/bin/kitty ${pkgs.tmux}/bin/tmux -u attach-session -dEt "$SESSION"
;;
esac'';
};
"scripts/power_profile.sh" =
let
sudo = "/run/wrappers/bin/sudo";
cpupower = "${osConfig.boot.kernelPackages.cpupower}/bin/cpupower";
powerprofilesctl = "${pkgs.power-profiles-daemon}/bin/powerprofilesctl";
in {
executable = true;
text = ''
#!/bin/sh
set -eu
POWER_PROFILE_FILE="$HOME/.cache/power_profile"
POWER_PROFILE="powersave"
if [ -f "$POWER_PROFILE_FILE" ]; then
POWER_PROFILE="$(<$POWER_PROFILE_FILE)"
fi
case "$1" in
"toggle")
if [ "$POWER_PROFILE" == "powersave" ]; then
${sudo} ${cpupower} frequency-set --governor performance > /dev/null
${powerprofilesctl} set performance
POWER_PROFILE="performance"
elif [ "$POWER_PROFILE" == "performance" ]; then
${sudo} ${cpupower} frequency-set --governor powersave > /dev/null
${powerprofilesctl} set power-saver
POWER_PROFILE="powersave"
fi
echo $POWER_PROFILE > $POWER_PROFILE_FILE
;;
Logout)
swaymsg exit
;;
*)
${pkgs.libnotify}/bin/notify-send -t 1500 -u low "Invalid Option"
;;
esac
}
"icon")
if [ "$POWER_PROFILE" == "powersave" ]; then
echo "󰌪"
elif [ "$POWER_PROFILE" == "performance" ]; then
echo "󰓅"
fi
;;
*)
;;
esac
OPTIONS="Shutdown\nReboot\nHibernate\nLogout"
chpower "$(printf "%b" "$OPTIONS" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Power Menu")"
'';
executable = true;
};
xdg.configFile."scripts/volume_up.sh" = {
text = ''
#!/bin/sh
set -eu
${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SINK@ 0
[ $(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk -F': ' '{print $2}' | sed 's/\.//') -lt 100 ] && ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+
'';
executable = true;
};
xdg.configFile."scripts/tmux_sessions.sh" = {
text = ''
#!/bin/sh
set -eu
SESSION="$(${pkgs.tmux}/bin/tmux list-sessions -F "(#{session_attached}) #S [#{pane_current_command} in #{pane_current_path}] #{pane_title}" | sort | ${pkgs.rofi-wayland}/bin/rofi -dmenu -p "Running TMUX Sessions" | awk '{print $2}')"
case "$SESSION" in
"")
;;
*)
${pkgs.kitty}/bin/kitty ${pkgs.tmux}/bin/tmux -u attach-session -dEt "$SESSION"
;;
esac'';
executable = true;
'';
};
};
}

View file

@ -1,4 +1,4 @@
{ pkgs, ... }:
{ config, pkgs, ... }:
{
programs.waybar.enable = true;
@ -12,7 +12,7 @@
height = 28;
modules-left = [ "sway/workspaces" "sway/window" "sway/mode" ];
modules-center = [ ];
modules-right = [ "tray" "idle_inhibitor" "network" "bluetooth" "pulseaudio" "backlight" "battery" "clock" ];
modules-right = [ "tray" "custom/power_profile" "idle_inhibitor" "network" "bluetooth" "pulseaudio" "backlight" "battery" "clock" ];
"sway/mode" = {
"format" = "{}";
};
@ -88,6 +88,16 @@
"tray" = {
"spacing" = 4;
};
"custom/power_profile" =
let
script = "${config.xdg.configHome}/scripts/power_profile.sh";
in {
exec = "${script} icon";
on-click = "${script} toggle";
format = "{}";
tooltip = false;
interval = "10";
};
};
};
}

View file

@ -58,7 +58,8 @@ window#waybar.solo {
#tray,
#mode,
#idle_inhibitor,
#bluetooth {
#bluetooth,
#custom-power_profile {
padding: 0 10px;
color: @foreground;
background-color: @background;