-
Notifications
You must be signed in to change notification settings - Fork 1
/
ruby.zsh
94 lines (78 loc) · 3.04 KB
/
ruby.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
# Code in this file comes from https://github.com/denysdovhan/spaceship-prompt/tree/666e920e5c0a3ba22f49a41fd3a1779d997d238c
# Check if command exists in $PATH
# USAGE:
# spaceship::exists <command>
spaceship::exists() {
command -v $1 > /dev/null 2>&1
}
# Draw prompt section (bold is used as default)
# USAGE:
# spaceship::section <color> [prefix] <content> [suffix]
spaceship::section() {
local color prefix content suffix
[[ -n $1 ]] && color="%F{$1}" || color="%f"
[[ -n $2 ]] && prefix="$2" || prefix=""
[[ -n $3 ]] && content="$3" || content=""
[[ -n $4 ]] && suffix="$4" || suffix=""
[[ -z $3 && -z $4 ]] && content=$2 prefix=''
echo -n "%{%B%}" # set bold
if [[ $spaceship_prompt_opened == true ]] && [[ $SPACESHIP_PROMPT_PREFIXES_SHOW == true ]]; then
echo -n "$prefix"
fi
spaceship_prompt_opened=true
echo -n "%{%b%}" # unset bold
echo -n "%{%B$color%}" # set color
echo -n "$content" # section content
echo -n "%{%b%f%}" # unset color
echo -n "%{%B%}" # reset bold, if it was diabled before
if [[ $SPACESHIP_PROMPT_SUFFIXES_SHOW == true ]]; then
echo -n "$suffix"
fi
echo -n "%{%b%}" # unset bold
}
spaceship_prompt_opened=true
SPACESHIP_PROMPT_PREFIXES_SHOW=true
SPACESHIP_PROMPT_DEFAULT_PREFIX=" "
#
# Ruby
#
# A dynamic, reflective, object-oriented, general-purpose programming language.
# Link: https://www.ruby-lang.org/
# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
SPACESHIP_RUBY_SHOW="${SPACESHIP_RUBY_SHOW=true}"
SPACESHIP_RUBY_PREFIX="${SPACESHIP_RUBY_PREFIX="$SPACESHIP_PROMPT_DEFAULT_PREFIX"}"
SPACESHIP_RUBY_SUFFIX="${SPACESHIP_RUBY_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}"
SPACESHIP_RUBY_SYMBOL="${SPACESHIP_RUBY_SYMBOL="💎"}"
SPACESHIP_RUBY_COLOR="${SPACESHIP_RUBY_COLOR="red"}"
# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------
# Show current version of Ruby
spaceship_ruby() {
[[ $SPACESHIP_RUBY_SHOW == false ]] && return
# Show versions only for Ruby-specific folders
[[ -f Gemfile || -f Rakefile || -f .ruby-version || -n *.rb(#qN^/) ]] || return
local 'ruby_version'
if spaceship::exists rvm-prompt; then
ruby_version=$(rvm-prompt i v g)
elif spaceship::exists chruby; then
ruby_version=$(chruby | sed -n -e 's/ \* //p')
elif spaceship::exists rbenv; then
ruby_version=$(rbenv version-name)
elif spaceship::exists asdf; then
ruby_version=$(asdf current ruby | awk '{print $1}')
else
return
fi
[[ -z $ruby_version || "${ruby_version}" == "system" ]] && return
ruby_version="${ruby_version//#ruby-/}"
# Add 'v' before ruby version that starts with a number
[[ "${ruby_version}" =~ ^[0-9].+$ ]] && ruby_version="v${ruby_version}"
spaceship::section \
"$SPACESHIP_RUBY_COLOR" \
"$SPACESHIP_RUBY_PREFIX" \
"${SPACESHIP_RUBY_SYMBOL}${ruby_version}" \
"$SPACESHIP_RUBY_SUFFIX"
}