1
0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2026-02-13 05:59:46 +08:00

Compare commits

..

No commits in common. "62929263fafd9e3c1da043bc9b40fa97fccfa7a1" and "fcbfdf42de702d55174fe2b19142ba232289671e" have entirely different histories.

7 changed files with 77 additions and 105 deletions

View File

@ -1,79 +1,44 @@
#!/usr/bin/env zsh #!/usr/bin/env zsh
## Setup ## setup ##
[[ -o interactive ]] || return # don't load on non-interactive shells [[ -o interactive ]] || return #interactive only!
[[ -z "$SSH_CLIENT" && -z "$SSH_TTY" ]] || return # don't load on a SSH connection zmodload zsh/datetime || { print "can't load zsh/datetime"; return } # faster than date()
autoload -Uz add-zsh-hook || { print "can't add zsh hook!"; return }
zmodload zsh/datetime # faster than `date` (( ${+bgnotify_threshold} )) || bgnotify_threshold=5 #default 10 seconds
## Zsh Hooks ## definitions ##
function bgnotify_begin { if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom function override
bgnotify_timestamp=$EPOCHSECONDS function bgnotify_formatted { ## args: (exit_status, command, elapsed_seconds)
bgnotify_lastcmd="${1:-$2}" elapsed="$(( $3 % 60 ))s"
} (( $3 >= 60 )) && elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
(( $3 >= 3600 )) && elapsed="$(( $3 / 3600 ))h $elapsed"
function bgnotify_end { [ $1 -eq 0 ] && bgnotify "#win (took $elapsed)" "$2" || bgnotify "#fail (took $elapsed)" "$2"
{
local exit_status=$?
local elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
# check time elapsed
[[ $bgnotify_timestamp -gt 0 ]] || return
[[ $elapsed -ge $bgnotify_threshold ]] || return
# check if Terminal app is not active
[[ $(bgnotify_appid) != "$bgnotify_termid" ]] || return
printf '\a' # beep sound
bgnotify_formatted "$exit_status" "$bgnotify_lastcmd" "$elapsed"
} always {
bgnotify_timestamp=0
} }
} fi
autoload -Uz add-zsh-hook currentAppId () {
add-zsh-hook preexec bgnotify_begin if (( $+commands[osascript] )); then
add-zsh-hook precmd bgnotify_end osascript -e 'tell application (path to frontmost application as text) to id' 2>/dev/null
## Functions
# allow custom function override
(( ${+functions[bgnotify_formatted]} )) || \
function bgnotify_formatted {
local exit_status=$1
local cmd="$2"
# humanly readable elapsed time
local elapsed="$(( $3 % 60 ))s"
(( $3 < 60 )) || elapsed="$((( $3 % 3600) / 60 ))m $elapsed"
(( $3 < 3600 )) || elapsed="$(( $3 / 3600 ))h $elapsed"
if [[ $1 -eq 0 ]]; then
bgnotify "#win (took $elapsed)" "$2"
else
bgnotify "#fail (took $elapsed)" "$2"
fi fi
} }
# for macOS, output is "app ID, window ID" (com.googlecode.iterm2, 116) currentWindowId () {
function bgnotify_appid { if hash osascript 2>/dev/null; then #osx
if (( ${+commands[osascript]} )); then osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0"
osascript -e 'tell application (path to frontmost application as text) to get the {id, id of front window}' 2>/dev/null elif (hash notify-send 2>/dev/null || hash kdialog 2>/dev/null); then #ubuntu!
elif (( ${+commands[xprop]} )); then xprop -root 2> /dev/null | awk '/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}' || echo "0"
xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | cut -d' ' -f5
else else
echo $EPOCHSECONDS echo $EPOCHSECONDS #fallback for windows
fi fi
} }
function bgnotify { bgnotify () { ## args: (title, subtitle)
# $1: title, $2: message if hash terminal-notifier 2>/dev/null; then #osx
if (( ${+commands[terminal-notifier]} )); then # macOS local term_id="$bgnotify_appid"
local term_id="${bgnotify_termid%%,*}" # remove window id
if [[ -z "$term_id" ]]; then if [[ -z "$term_id" ]]; then
case "$TERM_PROGRAM" in case "$TERM_PROGRAM" in
iTerm.app) term_id='com.googlecode.iterm2' ;; iTerm.app) term_id='com.googlecode.iterm2' ;;
@ -81,26 +46,48 @@ function bgnotify {
esac esac
fi fi
## now call terminal-notifier, (hopefully with $term_id!)
if [[ -z "$term_id" ]]; then if [[ -z "$term_id" ]]; then
terminal-notifier -message "$2" -title "$1" &>/dev/null terminal-notifier -message "$2" -title "$1" >/dev/null
else else
terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" &>/dev/null terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null
fi fi
elif (( ${+commands[growlnotify]} )); then # macOS growl elif hash growlnotify 2>/dev/null; then #osx growl
growlnotify -m "$1" "$2" growlnotify -m "$1" "$2"
elif (( ${+commands[notify-send]} )); then # GNOME elif hash notify-send 2>/dev/null; then #ubuntu gnome!
notify-send "$1" "$2" notify-send "$1" "$2"
elif (( ${+commands[kdialog]} )); then # KDE elif hash kdialog 2>/dev/null; then #ubuntu kde!
kdialog --title "$1" --passivepopup "$2" 5 kdialog --title "$1" --passivepopup "$2" 5
elif (( ${+commands[notifu]} )); then # cygwin elif hash notifu 2>/dev/null; then #cygwyn support!
notifu /m "$2" /p "$1" notifu /m "$2" /p "$1"
fi fi
} }
## Defaults
# notify if command took longer than 5s by default ## Zsh hooks ##
bgnotify_threshold=${bgnotify_threshold:-5}
# bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup bgnotify_begin() {
bgnotify_termid="$(bgnotify_appid)" bgnotify_timestamp=$EPOCHSECONDS
bgnotify_lastcmd="${1:-$2}"
bgnotify_appid="$(currentAppId)"
bgnotify_windowid=$(currentWindowId)
}
bgnotify_end() {
didexit=$?
elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
past_threshold=$(( elapsed >= bgnotify_threshold ))
if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then
if [[ $(currentAppId) != "$bgnotify_appid" || $(currentWindowId) != "$bgnotify_windowid" ]]; then
print -n "\a"
bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"
fi
fi
bgnotify_timestamp=0 #reset it to 0!
}
## only enable if a local (non-ssh) connection
if [ -z "$SSH_CLIENT" ] && [ -z "$SSH_TTY" ]; then
add-zsh-hook preexec bgnotify_begin
add-zsh-hook precmd bgnotify_end
fi

View File

@ -21,7 +21,7 @@ Set `$apt_pref` and `$apt_upgr` to whatever command you want (before sourcing Oh
| ------ | ---------------------------------------------------------------------- | ---------------------------------------------------------- | | ------ | ---------------------------------------------------------------------- | ---------------------------------------------------------- |
| `age` | `apt-get` | Command line tool for handling packages | | `age` | `apt-get` | Command line tool for handling packages |
| `api` | `aptitude` | Same functionality as `apt-get`, provides extra options | | `api` | `aptitude` | Same functionality as `apt-get`, provides extra options |
| `acse` | `apt-cache search` | Command line tool for searching apt software package cache | | `acs` | `apt-cache search` | Command line tool for searching apt software package cache |
| `aps` | `aptitude search` | Searches installed packages using aptitude | | `aps` | `aptitude search` | Searches installed packages using aptitude |
| `as` | `aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search` | Print searched packages using a custom format | | `as` | `aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search` | Print searched packages using a custom format |
| `afs` | `apt-file search --regexp` | Search file in packages | | `afs` | `apt-file search --regexp` | Search file in packages |

View File

@ -26,7 +26,7 @@ alias age='apt-get'
alias api='aptitude' alias api='aptitude'
# Some self-explanatory aliases # Some self-explanatory aliases
alias acse="apt-cache search" alias acs="apt-cache search"
alias aps='aptitude search' alias aps='aptitude search'
alias as="aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search" alias as="aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search"
@ -51,7 +51,7 @@ if [[ $use_sudo -eq 1 ]]; then
alias au="sudo $apt_pref $apt_upgr" alias au="sudo $apt_pref $apt_upgr"
alias ai="sudo $apt_pref install" alias ai="sudo $apt_pref install"
# Install all packages given on the command line while using only the first word of each line: # Install all packages given on the command line while using only the first word of each line:
# acse ... | ail # acs ... | ail
alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | xargs sudo $apt_pref install" alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | xargs sudo $apt_pref install"
alias ap="sudo $apt_pref purge" alias ap="sudo $apt_pref purge"

View File

@ -10,16 +10,6 @@ plugins=(... perms)
## Usage ## Usage
> **CAUTION:** these functions are harmful if you don't know what they do. * `set755` recursively sets all given directories (default to .) to octal 755.
* `set644` recursively sets all given files (default to .) to octal 644.
- `set755`: sets the permission to octal 755 for all given directories and their child directories (by default, starting from the current directory). * `fixperms` is a wrapper around `set755` and `set644` applied to a specified directory or the current directory otherwise. It also prompts prior to execution unlike the other two aliases.
- `set644`: sets the permission to octal 644 for all files of the given directory (by default, the current directory), recursively. It will only affect regular files (no symlinks).
- `resetperms` is a wrapper around `set755` and `set644` applied to a specified directory or the current directory otherwise.
It will set the permissions to 755 for directories, and 644 for files.
## Reference
- octal 644: _read and write_ for the owner, _read_ for the group and others users.
- octal 755: _read, write and execute_ permissions for the owner, and _read and execute_ for the group and others users.

View File

@ -6,25 +6,25 @@
### Aliases ### Aliases
# Set all files' permissions to 644 recursively in a directory # Set all files' permissions to 644 recursively in a directory
function set644 { set644() {
find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644 find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644
} }
# Set all directories' permissions to 755 recursively in a directory # Set all directories' permissions to 755 recursively in a directory
function set755 { set755() {
find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755 find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755
} }
### Functions ### Functions
# resetperms - fix permissions on files and directories, with confirmation # fixperms - fix permissions on files and directories, with confirmation
# Returns 0 on success, nonzero if any errors occurred # Returns 0 on success, nonzero if any errors occurred
function resetperms { fixperms () {
local opts confirm target exit_status chmod_opts use_slow_mode local opts confirm target exit_status chmod_opts use_slow_mode
zparseopts -E -D -a opts -help -slow v+=chmod_opts zparseopts -E -D -a opts -help -slow v+=chmod_opts
if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then
cat <<EOF cat <<EOF
Usage: resetperms [-v] [--help] [--slow] [target] Usage: fixperms [-v] [--help] [--slow] [target]
target is the file or directory to change permissions on. If omitted, target is the file or directory to change permissions on. If omitted,
the current directory is taken to be the target. the current directory is taken to be the target.
@ -40,7 +40,7 @@ EOF
return $exit_status return $exit_status
fi fi
if [[ $# -eq 0 ]]; then if [[ $# == 0 ]]; then
target="." target="."
else else
target="$1" target="$1"
@ -49,7 +49,7 @@ EOF
# Because this requires confirmation, bail in noninteractive shells # Because this requires confirmation, bail in noninteractive shells
if [[ ! -o interactive ]]; then if [[ ! -o interactive ]]; then
echo "resetperms: cannot run in noninteractive shell" echo "fixperms: cannot run in noninteractive shell"
return 1 return 1
fi fi
@ -68,20 +68,15 @@ EOF
if [[ $use_slow == true ]]; then if [[ $use_slow == true ]]; then
# Process directories first so non-traversable ones are fixed as we go # Process directories first so non-traversable ones are fixed as we go
find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \; find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \;
if [[ $? -ne 0 ]]; then exit_status=$?; fi if [[ $? != 0 ]]; then exit_status=$?; fi
find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \; find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \;
if [[ $? -ne 0 ]]; then exit_status=$?; fi if [[ $? != 0 ]]; then exit_status=$?; fi
else else
find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755 find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755
if [[ $? -ne 0 ]]; then exit_status=$?; fi if [[ $? != 0 ]]; then exit_status=$?; fi
find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644 find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644
if [[ $? -ne 0 ]]; then exit_status=$?; fi if [[ $? != 0 ]]; then exit_status=$?; fi
fi fi
echo "Complete" echo "Complete"
return $exit_status return $exit_status
} }
function fixperms {
print -ru2 "fixperms has been deprecated. Use resetperms instead"
return 1
}

View File

@ -15,7 +15,7 @@ Commands that use `$APT` will use `apt` if installed or defer to `apt-get` other
| Alias | Command | Description | | Alias | Command | Description |
|---------|--------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------| |---------|--------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
| age | `sudo $APT` | Run apt-get with sudo | | age | `sudo $APT` | Run apt-get with sudo |
| acse | `apt-cache search` | Search the apt-cache with the specified criteria | | acs | `apt-cache search` | Search the apt-cache with the specified criteria |
| acsp | `apt-cache showpkg` | Shows information about the listed packages | | acsp | `apt-cache showpkg` | Shows information about the listed packages |
| acp | `apt-cache policy` | Display the package source priorities | | acp | `apt-cache policy` | Display the package source priorities |
| afs | `apt-file search --regexp` | Perform a regular expression apt-file search | | afs | `apt-file search --regexp` | Perform a regular expression apt-file search |

View File

@ -1,6 +1,6 @@
(( $+commands[apt] )) && APT=apt || APT=apt-get (( $+commands[apt] )) && APT=apt || APT=apt-get
alias acse='apt-cache search' alias acs='apt-cache search'
alias afs='apt-file search --regexp' alias afs='apt-file search --regexp'