-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.elv
164 lines (142 loc) · 3.53 KB
/
chain.elv
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
use github.com/sblundy/elvish-chain-theme/styling
use str
prompt-segments = [ ]
rprompt-segments = [ ]
config = [
&prefix= ""
&suffix= ""
&segment-prefix= " "
&segment-suffix= " "
&default-style=(styling:create-style color0 color11)
]
prompt-config = [
&separator-trailing="\ue0b0"
&prefix= ""
&suffix= "\ue0b0 "
]
rprompt-config = [
&separator-leading="\ue0b2"
&prefix= "\ue0b2"
&suffix= ""
]
# Style manipulation
fn get-fg [s]{
put $s[fg-color]
}
fn set-fg [s fg]{
if (not (str:has-prefix $fg "color")) {
var fg = "color"$fg
}
s[fg-color] = $fg
put $s
}
fn get-bg [s]{
if (has-key $s bg-color) {
put $s[bg-color]
} else {
put 'default'
}
}
fn set-bg [s bg]{
if (not (str:has-prefix $bg "color")) {
var bg = "color"$bg
}
s[bg-color] = $bg
put $s
}
fn -build-prompt [segments cfg]{
if (== (count $segments) 0) {
return
}
var first = $true
var last-style = $cfg[default-style]
fn prefix-styled [text style]{
if (!=s "" $text) {
if (has-key $style bg-color) {
put (styled-segment $text &fg-color=(get-bg $style))
} else {
put (styled-segment $text)
}
}
}
fn suffix-styled [text]{
if (!=s "" $text) {
if (has-key $last-style bg-color) {
put (styled-segment $text &fg-color=(get-bg $last-style))
} else {
put (styled-segment $text)
}
}
}
fn separator-trailing-styled [text current-style]{
if (!=s "" $text) {
var my-style = [&]
if (has-key $last-style bg-color) {
set my-style[fg-color] = (get-bg $last-style)
}
if (has-key $current-style bg-color) {
set my-style[bg-color] = (get-bg $current-style)
}
put (styling:apply-style $text $my-style)
}
}
fn separator-leading-styled [text current-style]{
if (!=s "" $text) {
var my-style = [&]
if (has-key $last-style bg-color) {
set my-style[fg-color] = (get-bg $current-style)
}
if (has-key $current-style bg-color) {
set my-style[bg-color] = (get-bg $last-style)
}
put (styling:apply-style $text $my-style)
}
}
for seg $segments {
var out = [($seg)]
if (> (count $out) 0) {
out = $out[0]
var beginning-style = (styling:extract-style $out[0])
var ending-style = (styling:extract-style $out[-1])
if $first {
put (prefix-styled $cfg[prefix] $beginning-style)
set first = $false
} else {
if (has-key $cfg separator-trailing) {
put (separator-trailing-styled $cfg[separator-trailing] $beginning-style)
}
if (has-key $cfg separator-leading) {
put (separator-leading-styled $cfg[separator-leading] $beginning-style)
}
}
put (styling:apply-style $cfg[segment-prefix] $beginning-style)
put $out
put (styling:apply-style $cfg[segment-suffix] $ending-style)
set last-style = $ending-style
}
}
put (suffix-styled $cfg[suffix])
}
fn -render-prompt [segments @cfgs]{
var merged_cfg = [&]
for cfg $cfgs {
for k [(keys $cfg | all)] {
merged_cfg[$k] = $cfg[$k]
}
}
for s [(-build-prompt $segments $merged_cfg)] {
put $s
}
}
# Prompt and rprompt functions (careful, they will be executed concurrently)
fn prompt {
-render-prompt $prompt-segments $config $prompt-config
}
fn rprompt {
-render-prompt $rprompt-segments $config $rprompt-config
}
# Default init, assigning our functions to `edit:prompt` and `edit:rprompt`
fn init {
edit:prompt = $prompt~
edit:rprompt = $rprompt~
}