-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotman
executable file
·130 lines (113 loc) · 2.78 KB
/
dotman
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
#!/usr/bin/env nu
export def main [] {
let repo_dir = "~/dotfiles" | path expand
let dotman_files = $"($repo_dir)/dotman_files"
if not ($repo_dir | path exists) {
error make {msg: $"($repo_dir) does not exist, please create it"}
}
if not ($dotman_files | path exists) {
error make {msg: "No files added to dotman_files, please add at least one file"}
}
let deps_table = check_deps ["bat" "fd" "gitui" "sk"]
if not ($deps_table | get missing | any { is-empty }) {
print "There are dependencies missing:"
return ($deps_table)
}
let files = open $"($repo_dir)/dotman_files"
| parse -r '~/(\S+)'
| values
| flatten
let all_deps = check_deps [
"bat"
"dunstify"
"fd"
"gitui"
"grimshot"
"mpv"
"pactl"
"sk"
"swaymsg"
"wl-copy"
"xdg-user-dir"
]
print "dotman - Dotfiles Manager"
print "by sirkhancision\n"
let options = [
"[1] Link dotfiles"
"[2] Edit file"
"[3] Open gitui"
"[4] Check dependencies for scripts"
"[0] Exit"
]
# main loop
while true {
mut choice = ($options
| input list -f "Options"
| parse -r '\[(\d)\] .+'
| get capture0.0
| into int)
match $choice {
0 => { return },
1 => { link_files $files $repo_dir },
2 => { edit_files $repo_dir },
3 => { ^gitui --directory $repo_dir --workdir $repo_dir },
4 => { print $all_deps },
_ => { "Invalid option" }
}
}
}
def check_deps [deps: list<string>]: nothing -> table<any> {
let deps_table = [[dependencies]; [null]]
| upsert dependencies $deps
| flatten
let missing_deps = $deps_table
| each {|x|
if (which $x.dependencies | is-empty) { $x.dependencies } }
let missing_deps_num = $missing_deps | length
$deps_table | enumerate | each {|x|
if $x.index < ($missing_deps_num) {
upsert missing $missing_deps
} else {
upsert missing null
}
}
| flatten
}
def edit_files [repo_dir: path] {
if ($env.EDITOR | is-empty) {
print "Default editor not set, defaulting to vi"
$env.EDITOR = "vi"
sleep 2sec
}
cd $repo_dir
let files = (^fd --type f --hidden)
while true {
mut file = ($files
| ^sk --multi --preview "bat {} --color=always"
| lines
| str join ' ')
if not ($file | is-empty) {
nu -c $"($env.EDITOR) ($file)"
} else {
break
}
}
}
def link_files [files: list<string>, repo_dir: path] {
let files_paths = [null]
| append (
$files
| par-each {|x|
if (($"($repo_dir)/($x)" | path type) == dir) {
glob $"($repo_dir)/($x)/*"
} else {
$"($repo_dir)/($x)"
}
}
)
| compact
| flatten
$files_paths | par-each {|x|
^ln -sfv $x ($x | str replace --regex 'dotfiles/' '' | path dirname)
}
}