From d44bee6f731a0dc9326556014de46b5b35262435 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Wed, 18 Jan 2023 13:54:39 +0200 Subject: [PATCH] feat: mac/linux detection --- .zshrc | 1 + aliases.sh | 47 -------------------------- functions.sh | 72 ++++++++++++++++++++++++++++++++++++++++ man_src/home.7 | 38 ++++++++++----------- scripts/home/defaults.sh | 4 +++ scripts/home/home.sh | 4 +-- 6 files changed, 98 insertions(+), 68 deletions(-) create mode 100644 functions.sh diff --git a/.zshrc b/.zshrc index c4c3e282..fd0c011d 100644 --- a/.zshrc +++ b/.zshrc @@ -4,6 +4,7 @@ export DOTBIN="$DOTFILES/bin" source $DOTFILES/exports.sh # must run before zsh_init source $DOTFILES/zsh_init.sh source $DOTFILES/aliases.sh +source $DOTFILES/functions.sh source $DOTFILES/sources.sh source $DOTFILES/scripts/home/home.sh diff --git a/aliases.sh b/aliases.sh index 850a0c72..fc9c3714 100755 --- a/aliases.sh +++ b/aliases.sh @@ -52,50 +52,3 @@ alias unq="sudo xattr -rd com.apple.quarantine" alias h="home" alias rh="rhome" alias spider="ssh root@spider.casraf.dev" - -motd() { - if [[ -f /etc/motd.head ]]; then lolcat -f /etc/motd.head; fi - uname -a - if [[ -f /etc/motd ]]; then cat /etc/motd; fi -} - -docker-bash() { - docker exec -ti $1 /bin/bash -} - -# Functions - -# show all man entries under a specific section -# e.g. mansect 7 -mansect() { man -aWS ${1?man section not provided} \* | xargs basename | sed "s/\.[^.]*$//" | sort -u; } - -# TODO not working with custom commands... -tcd() { - source $HOME/.zshrc - cd $1 - shift - exec $@ 2>&1 | tee -- - # command "$@" 2>&1 - cd $OLDPWD -} - -# mkdir -p then navigate to said directory -mkcd() { - mkdir -p -- "$1" && cd -P -- "$1" -} - -listening() { - if [[ $# -eq 0 ]]; then - lsof -iTCP -sTCP:LISTEN -n -P - elif [[ $# -eq 1 ]]; then - lsof -iTCP -sTCP:LISTEN -n -P | grep -i --color $1 - else - echo "Usage: listening [pattern]" - fi -} - -# example echo '1' | prepend 'result: ' -prepend() { - echo -n "$@" - cat - -} diff --git a/functions.sh b/functions.sh new file mode 100644 index 00000000..fe8fdba0 --- /dev/null +++ b/functions.sh @@ -0,0 +1,72 @@ +source $HOME/.dotfiles/colors.sh + +motd() { + if [[ -f /etc/motd.head ]]; then lolcat -f /etc/motd.head; fi + uname -a + if [[ -f /etc/motd ]]; then cat /etc/motd; fi +} + +docker-bash() { + docker exec -ti $1 /bin/bash +} + +# Functions + +# show all man entries under a specific section +# e.g. mansect 7 +mansect() { man -aWS ${1?man section not provided} \* | xargs basename | sed "s/\.[^.]*$//" | sort -u; } + +# TODO not working with custom commands... +tcd() { + source $HOME/.zshrc + cd $1 + shift + exec $@ 2>&1 | tee -- + # command "$@" 2>&1 + cd $OLDPWD +} + +# mkdir -p then navigate to said directory +mkcd() { + mkdir -p -- "$1" && cd -P -- "$1" +} + +listening() { + if [[ $# -eq 0 ]]; then + lsof -iTCP -sTCP:LISTEN -n -P + elif [[ $# -eq 1 ]]; then + lsof -iTCP -sTCP:LISTEN -n -P | grep -i --color $1 + else + echo "Usage: listening [pattern]" + fi +} + +# example echo '1' | prepend 'result: ' +prepend() { + echo -n "$@" + cat - +} + +lcase() { + echo "$@" | tr '[:upper:]' '[:lower:]' +} + +int_res() { + # get all but last + c=$(($# - 1)) + out="$(lcase $(bash -c "${@:1:$c}"))" + check="$(lcase ${@: -1})" + if [[ $out =~ $check ]]; then + echo 0 + else + echo 1 + fi +} + +is_mac() { + int_res "uname -s" "darwin" +} + +is_linux() { + int_res "uname -s" "linux" +} diff --git a/man_src/home.7 b/man_src/home.7 index 7d4a2435..9cc05fb4 100644 --- a/man_src/home.7 +++ b/man_src/home.7 @@ -16,36 +16,36 @@ before running the supplied command home takes one command: -git, g Run arbitrary git commands from .dotfiles directory +git, g Run arbitrary git commands from .dotfiles directory -worflows, w Manage custom workflows for Alfred, keeping them in sync between dotfiles and external repository - (if applicable). - - Sub commands available: push, pull +worflows, w Manage custom workflows for Alfred, keeping them in sync between dotfiles and external repository + (if applicable). + - Sub commands available: push, pull -motd, m Manage syncd MOTD (Message of the Day) - - Sub commands available: restore, backup, edit, show +motd, m Manage syncd MOTD (Message of the Day) + - Sub commands available: restore, backup, edit, show -brew, b Use brew bundle to load or dump currently installed packages. - Will read/write from Brewfile in .dotfiles directory - - Subcommands: dump, restore. +brew, b Use brew bundle to load or dump currently installed packages. + Will read/write from Brewfile in .dotfiles directory + - Subcommands: dump, restore. -brew, b Manage data for Dropzone installation. - - Subcommands: dump, restore. +brew, b Manage data for Dropzone installation. + - Subcommands: dump, restore. -status, s See git status for dotfiles directory +status, s See git status for dotfiles directory -push, p Add all files, commit and push updates to git. Supply commit msg as first arg or it will ask - interactively. +push, p Add all files, commit and push updates to git. Supply commit msg as first arg or it will ask + interactively. -pull, l Pull updates from git and reload home +pull, l Pull updates from git and reload home -reload, rz Reload (source) the current ZSH shell. Incrementally slower as you use it in same terminal. +reload-term, rt Reload (source) the current ZSH shell. Incrementally slower as you use it in same terminal. -refresh, rh Refresh (source) only the home script and tools. Faster than reload. +reload-home, rh Reload (source) only the home script and tools. Faster than reload-term. -install, i Install all settings files to their appropriate locations (e.g. MAN page, plugins, themes, bins, etc) +install, i Install all settings files to their appropriate locations (e.g. MAN page, plugins, themes, bins, etc) -help, -h Dislpay help file +help, -h Dislpay help file .SH CUSTOM ALIASES diff --git a/scripts/home/defaults.sh b/scripts/home/defaults.sh index 93eca35a..1c957fad 100644 --- a/scripts/home/defaults.sh +++ b/scripts/home/defaults.sh @@ -1,6 +1,10 @@ # OSX defaults overrides __write_default() { + if [[ $(is_mac) == "1" ]]; then + return 0 + fi + cmd=$1 shift args=($@) diff --git a/scripts/home/home.sh b/scripts/home/home.sh index be2a87f5..ee33bb08 100644 --- a/scripts/home/home.sh +++ b/scripts/home/home.sh @@ -37,11 +37,11 @@ home() { reload-zsh __home_revert_dir ;; - reload | rz) + reload-term | rt) reload-zsh return 0 ;; - refresh | rh) + reload-home | rh) source $DOTFILES/scripts/home/home.sh return 0 ;;