Check if standard input is a terminal
John Smith

John Smith commited on 2022-133 23:50:55
Showing 1 changed files, with 18 additions and 1 deletions.


Memoire will now check if its standard input is a terminal,
and alter some behaviour accordingly:
- the message output at the start of the add-card interface
    is more suitable for an automated process when not reading
    from a terminal
- the review process will be cancelled when not reading from a terminal
... ...
@@ -109,6 +109,14 @@ local function disptime(ts)
109 109
     end
110 110
 end
111 111
 
112
+--[[
113
+    check if standard input is a terminal
114
+]]
115
+local function isatty()
116
+    local succ = os.execute("tty >/dev/null")
117
+    return succ
118
+end
119
+
112 120
 --[[
113 121
     check action based on first command-line argument;
114 122
     if nothing valid given, default to help
... ...
@@ -186,7 +194,11 @@ math.randomseed(os.time())
186 194
 if action == "a" then
187 195
     local deck = loader.load(deckfname)
188 196
     if deck then
197
+        if isatty() then
189 198
             io.write("Reading prompts -- press Ctrl-D to stop\n")
199
+        else
200
+            io.write("Reading prompts ...\n")
201
+        end
190 202
         local rt = io.read("a")
191 203
         local cardctr = 0
192 204
         local prctr = 0
... ...
@@ -219,7 +231,12 @@ elseif action == "h" then
219 231
     ))
220 232
 -- Review session
221 233
 elseif action == "r" then
222
-    local deck = loader.load(deckfname)
234
+    local deck
235
+    if isatty() then
236
+        deck = loader.load(deckfname)
237
+    else
238
+        io.write("Review should be done in the terminal\n")
239
+    end
223 240
     if deck then
224 241
         -- collect list of index-pairs for prompts to be reviewed this session
225 242
         local ctrt = review.list(deck, function(pr)
226 243