Skip to content

Instantly share code, notes, and snippets.

@lacostenycoder
Last active March 29, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacostenycoder/9f56bebf4d15f6add19a068d23101c49 to your computer and use it in GitHub Desktop.
Save lacostenycoder/9f56bebf4d15f6add19a068d23101c49 to your computer and use it in GitHub Desktop.
helpful functions
# Show listing ports in MacOS from https://stackoverflow.com/a/30029855/3625433
listening_ports() {
if [ $# -eq 0 ]; then
sudo lsof -iTCP -sTCP:LISTEN -n -P
elif [ $# -eq 1 ]; then
sudo lsof -iTCP -sTCP:LISTEN -n -P | grep -i --color $1
else
echo "Usage: listening [pattern]"
fi
}
# inspired by rails time_ago_in_words, depends on GNU coreutils, for MacOS brew install coreutils
# defaults to days with no 2nd argument.
timeAgo() {
n=$1
if [[ $2 = '' ]]; then
val='days'
else
val=$2
fi
gdate -d "$date -$n $val"
}
timeFromNow() {
n=$1
if [[ $2 = '' ]]; then
val='days'
else
val=$2
fi
gdate -d "$date +$n $val"
}
function findAlias() {
zsh -ixc : 2>&1 | ag $1 | grep '/' | grep alias:
}
# search for a file in all git branches
git-find-file() {
for branch in $(git log --all --oneline --no-color -- "$1" | cut -c 1-9);
do
git branch --contains $branch;
echo "found $1 in $branch";
done
}
# search a regex in any git branch
git-branch-grep() {
for i in $(git branch | grep "$1")
do
echo $i;
done
}
git-stash-prune() {
git stash list | cut -f 1 -d : | tail -"$1" | sort -r | xargs -n 1 git stash drop
}
# opens last migration in your editor. You may need to replace atom with name of your default editor
last_migration() {
atom $(echo "db/migrate/$(ls db/migrate | tail -1)")
}
# opens last post deploy task in your editor. You may need to replace atom with name of your default editor
# replace atom with name of your default editor
last_deploy() {
atom "lib/tasks/deployment/$(ls lib/tasks/deployment | tail -n1)"
}
# open tc issue in your browser
issue() {
arg=$1
if [[ !$arg ]]; then
arg=$(git branch | grep '*' | grep -Poh '\d+')
fi
if [[ $arg ]]; then
open "https://github.com/thoroughcare/thoroughcare/issues/$arg"
fi
}
# open tc commit hash in your browser
show_commit() {
open "https://github.com/thoroughcare/thoroughcare/commit/$1"
}
# kill any orphaned overmind processes.
killOvermind() {
kill $(ps au | grep 'overmind' | grep -v 'ggrep' | awk '{print $2}')
}
# terminal calculator
# syntax: c 100*100-15+5
# should output 9985.55555555555555555555
c(){
echo "$*" | bc -l
}
showDesktop() {
defaults write com.apple.finder CreateDesktop -bool $1
killall Finder
}
issue() {
if [ $1 ]; then
issue_number=$1
else
issue_number=$(git branch | grep '*' | grep '[0-9]' | sed 's/[^0-9]*//g')
fi
$(gh issue view $issue_number -w)
}
# ================================================================
# iTerm2 tab color functions
#
# Author: Connor de la Cruz (connor.c.delacruz@gmail.com)
# Repo: https://github.com/connordelacruz/iterm2-tab-color
# ================================================================
# Set the tab color
it2-tab-color() {
# takes 1 hex string argument or 3 hex values for RGB
local R G B
case "$#" in
3)
R="$1"
G="$2"
B="$3"
;;
1)
local hex="$1"
# Remove leading # if present
if [[ "${hex:0:1}" == "#" ]]; then
hex="${hex:1}"
fi
# Get hex values for each channel and convert to decimal
R="$((16#${hex:0:2}))"
G="$((16#${hex:2:2}))"
B="$((16#${hex:4}))"
;;
*)
echo "Usage: it2-tab-color color_hex"
echo " color_hex: 6 digit hex value (e.g. 1B2B34)"
echo " it2-tab-color r_val g_val b_val"
echo " *_val: values for R, G, B from 0-255 (e.g. 27 43 52)"
return
;;
esac
echo -ne "\033]6;1;bg;red;brightness;$R\a"
echo -ne "\033]6;1;bg;green;brightness;$G\a"
echo -ne "\033]6;1;bg;blue;brightness;$B\a"
# Export environment variable to maintain colors during session
export IT2_SESSION_COLOR="$R $G $B"
}
# Reset tab color to default
it2-tab-reset() {
echo -ne "\033]6;1;bg;*;default\a"
# Unset environment variable
unset IT2_SESSION_COLOR
}
# Check for ~/.base16_theme and set the tab color based on that
it2-b16-theme() {
if [ -f "$HOME/.base16_theme" ]; then
local colornum color
# If no argument was passed, default to color00
if [ "$#" -lt 1 ]; then
colornum="00"
else
# Add leading 0 if necessary
colornum="$(printf "%02d" $1)"
fi
color="$(perl -nle "print \$& if m{color$colornum=\"\K.*(?=\")}" "$HOME/.base16_theme")"
it2-tab-color ${color///}
fi
}
# Restore session tab color
if [ -n "$IT2_SESSION_COLOR" ]; then
it2-tab-color $IT2_SESSION_COLOR
fi
# Replace Audio in Video
replace_audio_in_video() {
fmpeg -i "$1" -i "$2" -c:v copy -map 0:v:0 -map 1:a:0 new_"$1"
}
@lacostenycoder
Copy link
Author

Add ability to toggle focused keyword

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