-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.nu
More file actions
172 lines (155 loc) · 4.72 KB
/
Copy pathmod.nu
File metadata and controls
172 lines (155 loc) · 4.72 KB
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
export-env {
$env.enverlay.autoload_direnv = null
$env.enverlay.excluded_direnv_vars = [name NAME LANG LOCALE_ARCHIVE]
# This are set by nix-based .envrc files, and you most likely don't want to change the ones you have already
$env.config.hooks.env_change.PWD = $env.config.hooks.env_change.PWD? | default [] | append {||
match $env.enverlay.autoload_direnv? {
# Auto load & unload:
"full" => { load }
# Only load if a .envrc is found. Else keep the env:
"load" if (".envrc" | path exists) => { load }
}
}
}
export def allow [dir: path = "."] {
cd $dir
^direnv allow
}
export def revoke [dir: path = "."] {
cd $dir
^direnv revoke
}
def __export [dir: path = "."] {
cd $dir
let proc = ^direnv export json | complete
let e = if (
($proc.stderr | is-empty)
or ($proc.stderr | lines | last | ansi strip) =~ "^direnv: (export|unloading)"
) {
$proc.stdout | from json | default {} | reject -o ...$env.enverlay.excluded_direnv_vars
} else {
error make {msg: $"direnv errored:\n($proc.stderr)"}
}
if ($e has PATH) {
$e | update PATH {split row (char env_sep)}
} else {$e}
}
export def status [] {
^direnv status --json | from json
}
# Tries to find a .envrc in the given directory, and loads it.
# The directory should be allowed with `direnv allow` first.
#
# If the directory doesn't contain a .envrc, unloads the current env
export def --env load [dir: path = "."] {
let jid = job spawn {
sleep 0.1sec
print -n $"(ansi grey)direnv loading...(ansi default)"
}
load-env (__export $dir)
try { job kill $jid }
let loadedRC = (status).state.loadedRC?
if $loadedRC != null and $loadedRC.allowed? != 0 {
error make {msg: $"($loadedRC.path) is not allowed"}
}
}
# Unloads the previously loaded .envrc
export def --env unload [] {
let d = mktemp --directory
cd $d
load
cd -
rm -rf $d
}
# Loads the .envrc if no env is currently loaded, else unload the current env
export def --env toggle [] {
if (status).state.loadedRC.allowed? == 0 {
unload
} else {
load
}
}
# Switch auto-(un)loading of the direnv on or off
#
# Without flags, auto-loading AND unloading are switched on
export def --env auto [
--load-only (-l) # Switch ONLY auto-loading on
--off (-o) # Switch everything off
] {
$env.enverlay.autoload_direnv = if $off {
null
} else if $load_only {
"load"
} else {
"full"
}
}
def direnv-folder [envrc_path] {
$envrc_path | path dirname | path basename
}
export def render [] {
let width = (term size).columns
let state = (status).state
let found = $state.foundRC.path?
let loaded = if $state.loadedRC.path? != null and $state.loadedRC.allowed == 0 {
$state.loadedRC.path
}
let found_allowed = $state.foundRC.allowed? == 0
let envs = [
...(
match ($env | get -o name) { # Nix shell/develop set the $name (lowercase) env var
null => []
$name => [$"(ansi blue)❄ ($name)(ansi reset)"]
}
)
...(
match [$found $loaded] {
[null null] => []
[$path null] => [$"(ansi grey)🖿 (ansi attr_italic)(if not $found_allowed {(ansi attr_strike)} else {""})(direnv-folder $path)(ansi reset)"]
[null $path] => [$"(ansi yellow)🗁 (direnv-folder $path)(ansi reset)"]
_ if ($found == $loaded) => [$"(ansi green)🗁 (direnv-folder $loaded)(ansi reset)"]
_ => [$"(ansi red)🗁 (direnv-folder $loaded)(ansi reset)"]
}
)
]
let direnv_auto_bit = if $env.enverlay.autoload_direnv? != null {
$"(ansi magenta)📂(match $env.enverlay.autoload_direnv {
"full" => "auto"
"load" => "auto-load"
})(ansi reset) "
}
let envs_bit = $envs | each {[$in ' ']} | flatten | str join ""
let num_shown_overlays = $width / 20 | into int
let overlays = overlay list | match ($in | describe) {
# Pre-Nu 0.107: 'active' column doesn't exist:
"list<string>" => {$in | wrap name | insert active true}
_ => $in
} | reverse
let shown_overlays = $overlays | take $num_shown_overlays
let hidden_overlays = $overlays | drop $num_shown_overlays
let sep = $"(ansi yellow)|(ansi reset)"
let overlays_bit = $shown_overlays |
each {|o|
if $o.active {
$o.name
} else {
$"(ansi grey)(ansi attr_strike)($o.name)(ansi reset)"
}
} |
if $hidden_overlays != [] {
append $"+($hidden_overlays | length)"
} else { $in } |
str join $sep
$"($direnv_auto_bit)($envs_bit)($overlays_bit)"
}
def cmd [cmd] {
{send: ExecuteHostCommand, cmd: $cmd}
}
export def default-keybindings [--prefix = "enverlay "] {
[
[modifier keycode event];
[alt char_a (cmd $'($prefix)toggle')]
[alt char_e (cmd $'($prefix)load')]
] | insert mode emacs
}
export alias export = __export