-
Notifications
You must be signed in to change notification settings - Fork 0
/
kratos.elv
251 lines (227 loc) · 6.64 KB
/
kratos.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright (c) 2018, 2020, Cody Opel <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use github.com/chlorm/elvish-git/status
use github.com/chlorm/elvish-ssh/agent
use github.com/chlorm/elvish-stl/env
use github.com/chlorm/elvish-stl/io
use github.com/chlorm/elvish-stl/os
use github.com/chlorm/elvish-stl/path
use github.com/chlorm/elvish-stl/platform
use github.com/chlorm/elvish-stl/str
use github.com/chlorm/elvish-xdg/xdg-dirs
var KRATOS-DIR = (path:join (xdg-dirs:runtime-dir) 'kratos')
var LOCKFILENAME = 'session.lock'
var LOCKFILE = (path:join $KRATOS-DIR $LOCKFILENAME)
var INITIALIZED = (os:exists $LOCKFILE)
fn cache-new {|name contents~|
var c = (path:join $KRATOS-DIR $name)
if (not (os:exists $c)) {
os:touch $c
os:chmod 600 $c
try {
print ($contents~) > $c
} catch e { fail $e['reason'] }
}
put $c
}
fn cache-read {|cache|
put (io:open $cache)
}
fn cache-remove {|cache|
os:remove $cache
}
fn init-xdg-dir-vars {
try {
use ../elvish-xdg/xdg-dirs
xdg-dirs:populate-env
} catch e { echo $e['reason'] >&2 }
}
fn init-session-default-shell {
if $platform:is-windows { return }
try {
use ../elvish-as-default-shell/default-shell
default-shell:init-session
} catch e { echo $e['reason'] >&2 }
}
fn init-session-automount-tmpfs {
if $platform:is-windows { return }
try {
use ../elvish-tmpfs/automount
} catch e { echo $e['reason'] >&2 }
}
fn init-session-agent {
try {
agent:init-session
} catch e { echo $e['reason'] >&2 }
}
fn init-session-dirs {
var initDirs = [ ]
try {
for i [ (str:split $env:DELIMITER (env:get 'KRATOS_INIT_DIRS')) ] {
set initDirs = [ $@initDirs $i ]
}
} catch _ {
return
}
for dir $initDirs {
if (not (os:exists $dir)) {
try {
os:makedirs $dir
} catch e { echo $e['reason'] >&2 }
}
}
}
fn init-session {
var startupLock = 'startup.lock'
if (os:exists (path:join $KRATOS-DIR $startupLock)) {
return
}
if (not (os:exists $KRATOS-DIR)) {
os:makedir $KRATOS-DIR
}
var startup = (cache-new $startupLock $nop~)
init-xdg-dir-vars
init-session-dirs
run-parallel ^
$init-session-agent~ ^
$init-session-automount-tmpfs~ ^
$init-session-default-shell~
var _ = (cache-new $LOCKFILENAME $nop~)
cache-remove $startup
}
fn init-instance-color-scheme {
try {
use github.com/chlorm/elvish-term/color-scheme
# TODO: add an interface to allow user defined themes
color-scheme:set $color-scheme:gitiles
} catch e { echo $e['reason'] >&2 }
}
fn init-instance-ls-colors {
if $platform:is-windows { return }
try {
use github.com/chlorm/elvish-auto-env/ls
var lsCache = (cache-new 'ls' $ls:get~)
try {
ls:set &static=(cache-read $lsCache)
} catch e { echo $e['reason'] >&2 }
} catch e { echo $e['reason'] >&2 }
}
fn init-instance-editor-env {
use github.com/chlorm/elvish-auto-env/editor
try {
editor:set
} catch e { echo $e['reason'] >&2 }
}
fn init-instance-pager-env {
try {
use github.com/chlorm/elvish-auto-env/pager
var pagerCache = (cache-new 'pager' $pager:get~)
try {
pager:set &static=(cache-read $pagerCache)
} catch e { echo $e['reason'] >&2 }
} catch e { echo $e['reason'] >&2 }
}
fn init-instance-agent {
try {
agent:init-instance
} catch e { echo 'Agent: '(to-string $e['reason']) >&2 }
}
fn init-instance-nix-per-user-profile {
try {
if (os:is-dir $E:ROOT'/nix/store') {
use github.com/chlorm/elvish-util-wrappers/nix
nix:user-profile-init
}
} catch e { echo $e['reason'] >&2 }
}
fn init-instance-path {
set paths = [
(env:get $xdg-dirs:XDG-BIN-HOME)
(path:join (path:home) '.cargo' 'bin')
(path:join (path:home) 'go' 'bin')
$@paths
]
}
fn init-instance-prompt {
var alacritty = $false
try {
var _ = (get-env 'ALACRITTY_SOCKET')
set alacritty = $true
} catch _ { }
set edit:prompt = {
var user = $nil
fn pill-begin {
var p = "\ue0b6"
# Fix ligatures in alacritty
if $alacritty {
set p = $p' '
}
styled $p fg-black
}
fn pill-end {
var p = "\ue0b4 "
# Fix ligatures in Alacritty
if $alacritty {
set p = "\ue0b4"
}
styled $p fg-black
}
try {
set user = (env:get 'USER')
} catch _ {
set user = (env:get 'username')
}
pill-begin
styled $user fg-green bg-black
if (env:has 'SSH_CLIENT') {
styled-segment &dim=$true &fg-color=white '@' &bg-color=black
styled-segment (platform:hostname) &bold=$true &fg-color=red &bg-color=black
#} else {
# styled-segment &bold=$true &fg-color=white (platform:hostname)
}
pill-end
try {
var g = (status:status)
pill-begin
styled ' '$g['branch']['head']' ' fg-white bg-black
try {
styled '+'(status:count-unstaged $g) fg-green bg-black
} catch _ { }
try {
styled '~'(status:count-untracked $g) fg-yellow bg-black
} catch _ { }
try {
styled '-'(status:count-deleted $g) fg-red bg-black
} catch _ { }
pill-end
} catch _ { }
pill-begin
styled (tilde-abbr $pwd) fg-red bg-black
pill-end
styled-segment ' ' &fg-color=cyan
}
set edit:rprompt = { }
}
fn init-instance {
init-xdg-dir-vars
run-parallel ^
$init-instance-color-scheme~ ^
$init-instance-ls-colors~ ^
$init-instance-editor-env~ ^
$init-instance-pager-env~ ^
$init-instance-agent~ ^
$init-instance-nix-per-user-profile~ ^
$init-instance-path~ ^
$init-instance-prompt~
}