Last active
August 26, 2024 19:57
-
-
Save maikelthedev/996596e7bcb1a23fbaa952b45ba43202 to your computer and use it in GitHub Desktop.
Fish shell function to remind you, you're on battery when typing "code" and offering you to load neovim instead of code to save battery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function code | |
# Determine the path to the code command | |
set path_is (command -v code) | |
# Check if the system is on battery | |
if is_on_battery | |
# Ask the user if they are sure they want to open VSCode | |
if ask_sure | |
# User confirmed; open VSCode with the provided argument | |
$path_is $argv | |
else | |
# User declined; open the file in nvim instead | |
nvim $argv | |
end | |
else | |
# Not on battery; directly open VSCode | |
$path_is $argv | |
end | |
end | |
# On nixos you'll need the package "dialog" | |
function ask_sure | |
# Prompt the user with a dialog box | |
command dialog --default-button "no" \ | |
--no-label "No" \ | |
--yes-label "Sure" \ | |
--yesno "Do you really want to open VSCode? You're on battery!" 0 0 | |
end | |
function is_on_battery | |
# Check if the laptop is discharging (on battery) | |
command upower -i (command upower -e | grep 'BAT') | grep -q "state:\s*discharging" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use fish shell all you need to do is to save this as
~/.config/fish/functions/code.fish
Then you can use
code somefile
orcode somefolder
the same way you used to before.