repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
scrollback.luaLua67 lines2.6 KiBraw download
-- scrollback.lua — kitty's copy mode (ctrl+a>[) as a Neovim buffer.
-- Started by home/modules/terminal.nix as
--   bash -c 'exec nvim 63<&0 0</dev/null -u <this file> -c "let g:kitty_input_line=INPUT_LINE_NUMBER" …'
-- kitty hands the scrollback (with colours) on fd 63; it is rendered through a
-- :terminal so the ANSI colours survive, then the buffer is left in normal
-- mode at the line that was under the cursor. Vi motions and search work as
-- usual; / ? n N, v V ctrl-v select, y copies to the clipboard (wl-copy),
-- Enter copies and leaves, q or Esc leave. Nothing from the AstroNvim config
-- is loaded (-u), so this starts instantly.

local o = vim.opt
o.number = false
o.relativenumber = false
o.list = false
o.showtabline = 0
o.foldcolumn = "0"
o.laststatus = 0
o.signcolumn = "no"
o.ruler = false
o.showmode = false
o.cmdheight = 1
o.scrollback = 100000
o.termguicolors = true
o.background = "dark"
o.clipboard = "unnamedplus" -- y → system clipboard
o.ignorecase = true
o.smartcase = true
o.incsearch = true
o.hlsearch = true
o.shell = "bash"

local function quit()
  vim.cmd("qa!")
end
local map = vim.keymap.set
map({ "n", "v" }, "q", quit, { silent = true, desc = "leave copy mode" })
map("n", "<Esc>", quit, { silent = true, desc = "leave copy mode" })
map("n", "i", "<Nop>") -- no insert in a dead terminal
map("n", "a", "<Nop>")
map("v", "y", '"+y', { silent = true, desc = "copy selection" })
map("v", "<CR>", '"+y<cmd>qa!<CR>', { silent = true, desc = "copy selection and leave (tmux)" })
map("n", "<CR>", "<Nop>")

vim.schedule(function()
  -- the -c flags have run by now, so the kitty positions are in vim.g
  local input_line = tonumber(vim.g.kitty_input_line) or 0
  local cursor_line = tonumber(vim.g.kitty_cursor_line) or 1
  local cursor_col = tonumber(vim.g.kitty_cursor_col) or 1

  vim.api.nvim_create_autocmd("TermEnter", { callback = function() vim.cmd("stopinsert") end })
  vim.api.nvim_create_autocmd("TermClose", {
    once = true,
    callback = function()
      vim.cmd("stopinsert")
      local target = math.max(0, input_line - 1) + cursor_line
      pcall(vim.fn.cursor, target, cursor_col)
      vim.cmd("normal! zz")
      if vim.env.KITTY_SCROLLBACK_TEST then
        -- smoke test (terminal.nix build check): print the text and leave
        io.stdout:write(table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n"), "\n")
        quit()
      end
    end,
  })
  -- strip kitty's OSC 8 file:// hyperlinks, which would print as noise
  vim.cmd([[terminal sed </dev/fd/63 -e 's/\x1b]8;;file:[^\\]*\\//g' && sleep 0.01 && printf '\x1b]2;\a']])
end)