-
Notifications
You must be signed in to change notification settings - Fork 1
/
gelp.nu
223 lines (206 loc) · 6.27 KB
/
gelp.nu
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
#!/bin/envnu
# gelp.nu - yet another git helper
# Dependencies: fd-find
use utils.nu [venumerate, not-implemented]
# Parse git remote url
def parse-remote-url [
repo_path: path # FS repo path
] {
let git_remote = git -C $repo_path remote get-url origin
do -i { $git_remote | url parse | url join } | default (
$git_remote |
parse '{version_control}@{host}:{username}/{repo}.git' |
get 0 |
insert "scheme" "https" |
insert "path" $"($in.username)/($in.repo)" |
select host path scheme |
url join
)
}
# Get information on running docker containers
export def docker-info [
--verbose (-v)
] {
let info = (docker ps |
from ssv |
insert _ {
docker inspect -f '{{ json .Mounts }}' (
$in | get "CONTAINER ID"
) |
from json |
get 0
} |
flatten |
rename -b { str snake-case })
if $verbose { $info } else { $info | select names image status }
}
# Pull projects from filesystem and update database for faster project search
def update-project-cache [] {
fd -Hap -g "**/.git" $"($env.HOME)" -E ".cache" -E ".local" -E ".cargo" |
lines |
each { update-uses ($in | path dirname) --no-uses }
}
# Select project to open
def-env select-project [] -> record {
let docker_info = docker-info -v
let projects = get-project-history |
reject id |
default 0 uses |
sort-by score -r |
join ($docker_info | rename -c {"source": "project_dir"}) project_dir --left |
rename -b { str downcase | str trim | str replace -a ' ' '_' }
let project_idx = (
$projects |
each { |row|
if not ($in | get -i image | is-empty) {
$"($row.project_dir | path basename) \((ansi g)container: (ansi reset)($row.image)\)"
} else {
$"($row.project_dir | path basename)"
}
} |
venumerate |
input list -f |
split row ' ' |
first |
if ($in | is-empty) { return } else { into int }
)
let project = ($projects | get ($project_idx - 1))
$project
}
# Run action on project
def-env run-action [
project: record # Project to run action on
] {
let action = (["edit", "git", "cd", "open remote"] | input list -f $"Action for ($project.project_dir | path basename):")
update-uses $project.project_dir
if ($action == "edit") {
cd $project.project_dir; run-external ($env.EDITOR) ($project.project_dir)
} else if ($action == "git") {
cd $project.project_dir; gitui -d $project.project_dir
} else if ($action == "cd") {
cd $project.project_dir
} else if ($action == "open remote") {
open-remote $project.project_dir
}
}
# Open project remote in browser
export def open-remote [
project_dir?: path # Project directory to open. If none, default to recent
] {
if ($project_dir | is-empty) {
xdg-open (parse-remote-url (get-last-used | get project_dir))
} else {
xdg-open (parse-remote-url $project_dir) # TODO: Maybe change to current directory's project
}
}
### Database
# Update number of uses of a project
def update-uses [
project_dir: path # Project path
--no-uses # Default for projects stored but not yet used
] {
create-env
let use_date = (if $no_uses {
[0, ("01-01-1970" | into datetime | format date "%Y-%m-%dT%H:%M:%S")]
} else {
[1, (date now | format date "%Y-%m-%dT%H:%M:%S")]
})
open $env.gelp_db_dir | query db $"
INSERT INTO projects
\(project_dir, last_used, uses\)
VALUES \(
'($project_dir)',
'($use_date.1)',
($use_date.0)
\)
ON CONFLICT \(project_dir\) DO UPDATE SET
uses = uses + excluded.uses,
last_used = CASE WHEN excluded.uses = 0
THEN last_used
ELSE '($use_date.1)'
END;
"
}
# Get project history
def get-project-history [
--non-weighted # Don't use weighting function
] {
create-env
let project_history = (open $env.gelp_db_dir |
query db $"SELECT * FROM projects;" |
update last_used { || into datetime })
# TODO: better weighting
let weighting_function = if $non_weighted {
$in.item.uses
} else { # Weight using exponential decay of last use and number of uses
{(($in.index / ($project_history | length)) | math exp) * $in.item.uses}
}
$project_history |
enumerate |
insert score $weighting_function |
flatten
}
# Create SQLite database
def create-db [
db_path: path # Database path
] {
mkdir ($db_path | path dirname)
touch $db_path
open $db_path | query db "
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
project_dir TEXT NOT NULL UNIQUE,
uses INTEGER,
last_used TEXT NOT NULL
);
"
}
# Get last-used project
def get-last-used [] {
get-project-history | sort-by last_used | last
}
# Clear project use history
export def clear-history [] {
create-env
if (input "Are you sure? ") == "y" {
let num_entries = (open $env.gelp_db_dir | query db $"
SELECT * FROM projects;
DELETE FROM projects;
" | length)
open $env.gelp_db_dir | query db $"DELETE FROM projects;"
print $"($num_entries) row(s) deleted"
} else {
print "Cancelling..."
}
}
# Add some environment variables if not already present
def-env create-env [] {
$env.gelp_dir = ($env | get -i gelp_dir | default ($env.HOME | path join ".local/share/gelp"))
$env.gelp_db_dir = ($env.gelp_dir | path join "history.db")
}
# TODO: Not done
def add-sync-info [] {
let projects = $in
$projects |
merge ($projects | each { |project| do { git -C $project.project_dir rev-parse --abbrev-ref HEAD } | complete }) |
update stdout { |row| if ($row.exit_code == 0) and (($row.stdout | str trim) != "HEAD") { $row.stdout | str trim } else { null } } |
rename -c { stdout: curr_branch } |
reject exit_code stderr |
insert synced { |row|
if $row.curr_branch != null {
git -C $row.project_dir diff --numstat $row.curr_branch $"remotes/origin/($row.curr_branch)" | is-empty
} else {
false
}
}
}
# gelp.nu - yet another git helper
export def-env main [
--last (-l) # Bypass selector, use last project
--update-cache (-u) # Update cached projects
] {
if $update_cache { update-project-cache }
let project = if $last { get-last-used } else { (select-project) }
if ($project | is-empty) { return }
run-action $project
}