-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedo-commands.el
262 lines (237 loc) · 10.2 KB
/
speedo-commands.el
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
252
253
254
255
256
257
258
259
260
261
262
;;; speedo-commands.el --- Commands for speedo.el -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2024 Nicholas Vollmer
;; Author: Nicholas Vollmer
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'speedo)
;;;###autoload
(defun speedo-new-db (title &optional category dir &rest segments)
"Write new database for game with TITLE to DIR.
DIR is optional and defaults to `speedo-directory'.
CATEGORY and SEGMENTS are added to the DB structure if provided.
When called interactivley, prompt for optional values."
(interactive "stitle: \nscategory: ")
(let* ((dir (expand-file-name (or dir speedo-directory default-directory)))
(category (if (string-empty-p category) nil category))
(default-file (let ((name
(replace-regexp-in-string
"\\(?:[[:space:]]+\\)" "-"
(concat title (when category (format "-%s" category)))))
(i 0)
(id ""))
(while (file-exists-p (expand-file-name name dir))
(setq name (string-remove-suffix id name)
name (concat name
(setq id (format "<%d>" (cl-incf i))))))
(concat name speedo--file-extension)))
(file (let ((input (read-file-name "Write DB to: " dir nil nil default-file)))
(if (string-suffix-p speedo--file-extension input)
input
(concat input speedo--file-extension))))
(index 0)
segment)
(unless (or segments (not (called-interactively-p 'interactive)))
(while (not (string-empty-p
(setq segment (read-string (format "segment %d (empty to exit): "
(cl-incf index))))))
(push segment segments))
(setq segments (nreverse segments)))
(speedo--write-data
(list :title title
:category category
:segments (mapcar (lambda (segment) (list :name segment))
segments))
file nil nil nil 'must-be-new)))
;;;###autoload
(defun speedo-import-splits (file)
"Import splits from JSON FILE.
Assumes splits.io exchange fomat:
https://github.com/glacials/splits-io/tree/master/public/schema"
(interactive "f")
(if-let* ((data (ignore-errors
(json-parse-string
(with-current-buffer (find-file-noselect file 'nowarn 'raw)
(buffer-string))
:object-type 'plist
:array-type 'list
:null-object nil
:false-object nil)))
(title (plist-get (plist-get data :game) :longname))
(category (plist-get (plist-get data :category) :longname))
(segments (mapcar (lambda (segment) (plist-get segment :name))
(plist-get data :segments))))
(apply #'speedo-new-db (append (list title category nil) segments))
(user-error "Unable to parse %S" file)))
(defun speedo-next ()
"Start the next segment or a new attempt.
If SKIP is non-nil, don't record a duration for the current segment."
(interactive)
(with-current-buffer speedo-buffer
(if (speedo--attempt-in-progress-p)
(let ((last (1- (length (plist-get speedo--current-attempt :segments)))))
(speedo--segment-end)
(when (= speedo--segment-index last) (speedo--attempt-end)))
(speedo--attempt-init))
(when (speedo--attempt-in-progress-p) (cl-incf speedo--segment-index))
(speedo--segment-start)
(speedo--display-ui)
(speedo--update-header)
(if (speedo--attempt-in-progress-p)
(speedo--goto-index)
(forward-line))))
(defun speedo-previous ()
"Select the previous segment."
(interactive)
(with-current-buffer speedo-buffer
(unless (speedo--attempt-in-progress-p) (user-error "No attempt in progress"))
(when (= speedo--segment-index 0) (user-error "No previous segment"))
;; clear out attempt data for this segment and the previous
(let ((current (speedo--current-segment)))
(setf current (plist-put current :duration nil)
current (plist-put current :skip nil)))
(cl-decf speedo--segment-index)
(let ((current (speedo--current-segment)))
(setf current (plist-put current :duration nil)
current (plist-put current :skip nil)))
(speedo--goto-index)
(speedo--display-ui)))
(defun speedo-skip ()
"Skip the current segment."
(interactive)
(with-current-buffer speedo-buffer
(unless (speedo--attempt-in-progress-p) (user-error "No attempt in progress"))
(let ((skipped (speedo--current-segment)))
(setf skipped (plist-put skipped :skip t)))
(speedo-next)))
(defun speedo-mistake ()
"Record a mistake in the current segment."
(interactive)
(if (speedo--attempt-in-progress-p)
(let ((current (speedo--current-segment)))
(setf current
(plist-put current :mistakes
(append (plist-get current :mistakes)
(list (- (speedo--timestamp)
(plist-get speedo--current-attempt :start))))))
(message "mistake recorded"))
(user-error "No run in progress")))
(defun speedo-reset ()
"Reset the current attempt if it is in progress.
If no attempt is in progress, clear the UI times."
(interactive)
(when (speedo--attempt-in-progress-p)
(setq speedo--current-attempt
(plist-put speedo--current-attempt :reset
(- (speedo--timestamp)
(plist-get speedo--current-attempt :start))))
(speedo--attempt-end))
(setq speedo--segment-index -1
speedo--state 'pre
speedo--current-attempt nil)
(speedo--clear)
(goto-char (point-min)))
;;;###autoload
(defun speedo-delete-attempts (attempts)
"Delete ATTEMPTS from current DB."
(interactive (list (speedo-read-attempt nil 'multiple)))
(setf (plist-get speedo--data :attempts)
(speedo--delete-attempts attempts speedo--data))
(let ((len (length attempts)))
(message "Deleted %d attempt%s" len (if (eq len 1) "" "s")))
(speedo--run-pb (speedo--attempts) 'nocache)
(when (get-buffer speedo-buffer)
(speedo--cache-targets)
(speedo--display-ui)))
(defun speedo-quit-window ()
"Quit the `speedo-buffer'."
(interactive)
(with-current-buffer speedo-buffer
(internal-show-cursor (selected-window) t) ;;show hidden cursor
(quit-window)))
(defun speedo-comparison-next (&optional n)
"Compare against Nth next standard in `speedo-comparison-targets'."
(interactive "p")
(speedo--nth-target (or n 1)))
(defun speedo-comparison-previous (&optional n)
"Compare against Nth next standard in `speedo-comparison-targets'."
(interactive "p")
(speedo--nth-target (- (or n 1))))
;;;###autoload
(defun speedo-load-file (&optional file hide)
"Load a splits FILE.
If HIDE is non-nil, do not display `speedo-buffer' after loading."
(interactive)
(when (speedo--attempt-in-progress-p)
(user-error "Cannot Load file while attempt is in progress"))
(when (bound-and-true-p speedo-edit--in-progress)
(user-error "Cannot Load file while attempt edit in progress"))
(let ((file (or file (read-file-name "Splits file: " speedo-directory
nil 'must-match nil
#'speedo--db-file-p))))
(when (and (speedo--data-modified-p)
(yes-or-no-p (format "%S modified. Save before loading %s? "
speedo--data-file file)))
;; Force because we just checked for modifications above
(speedo-save-file 'force))
(speedo--load-file file)
(speedo--cache-targets)
(speedo--target-attempt (cdar speedo-comparison-targets))
(unless hide
(pop-to-buffer (get-buffer-create speedo-buffer))
(speedo-mode)
(goto-char (point-min)))))
;;;###autoload
(defun speedo ()
"Open the splits buffer."
(interactive)
(if speedo--data
(unless (string= (buffer-name (current-buffer)) speedo-buffer)
(pop-to-buffer (get-buffer-create speedo-buffer) nil)
(when speedo-hide-cursor (speedo--hide-cursor))
(unless (derived-mode-p 'speedo-mode) (speedo-mode)))
(speedo-load-file
(when speedo-default-splits-file
(expand-file-name speedo-default-splits-file speedo-directory)))))
;;;###autoload
(defun speedo-save-file (&optional force)
"Save `speedo--data' to `speedo--data-file'.
If FORCE is non-nil, save without checking if data has been modified."
(interactive "P")
(if (or force (speedo--data-modified-p))
(speedo--write-data
(speedo--convert-data speedo--data 'human)
speedo--data-file)
(message "(No changes need to be saved)")))
(declare-function speedo-edit-attempt "speedo-edit" (attempt))
(defun speedo-post-edit-last (attempt)
"Update UI to display edited ATTEMPT."
(setq speedo--current-attempt attempt
speedo--time (speedo--segments-duration (plist-get attempt :segments)))
(speedo)
(speedo--display-ui)
(speedo--update-header)
(remove-hook 'speedo-edit-finalize-functions #'speedo-post-edit-last))
;;;###autoload
(defun speedo-edit-last-attempt ()
"Edit most recent attempt."
(interactive)
(add-hook 'speedo-edit-finalize-functions #'speedo-post-edit-last)
(if (speedo--attempt-in-progress-p)
(user-error "Cannot edit while attempt in progress")
(if-let ((last (speedo-target-last-attempt)))
(speedo-edit-attempt last)
(user-error "No last attempt found"))))
(provide 'speedo-commands)
;;; speedo-commands.el ends here