-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolkit.nu
148 lines (128 loc) · 4 KB
/
toolkit.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
def --wrapped main [...rest] {
nu -c $'use toolkit.nu; toolkit ($rest | str join " ")'
}
export def download-challenge [title challenge_id description] {
let challenge_path = $title | str downcase | str replace --all -r '\s' '-'
mkdir $challenge_path
http get $"https://vimgolf.com/challenges/($challenge_id).json"
| do {|response|
$response.in.data
| save -f ([$challenge_path input.txt] | path join)
$response.out.data
| save -f ([$challenge_path output.txt] | path join)
} $in
[
$"# [($title)]\(https://www.vimgolf.com/challenges/($challenge_id))"
$description
"## Input"
"```"
(open ([$challenge_path input.txt] | path join))
"```"
"## Output"
"```"
(open ([$challenge_path output.txt] | path join))
"```"
]
| str join (char newline)
| save -f ([$challenge_path README.md] | path join)
}
export def get-latest-challenge [] {
http get http://feeds.vimgolf.com/latest-challenges
| get content.content.0
| where tag == item
| get 0.content
| {
title: ($in | where tag == title | get 0.content.0.content)
challenge_id: ($in | where tag == link | get 0.content.0.content | parse 'https://www.vimgolf.com/challenges/{challenge_id}' | get 0.challenge_id)
description: ($in | where tag == description | get 0.content.0.content)
}
| do {|challenge|
download-challenge $challenge.title $challenge.challenge_id $challenge.description
print $"Downloaded: ($challenge.title)"
} $in
null
}
export def run-challenge [] {
let challenge = ls
| where type == dir
| get name
| str join (char newline)
| ^fzf --prompt="Run Challenge: " --reverse --height=20%
let mode = ls $challenge
| where name =~ mode.txt
| get name
| path parse
| get stem
| str join (char newline)
| ^fzf --prompt="With Mode: " --reverse --height=20%
match $mode {
ex-mode => { run-ex-mode $challenge },
normal-mode => { run-normal-mode $challenge }
insert-mode => { run-insert-mode $challenge }
}
^git diff --exit-code --no-index ($challenge | path join input.txt) ($challenge | path join output.txt)
| complete
| if $in.exit_code == 0 {
print $"($challenge): Succeed"
} else {
print $in.stdout
print $"($challenge): Failed"
}
^git restore ($challenge | path join input.txt)
}
def run-ex-mode [challenge] {
open ($challenge | path join ex-mode.txt)
| ^nvim -e -s ($challenge | path join input.txt)
}
def run-normal-mode [challenge] {
^nvim -s ($challenge | path join normal-mode.txt) ($challenge | path join input.txt)
}
def run-insert-mode [challenge] {
print "TODO: Can't run insert mode yet"
#^nvim -c 'startinsert' -s ($challenge | path join normal-mode.txt) ($challenge | path join input.txt)
}
export def try-challenge [] {
let challenge = ls
| where type == dir
| get name
| str join (char newline)
| ^fzf --prompt="Try Challenge: " --reverse --height=20%
let mode = modes
| str join (char newline)
| ^fzf --prompt="With Mode: " --reverse --height=20%
match $mode {
ex-mode => { try-ex-mode $challenge },
normal-mode => { try-normal-mode $challenge }
insert-mode => { try-insert-mode $challenge }
}
}
def try-ex-mode [challenge] {
^nvim -e -W ($challenge | path join ex-mode.txt) ($challenge | path join input.txt)
}
def try-normal-mode [challenge] {
^nvim -W ($challenge | path join normal-mode.txt) ($challenge | path join input.txt)
}
def try-insert-mode [challenge] {
^nvim -c 'startinsert' -W ($challenge | path join insert-mode.txt) ($challenge | path join input.txt)
}
export def update-readme [] {
ls */*-mode.txt
| rename --column { size: score }
| insert challenge { get name | path parse | get parent }
| insert mode { get name | path parse | get stem | str replace '-mode' '' }
| group-by challenge
| transpose challenge data
| each {|it|
$it.data
| reduce --fold { challenge: $it.challenge } {|it, acc|
$acc | insert $it.mode $it.score
}
}
}
def modes [] {
[
ex
normal
insert
]
}