Separate card-correction from review interface
John Smith

John Smith commited on 2022-302 14:17:10
Showing 2 changed files, with 51 additions and 43 deletions.


Marking a card for correction during review will make it be listed
at the end of the session, without permitting edits.
Use the "c" option (with a prompt index, as with "t")
to open the card's text in the $EDITOR and correct it.
... ...
@@ -157,6 +157,35 @@ local actions = {
157 157
         end,
158 158
     },
159 159
 
160
+    c = {
161
+        desc = "(c)orrect card",
162
+        nd = true,
163
+        ni = true,
164
+        fn = function(config, deck, _, ip)
165
+            local sp = deck[ip[1]][ip[2]]
166
+            local rt = sp:spt()
167
+            local fname = "memoire_cc.tmp"
168
+            local fh = io.open(fname, "w")
169
+            fh:write(rt)
170
+            fh:close()
171
+            os.execute(string.format(
172
+                "%s %q",
173
+                os.getenv("EDITOR") or "vi",
174
+                fname
175
+            ))
176
+            fh = io.open(fname, "r")
177
+            local nt = fh:read("a"):gsub("%s+$", ""):gsub("^%s+", "")
178
+            fh:close()
179
+            os.remove(fname)
180
+            if nt ~= sp:spt() and nt ~= "" then
181
+                local cardrec = Prompt:torec(deck[ip[1]])
182
+                cardrec.PromptText = { nt }
183
+                deck[ip[1]] = Prompt:fromrecs({ cardrec }, config)[1]
184
+                return deck
185
+            end
186
+        end,
187
+    },
188
+
160 189
     h = {
161 190
         desc = "(h)elp",
162 191
         nd = false,
... ...
@@ -241,7 +270,7 @@ local actions = {
241 270
                 local resp = io.read("l")
242 271
                 local sc = succfromstr(resp)
243 272
                 if sc == -1 then
244
-                    table.insert(editqueue, ip[1])
273
+                    table.insert(editqueue, ip)
245 274
                 else
246 275
                     if sc == 0 then
247 276
                         lrc = lrc + 1
... ...
@@ -265,35 +294,9 @@ local actions = {
265 294
                 ))
266 295
             end
267 296
             if #editqueue > 0 then
268
-                io.write(string.format(
269
-                    "\n%d cards queued for editing; press Enter to proceed",
270
-                    #editqueue
271
-                ))
272
-                io.flush()
273
-                io.read("l")
274
-                for ind, ci in ipairs(editqueue) do
275
-                    local pr = deck[ci][1]
276
-                    if pr then
277
-                        io.write(string.format(
278
-                            "Card #%d:\n%s\nCancel correction / Delete / Edit ",
279
-                            ci, pr:spt()
280
-                        ))
281
-                        io.flush()
282
-                        local resp = io.read("l"):sub(1, 1):lower()
283
-                        if resp == "d" then
284
-                            deck[ci][1].deleted = true
285
-                        elseif resp == "e" then
286
-                            io.write("Enter edited version:\n")
287
-                            local ev = io.read("l")
288
-                            local cardrec = Prompt:torec(deck[ci])
289
-                            cardrec.PromptText = { ev }
290
-                            deck[ci] = Prompt:fromrecs({ cardrec }, config)[1]
291
-                        -- assume all other inputs to mean Cancel
292
-                        else
293
-                        end
294
-                    else
295
-                        io.write(string.format("Card #%d is invalid!\n", ci))
296
-                    end
297
+                io.write(string.format("The following cards are marked for correction:\n"))
298
+                for _, ip in ipairs(editqueue) do
299
+                    io.write(string.format("%d:%d %s\n", ip[1], ip[2], deck[ip[1]][ip[2]]))
297 300
                 end
298 301
             end
299 302
             if climn > 0 then
... ...
@@ -313,19 +316,8 @@ local actions = {
313 316
     t = {
314 317
         desc = "(t)oggle suspension of prompt",
315 318
         nd = true,
316
-        fn = function(config, deck)
317
-            local ip
318
-            if arg[2] then
319
-                local a, b = arg[2]:match("(%d+):(%d+)")
320
-                if a and b then
321
-                    -- a and b are valid numbers because they came from a match
322
-                    ip = { tonumber(a), tonumber(b) }
323
-                end
324
-            end
325
-            if not ip then
326
-                io.write("Bad prompt index (must be cardnum:promptnum)\n")
327
-                return nil
328
-            end
319
+        ni = true,
320
+        fn = function(config, deck, _, ip)
329 321
             local sp = deck[ip[1]][ip[2]]
330 322
             local dir
331 323
             if sp.delay < 0 then
... ...
@@ -81,7 +81,23 @@ math.randomseed(os.time())
81 81
 if action.nd then
82 82
     local deck = loader.load(deckfname, config)
83 83
     if deck then
84
+        if action.ni then
85
+            local ip
86
+            if arg[2] then
87
+                local a, b = arg[2]:match("(%d+):(%d+)")
88
+                if a and b then
89
+                    -- a and b are valid numbers because they came from a match
90
+                    ip = { tonumber(a), tonumber(b) }
91
+                end
92
+            end
93
+            if ip then
94
+                deck = action.fn(config, deck, deckfname, ip)
95
+            else
96
+                io.write("Bad prompt index (must be cardnum:promptnum)\n")
97
+            end
98
+        else
84 99
             deck = action.fn(config, deck, deckfname)
100
+        end
85 101
         if deck then
86 102
             loader.save(deckfname, deck)
87 103
         end
88 104