Code Monkey home page Code Monkey logo

zlong_alert.zsh's Introduction

zlong_alert.zsh

zlong_alert.zsh will send a desktop notification and sound a bell to alert you when a command that has taken a long time (default: 15 seconds) has completed.

Desktop notifications are sent using notify-send on Linux and using alerter on MacOS.


Installation

Pre-requisite for MacOS only

Ensure that you downloaded the alerter binary from here, have placed it in your PATH, and given the file executable permissions before continuing with any of the installation methods.

zplug

zplug "kevinywlui/zlong_alert.zsh"

Oh My Zsh

  1. Download the plugin

    a. Clone into $ZSH_CUSTOM/plugins/zlong_alert.

    or

    b. if on archlinux you can use this aur package

  2. Add zlong_alert to plugins in .zshrc.

Zim

Add in your ~/.zimrc:

zmodule "kevinywlui/zlong_alert.zsh" --name zlong_alert

Manual

This script just needs to be sourced so add this to your .zshrc:

source /path/to/zlong_alert.zsh

Configuration

There are 7 variables you can set that will alter the behavior this script.

  • zlong_duration (default: 15): number of seconds that is considered a long duration.
  • zlong_ignore_cmds (default: "vim ssh"): commands to ignore (do not notify).
  • zlong_ignore_pfxs (default: "sudo time"): prefixes to ignore (consider command in argument).
  • zlong_send_notifications (default: true): whether to send notifications.
  • zlong_terminal_bell (default: true): whether to enable the terminal bell.
  • zlong_ignorespace (default: false): whether to ignore commands with a leading space
  • zlong_message (default: '"Done: $cmd Time: $ftime"'): define a custom message to display

For example, adding the following anywhere in your .zshrc

zlong_send_notifications=false
zlong_duration=2
zlong_ignore_cmds="vim ssh pacman yay"

will alert you, without sending a notification, if a command has lasted for more than 2 seconds, provided that the command does not start with any of vim ssh pacman yay.

zlong_message

zlong_message requires very specific syntax in order to function correctly. Arguments passed must be wrapped in single quotes and then doubles quotes in order for the variables to be passed in correctly to the evaluation function. Currently, the variables $cmd and $ftime are available to be included in your zlong_message definition. Some notification clients (i.e. notify-send) allow both a heading and a body message to be passed. Examples of how to do so are below:

zlong_message='"Done: $cmd Time: $ftime"'
zlong_message='"Finished ($ftime)" "$cmd"'

Changelog

See CHANGELOG

Credit

This script is the result of me trying to understand and emulate this gist: https://gist.github.com/jpouellet/5278239 My version fixes some things (possibly bugs?) that I did not like about the original version.

zlong_alert.zsh's People

Contributors

chrisrhayden avatar dakshin-k avatar deseven avatar fdelavier avatar hankyusa avatar kevinywlui avatar omernaveedxyz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

zlong_alert.zsh's Issues

Add to the AUR

This isn't really an issue per say, but just want to get into contact with @kevinywlui .

First of all, great plugin! I love it! I use Arch Linux and like to add everything on my system through my package manager, so I wrote a PKGBUILD for Arch and put it on the AUR so anyone can use it (https://aur.archlinux.org/packages/zlong-alert-git). I'm listed as the maintainer there. If you want to take it over just let me know, it just tracks this git repo.

Feel free to close this issue whenever you want.

Commands from remote shell

Hello! Nice repo and functionality!

I was wondering if there could be any support for temrminal sessions running on remote servers, and if it would be too complicated to make work.

I am willing to make a PR if it's feasible.

Any ideas?

commands that start with a space (or spaces) do not work

# does work
for i in {0..15}; do sleep 1; echo "$i"; done

# does not work
    for i in {0..15}; do sleep 1; echo "$i"; done

Commands that start with a space get ignored because of how the cut command on line 43 works

local cmd_head=$(echo $zlong_last_cmd | cut -d ' ' -f 1)

switching to awk '{print $1}' should work but space is usually a way to ignore things so maybe adding an option could be cool.

echo 'command foo bar' | awk '{print $1}'
# -> command

echo '    for i in {0..15}; do sleep 1; echo "$i"; done' | awk '{print $1}'
# -> for

echo ' command  foo    bar thing' | awk '{print $1}'
# -> command

Add support for MacOS notifications

Hi, I've been using this plugin for a while now and find it very useful, however the notify_send package works only on Linux and not on MacOS.

I have made the required code changes on my machine and it has been working well for the past few months. So I would like to raise a PR to enable this plugin to officially support MacOS notifications.

I wanted to confirm that this is something the maintainer(s) of this project are interested in adding before going ahead with the PR. So please do let me know if I can start working on this.

Thanks a lot :)

Note: I would be adding a dependency on the alerter library to accomplish this

clean up the plugin, reduce the global variables

It would be nice to not pollute the global env so much. I think we should check the variables inside the functions they are used.

We should also clean up some of the small stuff like not quoting variables. I know its not always necessary in zsh but I think it makes some things confusing and has cased issues in the past.

The style should also be consistent. I'm fine with any style just as long as the plugin is consistent.

Incompatibility with custom config setopt globsubst

Hi,

I notice an issue when using zlong_alert plugin with my existing zsh customizations.

Indeed, in my .zshrc I typically use this configuration :
setopt globsubst

Problem: this option break zlong_alert when using "eval" build-in command.

Without option globsubst :

% unsetopt globsubst

% eval $(echo date +"%D")
11/01/22

=> OK

With option globsubst :

% setopt globsubst

% eval $(echo date +"%D")
11/01/22
zlong_alert_post:3: missing end of string

% date +"%D"
11/01/22

=> eval command fails (regardless the duration of the command execution)

Maybe zlong_alert should use something like "emulate -L zsh" ?

According to man zshoptions :

       LOCAL_OPTIONS <K>
              If this option is set at the point of return from a shell  func‐
              tion, most options (including this one) which were in force upon
              entry to the function are restored; options  that  are  not  re‐
              stored  are PRIVILEGED and RESTRICTED.  Otherwise, only this op‐
              tion, and the LOCAL_LOOPS, XTRACE and  PRINT_EXIT_VALUE  options
              are  restored.   Hence  if  this  is explicitly unset by a shell
              function the other options in force at the point of return  will
              remain  so.   A shell function can also guarantee itself a known
              shell configuration with a formulation like  `emulate  -L  zsh';
              the -L activates LOCAL_OPTIONS.

Thanks

zlong_alert could hang after some special commands

In oh-my-zsh, there's a global alias alias -g ....='../../..' which allows you to go back to the 3rd parent folder by typing ..... However, this command will make zlong_alert enter an infinite loop between these lines

while [[ -n "$last_cmd_no_pfx" && -z "$no_pfx" ]]; do
cmd_head="${last_cmd_no_pfx%% *}"
if [[ "$zlong_ignore_pfxs" =~ "(^|[[:space:]])${(q)cmd_head}([[:space:]]|$)" ]]; then
last_cmd_no_pfx="${last_cmd_no_pfx#* }"

given zlong_ignore_pfxs="sudo time" (the default) and cmd_head=.... isn't properly quoted for regex.

I'm not sure which way we should go to fix this. Maybe properly quote cmd_head? Or just use some heuristics, like don't check for prefixes if there isn't any whitespace in the command?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.