forked from spaceship-prompt/spaceship-prompt
-
Notifications
You must be signed in to change notification settings - Fork 12
/
install.zsh
executable file
·100 lines (83 loc) · 2.87 KB
/
install.zsh
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
#!/usr/bin/env zsh
#
# Author: Denys Dovhan, denysdovhan.com
# https://github.com/denysdovhan/spaceship-zsh-theme
# Source ~/.zshrc because we need oh-my-zsh variables
source "$HOME/.zshrc"
# ------------------------------------------------------------------------------
# HELPERS
# Useful functions for common tasks
# ------------------------------------------------------------------------------
# Check if program exists
# USAGE:
# exists <program>
exists() {
command -v $1 >/dev/null 2>&1
}
# Paint text in color
# USAGE:
# paint <color> [text...]
paint() {
echo "$fg[$1]${@:2}$reset_color"
}
# Logging functions.
# USAGE:
# log|error|success [text...]
log() { paint 'cyan' "SPACESHIP: $*" }
error() { echo ; paint 'red' "SPACESHIP: $*" ; echo }
success() { echo ; paint 'green' "SPACESHIP: $*" ; echo }
# ------------------------------------------------------------------------------
# VARIABLES
# Paths to important resources
# ------------------------------------------------------------------------------
URL='https://raw.githubusercontent.com/denysdovhan/spaceship-zsh-theme/master/spaceship.zsh'
SPACESHIP="$PWD/spaceship.zsh"
DIST="$ZSH_CUSTOM/themes/spaceship.zsh-theme"
DOWNLOADED=false
# ------------------------------------------------------------------------------
# MAIN
# Checkings and installing process
# ------------------------------------------------------------------------------
# Should we download Spaceship form GitHub
if [ ! -f "$SPACESHIP" ]; then
SPACESHIP=$(mktemp /tmp/spaceship.zsh.XXXXXX)
log "Spaceship is not present in current directory"
log "Temporary file is created: $SPACESHIP"
if exists curl; then
log "Downloading using curl:"
curl "$URL" -o "$SPACESHIP" || $(error "Failed to load using curl!" && exit 1)
elif exists wget; then
log "Downloading using wget:"
wget "$URL" -O "$SPACESHIP" || $(error "Failed to load using wget!" && exit 1)
else
# Exit with error
error "curl and wget are unavailable!"
exit 1
fi
DOWNLOADED=true
else
log "Spaceship is present in current directory"
fi
# Check if $ZSH_CUSTOM is available
if [[ -z $ZSH_CUSTOM ]]; then
error '$ZSH_CUSTOM is not defined!'
exit 1
fi
mkdir -p "$(dirname $DIST)"
if [[ ! $DOWNLOADED ]]; then
log "Linking $SPACESHIP to $DIST..."
ln -sf "$SPACESHIP" "$DIST"
else
log "Copying $SPACESHIP to $DIST..."
cp -f "$SPACESHIP" "$DIST"
fi
cp -f "$SPACESHIP" "$DIST"
# Add source command to ~/.zshrc
log "Sourcing Spacehsip in ~/.zshrc..."
echo '\n' >> "$HOME/.zshrc"
echo 'source "'"$DIST"'"' >> "$HOME/.zshrc"
# Replace current theme to Spaceship
log 'Attempting to change $ZSH_THEME to "spaceship"...'
sed -i'' 's/ZSH_THEME=.*$/ZSH_THEME="spaceship"/g' "$HOME/.zshrc" \
&& success "Done! Please, reload your terminal." \
|| error "Cannot change theme in ~/.zshrc. Please, do it by yourself." \