/
promnow
174 lines (165 loc) · 6.31 KB
/
promnow
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# shebang is unnecessary, but helps vim identify the filetype
#
# 'promnow' sets a prompt if passed a prompt name, or shows a list of
# available prompts if no name is passed. It also sets a number of
# variable values - things that cannot change during the life of the shell,
# such as the OS type, and whether or not your device has a battery.
#
# Variables provided:
# _p_uid0 - user ID is 0 (ie. root or root-equivalent)
# _p_os - "Darwin", "Linux", or "?"
# _p_has_battery - true or false, does this computer have a battery?
# _p_numthreads - integer, how many processor threads are available
# _p_in_nvim - true or false, is this shell in a Neovim session?
# _p_in_screen - true or false, is this shell in a screen session?
# _p_in_ssh - true or false, is this shell in a ssh session?
# _p_in_tmux - true or false, is this shell in a tmux session?
# _p_in_typescript - true or false, is this shell in a typescript session
# _p_is_pi - true if the hardware is a Raspberry Pi
# This is a problematic hard-coded value: change it if you install the
# project anywhere other than the suggested location.
export PROMPTDIR="${HOME}/.bashprompt"
# we can't use $(dirname ${0}) to get this directory because this isn't run
# as a script, it's sourced.
# http://www.thegeekstuff.com/2013/12/bash-completion-complete/
# If you add a new prompt, it won't be visible in bash completion until you
# run the following command again.
_pn_dir="${PROMPTDIR}/prompts"
export _pn_dir
_promnow () {
local cur="${2}"
local _cur compreply
_cur="${_pn_dir}/${cur}"
mapfile compreply < <( compgen -f "${_cur}" )
# shellcheck disable=SC2206 # "Quote to prevent word splitting/globbing"
COMPREPLY=( ${compreply[@]#${_pn_dir}/} )
}
complete -F _promnow promnow
# as this isn't called directly by the user, give it a name that won't
# clutter the user's name space (as much - originally "promnow_help").
_promnow_help () {
echo "promnow <PROMPTNAME>"
echo "You must supply a single prompt name."
echo "Available prompts are:"
ls "${PROMPTDIR}/prompts/"
if [ -n "${_p_NAME}" ]
then
echo " You are currently using the '${_p_NAME}' prompt."
fi
}
_promnow_setup () {
if (( UID == 0 ))
then
export _p_uid0=true
else
export _p_uid0=false
fi
# set defaults, update if possible later
export _p_has_battery=false
export _p_os="?"
# set _p_os to string "Darwin" or "Linux"
if ( command uname -a | command grep Darwin > /dev/null )
then
export _p_os="Darwin"
######################### Darwin Battery(s) check #####################
if command ioreg -l | command grep Capacity >/dev/null
then
export _p_has_battery=true
fi
######################## Darwin Thread check ##########################
# this is LOGICAL cores, ie. threads:
_p_numthreads=$(sysctl -n hw.ncpu)
export _p_numthreads
elif ( command uname -a | command grep Linux > /dev/null )
then
export _p_os="Linux"
######################### Linux Battery(s) check #####################
num_batteries=$(command ls -1d /sys/class/power_supply/BAT* 2>/dev/null | command wc -l)
if (( num_batteries > 0 ))
then
export _p_has_battery=true
fi
######################## Linux Thread check ###########################
# the 'CPU(s)' line seems to be LOGICAL cores, ie. threads:
_p_numthreads=$(lscpu | grep '^CPU(s):' | awk '{ print $NF }')
export _p_numthreads
fi
####################### Raspberry Pi check ################################
export _p_is_pi=false
if grep -q Raspberry /proc/cpuinfo
then
export _p_is_pi=true
fi
####################### tmux check ########################################
export _p_in_tmux=false
# on one Linux machine 'which tmux' error was stdout, on another stderr!:
if command -v tmux > /dev/null 2>&1
then # tmux is installed
if [[ "${TMUX}" =~ "tmux" ]]
then
export _p_in_tmux=true
fi
fi
####################### screen check ######################################
export _p_in_screen=false
if command -v screen > /dev/null 2>&1
then # screen is installed
# screen sometime (Mac?) sets TERM, sometimes (Fedora) sets TERMCAP
# ... but tmux will sometimes set TERM to "screen" so we have to do
# a lot of checking.
if [[ "${TERM}" =~ "screen" ]] || [[ "${TERMCAP}" =~ "screen" ]] && [[ "${TMUX}x" == "x" ]]
then
export _p_in_screen=true
fi
fi
####################### typescript check ###################################
export _p_in_typescript=false
if command -v script > /dev/null 2>&1
then # script is installed
if [[ "${SCRIPT}" =~ "script" ]]
then
export _p_in_typescript=true
fi
fi
####################### ssh check #########################################
# Not checking to see if it's installed ...
export _p_in_ssh=false
if [ -n "$SSH_CLIENT" ]
then
export _p_in_ssh=true
fi
####################### shell inside NeoVim check #########################
export _p_in_nvim=false
if [ -n "${NVIM}" ] || [ -n "${NVIM_INDICATOR}" ] || [ -n "${NVIM_LISTEN_ADDRESS}" ]
then
export _p_in_nvim=true
fi
}
# set some env variables, then unclutter the environment (at least a little
# bit):
_promnow_setup ; unset _promnow_setup
promnow () {
if (( $# == 1 ))
then
promfullname="${PROMPTDIR}/prompts/${1}"
if [ -e "${promfullname}" ]
then
# replace any occurrences of 'prompt_command' with a no-op:
PROMPT_COMMAND="${PROMPT_COMMAND//prompt_command/:}"
# clean up further by getting rid of extra ": ;" sets:
# shellcheck disable=SC2001 # See if you can use ${variable//...
tmp="$( echo "${PROMPT_COMMAND}" | sed -e 's/;[[:space:]]*:[[:space:]]*//g' )"
PROMPT_COMMAND="${tmp}"
unset tmp
# shellcheck disable=SC1090 # "non-constant source"
source "${promfullname}"
export _p_NAME="${1}"
else
echo "Couldn't find prompt '${1}'"
_promnow_help
fi
else
_promnow_help
fi
}