Covert terminal file writing/reading

I occasionally run into the phenomenon of my spouse not being able to "let live" when it comes to online activity/participation.

To the rescue cometh a pair of scripts I wrote facilitating writing/reading text files in the terminal as covertly as possible.

Writing (well, appending)

I think I previous posted a lesser version of the "write" aid, which I've named "o", which I believe was short for "one" when I first wrote it, the implication being "let me covertly append one line at a time to a file".

Invocation looks like this:

$ o  [anything as a second argument, telling "o" to add two newlines instead of one to each appended line]

The script disable terminal stty echoing, then goes into a loop to receive lines of text and append them to the specified file. It re-enables stty echoing upon being directed to exit by entering a ^d.

The Lua code:

#! /usr/bin/env lua
local eol = '\n'
if arg[2] then
  eol = '\n\n'
end
local out = nil
if arg[1] then
  out = io.open(arg[1], 'a')
  if not out then
    print('=== could not open file for write: "' .. arg[1] .. '"')
    os.exit()
  end
else
  print('=== missing filename argument')
  os.exit()
end
os.execute('stty -echo')
while true do
  os.execute('clear')
  local line = io.stdin:read()
  if line then
    out:write(line .. eol)
    out:flush()
  else
    break
  end
end
os.execute('stty echo')

Reading

Covert reading is accomplished by a script I call "one", whose invocation looks like:

$ one 

It reads the file in its entirety, then clears the screen and displays the first line, then waits for input that directs moving forward or backward in the file, or exits. Assuming movement was directed, the script clears the screen and displays the new line, again waiting for more subcommand input.

It's a bit clunky, but so is having to "splain" why I'm reading whatever nonsense I might be reading.

Subcommands to the read loop:

I went with 'x' or 'q' to quit instead of ^d because I wanted to be able to move backward more quickly.

The Lua code:

#! /usr/bin/env lua
local handle = nil
if arg[1] then
  handle = io.open(arg[1], 'r')
  if not handle then
    print('=== could not open file for read: "' .. arg[1] .. '"')
    os.exit()
  end
  local lines = {}
  for line in handle:lines() do
    table.insert(lines, line)
  end
  handle:close()
  if #lines > 0 then
  local i = 1
    while true do
      os.execute('clear')
      io.stdout:write(i .. ') ' .. lines[i] .. ' ')
      local command = io.stdin:read()
      if command then
        if (command == 'n') or (command == 'j') or (command == '') then
          i = i + 1
          if i > #lines then
            i = #lines
          end
        elseif (command == 'p') or (command == 'k') then
          i = i - 1
          if i < 1 then
            i = 1
          end
        elseif (command == 'q') or (command == 'x') then
          os.execute('clear')
          break
        else
          command = tonumber(command)
          if command then
            i = i + command
            if i > #lines then
              i = #lines
            elseif i < 1 then
              i = 1
            end
          end
        end
      else
        i = i - 1
        if i < 1 then
          i = 1
        end
      end
    end
  end
else
  print('Usage: one ')
  print('SUBCOMMANDS:')
  print('- [n|j] to advance one')
  print('- [p|k]|^d to go back one')
  print('-  to advance or go back ( can be negative) that amount')
  print('- [x|q] to quit')
end
Proxy Information
Original URL
gemini://tilde.club/~oldernow/2023-11-26-13-38-37.gmi
Status Code
Success (20)
Meta
text/gemini; lang=en
Capsule Response Time
435.965166 milliseconds
Gemini-to-HTML Time
0.597214 milliseconds

This content has been proxied by September (3851b).