-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path.functions
136 lines (112 loc) · 4.41 KB
/
.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# This line imports an OS specific functions file if one exists
# Not been tested on any OS other than Mac OSX
# TODO: find a better way than uname to distinguish between linux distros
[ -f ".functions.$( uname )" ] && source ".functions.$( uname )"
# Utility function to generate completions for files in a directory
_dir_completions() {
# 1st argument $1 is the directory name
if [ -z "$1" ]; then
return
fi
# Only attempt completions if either only the command has been entered (i.e. 'play<tab>')
# or the command is followed by a single word (i.e. 'play grou<tab>')
if [ "${#COMP_WORDS[@]}" != "2" -a "${#COMP_WORDS[@]}" != "1" ]; then
return
fi
# The command in the substitution is a little convoluted; we're doing things
# like this to allow the function to return completions from multiple
# directories. Using the -l option to ls lists everything line by line along
# with a couple of lines of metadata (i.e. dir name, count). The NF > 8 in
# the awk invocation filters for those lines which contain more than 8
# fields, which filters out the metadata lines and leaves the long directory
# listing intact. From this, we print the 9th field, which contains the
# file/directory name. The xargs echo in the end converts from one file per
# line to a list of space separated files.
COMPREPLY=($(compgen -W "$(ls -l $@ | awk 'NF > 8 {print $9}' | xargs echo)" "${COMP_WORDS[1]}"))
}
# I use playgrounds to experiment with new languages and features
# without making a full fledged project
# This creates a new playground with the name given as a command line arg
play () {
# TODO : redirect io to /dev/null and write custom messages
if [ "$#" -lt 1 ] ; then
cd "$PATH_TO_PLAYGROUND" && echo 'In playground'
return
fi
mkdir -p "$PATH_TO_PLAYGROUND/$1" && cd "$PATH_TO_PLAYGROUND/$1"
if [ "$2" = "--autoremove" -o "$2" = "-a" ] ; then
touch "$PATH_TO_PLAYGROUND/$1/.autoremove"
fi
if [ -e "$PATH_TO_PLAYGROUND/$1/.autoremove" ] ; then
echo "WARN: This playground will be deleted on the 1st of the coming month"
fi
echo "In playground $1"
}
# compspec for play
_play_completions() {
_dir_completions "$PATH_TO_PLAYGROUND"
}
complete -F _play_completions play
# Create a new project and cd into it, does NOT check if project already exists
project () {
# TODO : redirect io to /dev/null and write custom messages
if [ "$#" -lt 1 ] ; then
cd "$PATH_TO_PROJECTS" && echo 'In Projects'
fi
mkdir -p "$PATH_TO_PROJECTS/$1" && cd "$PATH_TO_PROJECTS/$1"
echo "In project $1"
}
# compspec for project
_project_completions() {
_dir_completions "$PATH_TO_PROJECTS"
}
complete -F _project_completions project
# Create a useful tmux split which has 3 panes
# One of which is the main pane and the other two are smaller
ide () {
# Get the width of the screen
screen_width=$( tmux display -p '#{window_width}')
# The small panes will be a third the width of the window
small_pane_width=$( expr "$screen_width" / 3 )
# Split into two halves horizontally
tmux split-window -h
# Split the second half into two halves. These are the small panes
tmux split-window
# Resize the small panes
tmux resize-pane -x $small_pane_width
}
journal() {
curdir=$( pwd )
cd "$PATH_TO_JOURNAL"
vim "$1"
cd "$curdir"
}
_journal_completions() {
_dir_completions "$PATH_TO_JOURNAL"
}
complete -F _journal_completions journal
YELLOW_BOLD='\033[33;1m'
GREEN_BOLD='\033[32;1m'
RED_BOLD='\033[31;1m'
BLUE_BOLD='\033[36;1m'
CLEAR='\033[0m'
# Generic function for rendering lists of things like project ideas, blog post
# ideas, etc which have items structured as `[TAG] Title`
# First arg is the filename
_ls_todo_list() {
if [ -z "$1" ]; then
# Silent return for now
return
fi
grep -Po '^### \[\K([A-Z]*)].*$' "$1" | sed 's/]//;s/TODO/'$(printf "${BLUE_BOLD}TODO${CLEAR}")'/;s/DOING/'$(printf "${YELLOW_BOLD}DOING${CLEAR}")'/;s/WONTDO/'$(printf "${RED_BOLD}WONTDO${CLEAR}")'/;s/DONE/'$(printf "${GREEN_BOLD}DONE${CLEAR}")'/;s/ /\t/'
}
ls-project-ideas() {
_ls_todo_list "${PATH_TO_JOURNAL}/project-ideas.md"
}
json-diff() {
diff <( jq --sort-keys . $1 ) <( jq --sort-keys . $2 )
}
# This should be the last line of the file
# For local changes
# Don't make edits below this
[ -f "$HOME/.functions.local" ] && source "$HOME/.functions.local" || true