Skip to content

Instantly share code, notes, and snippets.

@maikelthedev
Last active August 26, 2024 19:57
Show Gist options
  • Save maikelthedev/996596e7bcb1a23fbaa952b45ba43202 to your computer and use it in GitHub Desktop.
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
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
@maikelthedev
Copy link
Author

maikelthedev commented Aug 26, 2024

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 somefileor code somefolderthe same way you used to before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment