From 50ad84da920fe42a4b89f5fa9834beb2b1822d04 Mon Sep 17 00:00:00 2001 From: Joshua Cold Date: Mon, 23 Nov 2026 09:57:40 -0700 Subject: [PATCH] feat(passwords): show each lookup's progress, and pick logins from a searchable float While the password managers unlock and search, a box in the top-right corner shows a line for each: what it's doing (waiting for a master password, unlocking, searching), a bar while it reads entries, then the logins it found or its error, and finally what was filled. The backends report this through a new optional progress callback on find. A site with several logins now opens a float page that filters them as you type: Enter fills, Alt-u only the username, Alt-p only the password. ps (:password-search) opens it even for one login. The page sees only each login's name; the passwords stay in Lua. Needs riptide 0.8 for centred float pages that take every key. Co-Authored-By: Claude Opus 5.5 --- bitwarden/lua/bitwarden/init.lua | 12 +- keepassxc/lua/keepassxc/init.lua | 14 +- pass/lua/pass/init.lua | 3 +- pass/test/pass_spec.lua | 26 ++- passwords/README.md | 16 +- passwords/lua/passwords/init.lua | 275 +++++++++++++++++++++++++----- passwords/lua/passwords/site.lua | 6 +- passwords/pages/picker.html | 14 ++ passwords/pages/picker.js | 186 ++++++++++++++++++++ passwords/pages/style.css | 109 ++++++++++++ passwords/riptide-plugin.toml | 3 +- passwords/test/passwords_spec.lua | 63 ++++++- rbw/lua/rbw/init.lua | 7 +- 13 files changed, 666 insertions(+), 68 deletions(-) create mode 100644 passwords/pages/picker.html create mode 100644 passwords/pages/picker.js create mode 100644 passwords/pages/style.css diff --git a/bitwarden/lua/bitwarden/init.lua b/bitwarden/lua/bitwarden/init.lua index c70c537..adcafde 100644 --- a/bitwarden/lua/bitwarden/init.lua +++ b/bitwarden/lua/bitwarden/init.lua @@ -17,7 +17,7 @@ function M.new(opts) local session local backend = {} - local function unlock(cb) + local function unlock(cb, progress) if session then return cb(session) end @@ -26,6 +26,7 @@ function M.new(opts) if not password or password == "" then return cb(nil) end + progress("unlocking the vault") site.run(rt.spawn, { cmd, "unlock", "--passwordenv", "RT_BW_PASSWORD", "--raw" }, { env = { RT_BW_PASSWORD = password } }, function(out) @@ -35,6 +36,7 @@ function M.new(opts) function(err) cb(nil, err) end) end local function ask() + progress("waiting for your master password") rt.ui.input({ prompt = "Bitwarden master password", secret = true }, with) end -- The master password saved on the Plugins tab, from the OS keyring (riptide 0.4+). @@ -47,11 +49,13 @@ function M.new(opts) end end - local function search(host, cb, retried) + local function search(host, cb, progress, retried) + progress = progress or function() end unlock(function(key, err) if not key then return cb(nil, err) end + progress("searching the vault") site.run(rt.spawn, { cmd, "list", "items", "--search", site.search_term(host) }, { env = { BW_SESSION = key } }, function(out) local ok, items = pcall(rt.json.decode, out) @@ -83,9 +87,9 @@ function M.new(opts) if retried or not (e:lower():find("lock") or e:lower():find("session")) then return cb(nil, e) end - search(host, cb, true) + search(host, cb, progress, true) end) - end) + end, progress) end backend.find = search diff --git a/keepassxc/lua/keepassxc/init.lua b/keepassxc/lua/keepassxc/init.lua index 862652f..8d966b7 100644 --- a/keepassxc/lua/keepassxc/init.lua +++ b/keepassxc/lua/keepassxc/init.lua @@ -57,12 +57,13 @@ function M.new(opts) return out end - local function with_password(cb) + local function with_password(cb, progress) if cached and remember > 0 and os.time() - cached_at < remember then return cb(cached) end cached = nil local function ask() + progress("waiting for the database password") rt.ui.input({ prompt = "KeePassXC database password", secret = true }, function(password) if not password then return cb(nil) @@ -83,12 +84,14 @@ function M.new(opts) end end - function backend.find(host, cb) + function backend.find(host, cb, progress) + progress = progress or function() end with_password(function(password) if not password then return cb(nil) end local input = { stdin = password .. "\n" } + progress("opening the database") site.run(rt.spawn, argv("search", site.search_term(host)), input, function(out) local paths = {} for _, path in ipairs(site.lines(out)) do @@ -96,6 +99,7 @@ function M.new(opts) table.insert(paths, path) end end + progress("reading entries", 0, #paths) site.each(paths, function(path, done) site.run(rt.spawn, argv("show", "-s", "-a", "Title", "-a", "UserName", "-a", "Password", "-a", "URL", path), input, function(shown) @@ -111,13 +115,15 @@ function M.new(opts) done({ label = label, saved = { url, title }, login = { username = username ~= "" and username or nil, password = pass } }) end, function() done(nil) end) - end, function(entries) cb(entries) end) + end, function(entries) cb(entries) end, function(n, total) + progress("reading entries", n, total) + end) end, function(err) -- Most likely a wrong password; don't keep it. cached = nil cb(nil, err) end) - end) + end, progress) end function backend.get(entry, cb) diff --git a/pass/lua/pass/init.lua b/pass/lua/pass/init.lua index ed044a9..4e187be 100644 --- a/pass/lua/pass/init.lua +++ b/pass/lua/pass/init.lua @@ -49,7 +49,8 @@ function M.new(opts) local cmd = command(opts) local backend = {} - function backend.find(host, cb) + function backend.find(host, cb, progress) + if progress then progress("listing the store") end local argv, run_opts if gopass then argv = { cmd, "ls", "--flat" } diff --git a/pass/test/pass_spec.lua b/pass/test/pass_spec.lua index 6f01212..39bb52a 100644 --- a/pass/test/pass_spec.lua +++ b/pass/test/pass_spec.lua @@ -22,21 +22,35 @@ local function open_login() run("open " .. page("login.html")) wait_for("load_finished", { pattern = "plugin-test.localhost" }) end +-- The passwords picker, once its logins have arrived. +local function open_picker() + local passwords = require("passwords") + wait_until(function() + local p = passwords.picker() + return p and p.ready + end) + wait(200) + return passwords.picker() +end + describe("pass", function() it("offers the site's entries and fills the one picked", function() open_login() run("password-fill") - -- Two entries belong here; the lookalike sites' don't. The picker's keys - -- follow the entries' order. - wait(500) - keys("1") + -- Two entries belong here; the lookalike sites' don't. + local picker = open_picker() + picker.type("alice") + wait(200) + picker.key("Enter") wait_field("pass", "pw-alice") assert.equals("alice@example.com", field("user")) -- The username alone, from the path after the site. run("password-fill-username") - wait(500) - keys("2") + picker = open_picker() + picker.type("bob") + wait(200) + picker.key("Enter") wait_field("user", "bob") assert.equals("pw-alice", field("pass"), "only the username changed") end) diff --git a/passwords/README.md b/passwords/README.md index cfd0b45..0da4894 100644 --- a/passwords/README.md +++ b/passwords/README.md @@ -12,14 +12,22 @@ one with it. | [keepassxc](../keepassxc) | a KeePassXC database (`keepassxc-cli`) | `pp` (`:password-fill`) fills the username and password, `pu` -the username and `pw` the password. With several password managers on, -the picker lists the logins of all of them. Add this plugin yourself only to -change its options: +the username and `pw` the password. While the password managers unlock +and search, a box in the top-right corner shows how each is getting on, then +what was filled. + +When the site has several logins, a picker opens over the page: type to filter +them, Enter fills, Alt-u fills only the username and Alt-p only the password. +`ps` (`:password-search`) opens it even for one login. With several +password managers on, it lists the logins of all of them. The picker only sees +each login's name; the passwords stay with the plugin. + +Add this plugin yourself only to change its options: ```lua rt.pack.add({ "https://github.com/joshzcold/riptide-plugins", subdir = "passwords", opts = { submit = true, -- press the form's button after :password-fill - keys = { login = "" }, -- your own keys; false for none + keys = { login = "", search = "" }, -- your own keys; false for none } }) ``` diff --git a/passwords/lua/passwords/init.lua b/passwords/lua/passwords/init.lua index bbdf584..8c7e1f2 100644 --- a/passwords/lua/passwords/init.lua +++ b/passwords/lua/passwords/init.lua @@ -11,8 +11,9 @@ local backends, order = {}, {} local opts = {} local bound = {} --- A password manager plugin's backend: find(host, cb) gives cb a list of --- { label, saved = { addresses, names or path parts }, ... } or (nil, err); +-- A password manager plugin's backend: find(host, cb, progress) gives cb a +-- list of { label, saved = { addresses, names or path parts }, ... } or +-- (nil, err), and may call progress(text, n, total) as it works; -- get(entry, cb) gives cb { username, password } or (nil, why); forget(), -- if any, drops an unlocked session. `backend` nil removes one. function M.register(name, backend) @@ -25,21 +26,131 @@ function M.register(name, backend) backends[name] = backend end --- Find the logins for the current site in every backend, let the user pick --- one, and fill it. `what` is "login", "username" or "password". -local function fill(what) - local host = site.host(rt.url()) - if not host then - return rt.notify("passwords: this page has no site to fill a login for", "error") +local SPINNER = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } +-- The status float waits this long, so a quick lookup doesn't flash it. +local SHOW_AFTER = 250 + +local function bar(n, total) + local width = 16 + local filled = total > 0 and math.floor(width * n / total + 0.5) or 0 + return string.rep("█", filled) .. string.rep("░", width - filled) +end + +-- The status float of the lookup in progress; its last lines are kept in +-- M.status for the tests. +local status + +local function status_lines(s) + local lines = { { { s.title, "title" } } } + for _, row in ipairs(s.rows) do + local line = { { row.name, "accent" }, { " " } } + if row.error then + line[1] = { "✗ " .. row.name, "error" } + table.insert(line, { row.error, "error" }) + elseif row.found then + line[1] = { "✓ " .. row.name, "accent" } + table.insert(line, { row.found == 1 and "1 login" or (row.found .. " logins"), "muted" }) + else + line[1] = { SPINNER[s.frame % #SPINNER + 1] .. " " .. row.name, "accent" } + if row.total then + table.insert(line, { bar(row.n or 0, row.total), "accent" }) + table.insert(line, { string.format(" %s %d of %d", row.text, row.n or 0, row.total), "muted" }) + else + table.insert(line, { (row.text or "starting") .. "…", "muted" }) + end + end + table.insert(lines, line) end - if #order == 0 then - return rt.notify("passwords: no password manager plugin is on; add pass, rbw, bitwarden or keepassxc", "error") + if s.footer then table.insert(lines, s.footer) end + return lines +end + +local function draw(s, timeout) + if status ~= s then return end + local lines = status_lines(s) + M.status = lines + if s.float then + if s.float:is_open() then s.float:update({ lines = lines, timeout = timeout }) end + elseif s.shown or timeout then + s.float = rt.ui.float({ title = "passwords", lines = lines, position = "top-right", width = 64, timeout = timeout }) end +end + +-- A status float for a lookup of `names` on `host`, with a row each. +local function start_status(host, names) + if status then M.end_status() end + local s = { title = "Logins for " .. host, rows = {}, by_name = {}, frame = 0 } + for _, name in ipairs(names) do + local row = { name = name } + table.insert(s.rows, row) + s.by_name[name] = row + end + status = s + local ticks = 0 + s.ticker = rt.every(100, function() + ticks = ticks + 1 + s.frame = s.frame + 1 + if not s.shown and ticks * 100 >= SHOW_AFTER then s.shown = true end + draw(s) + end) + draw(s) + return s +end + +-- Ends the status float: closes it, or leaves `footer` up for a moment. +function M.end_status(footer, highlight) + local s = status + if not s then + -- After the picker, the lookup's float is gone: say what happened in a new one. + if footer then + M.status = { { { footer, highlight or "info" } } } + rt.ui.float({ title = "passwords", lines = M.status, position = "top-right", width = 64, + timeout = highlight == "error" and 8000 or 3000 }) + end + return + end + if s.ticker then s.ticker:stop() end + if footer then + s.footer = { { footer, highlight or "info" } } + s.shown = true + draw(s, highlight == "error" and 8000 or 3000) + else + M.status = status_lines(s) + if s.float and s.float:is_open() then s.float:close() end + end + status = nil +end + +-- Asks `name` for the logins saved for `host`, keeping its status row up to date. +local function ask_backend(s, name, host, done) + local row = s.by_name[name] + local answered = false + backends[name].find(host, function(entries, err) + if answered then return end + answered = true + if err then + row.error = err + elseif entries then + row.found = #entries + else + row.error = "cancelled" + end + draw(s) + done({ name = name, entries = entries, err = err }) + end, function(text, n, total) + if answered then return end + row.text, row.n, row.total = text, n, total + draw(s) + end) +end + +-- Finds the logins for `host` in every backend, checked against the site: +-- `done(found, answered)`, with `found` a list of { name, entry }. +local function lookup(host, done) local names = { table.unpack(order) } - site.each(names, function(name, done) - backends[name].find(host, function(entries, err) - done({ name = name, entries = entries, err = err }) - end) + local s = start_status(host, names) + site.each(names, function(name, finished) + ask_backend(s, name, host, finished) end, function(results) local found, answered = {}, false for _, result in ipairs(results) do @@ -54,36 +165,106 @@ local function fill(what) end end end + done(found, answered, s) + end) +end + +-- Fills `choice` ({ name, entry }) on `host`; `what` is "login", "username" or "password". +local function use(choice, host, what) + backends[choice.name].get(choice.entry, function(login, why) + if not login then + M.end_status("✗ " .. tostring(why), "error") + return rt.notify("passwords: " .. tostring(why), "error") + end + rt.page.fill_login({ + host = host, + username = what ~= "password" and login.username or nil, + password = what ~= "username" and login.password or nil, + submit = what == "login" and opts.submit or false, + }) + local filled = what == "login" and "Filled " or ("Filled the " .. what .. " of ") + M.end_status("✓ " .. filled .. choice.entry.label) + end) +end + +-- The open picker page, if any. +local picker + +-- Lets you pick one of `found` on a float page, filtering as you type. +local function pick(found, host, what) + if picker then picker.page:close() end + local p = { found = found } + picker = p + local items = {} + for i, choice in ipairs(found) do + items[i] = { id = i, manager = choice.name, label = choice.entry.label } + end + p.page = rt.ui.page({ + path = "picker.html", + where = "float", + position = "center", + title = "Login for " .. host, + width = 640, + height = math.min(360, 100 + #items * 21), + keys = "all", + on_message = function(name, data, page) + if picker ~= p then return end + if name == "ready" then + p.ready = true + page:send("items", { host = host, what = what, items = items }) + elseif name == "choose" then + picker = nil + page:close() + local choice = found[tonumber(data.id)] + local how = ({ login = true, username = true, password = true })[data.what] and data.what or what + if choice then use(choice, host, how) end + elseif name == "close" then + picker = nil + page:close() + M.end_status() + end + end, + }) +end + +-- The open picker, for tests: whether its items have arrived, and keys for it. +function M.picker() + if not picker then return nil end + local p = picker + return { + ready = p.ready == true, + type = function(text) p.page:send("type", { text = text }) end, + key = function(key) p.page:send("key", { key = key }) end, + } +end + +-- Find the logins for the current site in every backend, let the user pick +-- one, and fill it. `what` is "login", "username" or "password"; `always_pick` +-- opens the picker even for a single login. +local function fill(what, always_pick) + local host = site.host(rt.url()) + if not host then + return rt.notify("passwords: this page has no site to fill a login for", "error") + end + if #order == 0 then + return rt.notify("passwords: no password manager plugin is on; add pass, rbw, bitwarden or keepassxc", "error") + end + lookup(host, function(found, answered) if #found == 0 then -- Nothing answered: every question was cancelled, or each one failed. - if answered then rt.notify("passwords: no login saved for " .. host, "warning") end + if answered then + M.end_status("No login saved for " .. host, "warning") + rt.notify("passwords: no login saved for " .. host, "warning") + else + M.end_status() + end return end - local several = #names > 1 - local function use(choice) - backends[choice.name].get(choice.entry, function(login, why) - if not login then - return rt.notify("passwords: " .. tostring(why), "error") - end - rt.page.fill_login({ - host = host, - username = what ~= "password" and login.username or nil, - password = what ~= "username" and login.password or nil, - submit = what == "login" and opts.submit or false, - }) - end) - end - if #found == 1 then - return use(found[1]) + if #found == 1 and not always_pick then + return use(found[1], host, what) end - rt.ui.select(found, { - prompt = "Login for " .. host, - format = function(choice) - return several and (choice.name .. ": " .. choice.entry.label) or choice.entry.label - end, - }, function(choice) - if choice then use(choice) end - end) + M.end_status() + pick(found, host, what) end) end @@ -104,6 +285,8 @@ function M.setup(user_opts) "Fill only the username saved for this site") rt.command("password-fill-password", function() fill("password") end, "Fill only the password saved for this site") + rt.command("password-search", function() fill("login", true) end, + "Search this site's logins and pick one to fill") rt.command("password-lock", function() local locked = {} for _, name in ipairs(order) do @@ -123,12 +306,20 @@ function M.setup(user_opts) bound = {} local keys = opts.keys if keys == nil then - keys = { login = "pp", username = "pu", password = "pw" } + keys = { login = "pp", username = "pu", password = "pw", search = "ps" } end if keys then - local desc = { login = "Fill this site's login", username = "Fill this site's username", password = "Fill this site's password" } + local desc = { + login = "Fill this site's login", + username = "Fill this site's username", + password = "Fill this site's password", + search = "Search this site's logins", + } for what, lhs in pairs(keys) do - rt.keymap.set("normal", lhs, function() fill(what) end, { desc = desc[what] }) + local function run() + if what == "search" then fill("login", true) else fill(what) end + end + rt.keymap.set("normal", lhs, run, { desc = desc[what] }) table.insert(bound, lhs) end end diff --git a/passwords/lua/passwords/site.lua b/passwords/lua/passwords/site.lua index e9aee4a..d0b46fc 100644 --- a/passwords/lua/passwords/site.lua +++ b/passwords/lua/passwords/site.lua @@ -67,8 +67,9 @@ function M.search_term(host) end -- Run `step(item, done)` for each item at once; `finish(results)` gets the --- non-nil results, in the items' order, once all are done. -function M.each(items, step, finish) +-- non-nil results, in the items' order, once all are done. `tick(n, total)`, +-- if given, hears each one finish. +function M.each(items, step, finish, tick) local results, left = {}, #items if left == 0 then return finish({}) @@ -77,6 +78,7 @@ function M.each(items, step, finish) step(item, function(result) results[i] = result or false left = left - 1 + if tick then tick(#items - left, #items) end if left == 0 then local out = {} for j = 1, #items do diff --git a/passwords/pages/picker.html b/passwords/pages/picker.html new file mode 100644 index 0000000..ef4c47e --- /dev/null +++ b/passwords/pages/picker.html @@ -0,0 +1,14 @@ + + + + + passwords + + + +
>
+
    +
    Enter fill · Alt-u username · Alt-p password · Ctrl-n/p move · Esc close
    + + + diff --git a/passwords/pages/picker.js b/passwords/pages/picker.js new file mode 100644 index 0000000..49c3872 --- /dev/null +++ b/passwords/pages/picker.js @@ -0,0 +1,186 @@ +// The login picker: a prompt, and this site's logins that match it, best +// first. It only ever sees labels; the passwords stay with the plugin. +"use strict"; + +const send = (name, data) => rt.send(name, JSON.stringify(data ?? {})); +const promptEl = document.getElementById("prompt"); +const countEl = document.getElementById("count"); +const listEl = document.getElementById("list"); + +let pool = []; +let query = ""; +let cursor = 0; +let shown = []; +let selected = 0; +let what = "login"; + +// Where `needle` matches `hay` in order, and how well: consecutive letters +// and starts of words count more. Null when it doesn't match. +function fuzzy(needle, hay) { + const at = hay.indexOf(needle); + if (at >= 0) { + const start = at === 0 || /\W/.test(hay[at - 1]); + return { score: 100 + needle.length * 10 + (start ? 50 : 0), hits: [...needle].map((_, i) => at + i) }; + } + const hits = []; + let score = 0; + let from = 0; + for (const ch of needle) { + const i = hay.indexOf(ch, from); + if (i < 0) return null; + score += hits.length && i === hits[hits.length - 1] + 1 ? 8 : 1; + if (i === 0 || /\W/.test(hay[i - 1])) score += 5; + hits.push(i); + from = i + 1; + } + return { score, hits }; +} + +// Each word of the query must match the manager or the label. +function match(item, words) { + let score = 0; + const hits = { label: new Set(), manager: new Set() }; + for (const word of words) { + const inLabel = fuzzy(word, item.labelLower); + const inManager = fuzzy(word, item.managerLower); + const best = inLabel && (!inManager || inLabel.score >= inManager.score) ? ["label", inLabel] : ["manager", inManager]; + if (!best[1]) return null; + score += best[1].score; + best[1].hits.forEach((i) => hits[best[0]].add(i)); + } + return { item, score, hits }; +} + +function filter() { + const words = query.toLowerCase().split(/\s+/).filter(Boolean); + const matched = []; + for (const item of pool) { + const m = match(item, words); + if (m) matched.push(m); + } + matched.sort((a, b) => b.score - a.score || a.item.order - b.item.order); + shown = matched; + selected = Math.min(selected, Math.max(0, shown.length - 1)); + countEl.textContent = `${matched.length}/${pool.length}`; + render(); +} + +// `text` with the characters at `hits` in bold. +function marked(text, hits, className) { + const span = document.createElement("span"); + span.className = className; + let run = ""; + let bold = false; + const flush = () => { + if (!run) return; + span.append(bold ? Object.assign(document.createElement("b"), { textContent: run }) : run); + run = ""; + }; + for (let i = 0; i < text.length; i++) { + if (hits.has(i) !== bold) { + flush(); + bold = !bold; + } + run += text[i]; + } + flush(); + return span; +} + +function render() { + promptEl.replaceChildren( + query.slice(0, cursor), + Object.assign(document.createElement("span"), { className: "cursor", textContent: query[cursor] ?? " " }), + query.slice(cursor + 1), + ); + if (!pool.length) { + listEl.replaceChildren(Object.assign(document.createElement("li"), { className: "empty", textContent: "Loading…" })); + return; + } + listEl.replaceChildren( + ...shown.map((m, i) => { + const row = document.createElement("li"); + row.className = i === selected ? "selected" : ""; + row.append(marked(m.item.manager, m.hits.manager, "manager"), marked(m.item.label, m.hits.label, "label")); + row.addEventListener("click", () => choose(i, what)); + return row; + }), + ); + listEl.children[selected]?.scrollIntoView({ block: "nearest" }); +} + +function choose(index, how) { + const m = shown[index]; + if (m) send("choose", { id: m.item.id, what: how }); +} + +function move(by) { + if (!shown.length) return; + selected = (selected + by + shown.length) % shown.length; + render(); +} + +function edit(text, at) { + query = text; + cursor = Math.max(0, Math.min(at, query.length)); + selected = 0; + filter(); +} + +// A key in nvim-style notation: "a", "C-n", "A-u", "Enter". +function press(k) { + switch (k) { + case "Enter": return choose(selected, what); + case "A-u": return choose(selected, "username"); + case "A-p": return choose(selected, "password"); + case "Escape": case "C-c": return send("close"); + case "ArrowDown": case "Tab": case "C-n": case "C-j": return move(1); + case "ArrowUp": case "C-p": case "C-k": return move(-1); + case "ArrowLeft": cursor = Math.max(0, cursor - 1); return render(); + case "ArrowRight": cursor = Math.min(query.length, cursor + 1); return render(); + case "Home": case "C-a": cursor = 0; return render(); + case "End": case "C-e": cursor = query.length; return render(); + case "Backspace": case "C-h": + if (cursor > 0) edit(query.slice(0, cursor - 1) + query.slice(cursor), cursor - 1); + return; + case "Delete": + return edit(query.slice(0, cursor) + query.slice(cursor + 1), cursor); + case "C-w": { + const start = query.slice(0, cursor).replace(/\S*\s*$/, "").length; + return edit(query.slice(0, start) + query.slice(cursor), start); + } + case "C-u": return edit(query.slice(cursor), 0); + default: + if ([...k].length === 1) edit(query.slice(0, cursor) + k + query.slice(cursor), cursor + 1); + } +} + +addEventListener("keydown", (e) => { + e.preventDefault(); + if (e.metaKey || ["Shift", "Control", "Alt", "Meta"].includes(e.key)) return; + if (e.key === "Tab" && e.shiftKey) return move(-1); + const key = e.key.length === 1 ? e.key.toLowerCase() : e.key; + if (e.altKey) return press(`A-${key}`); + press(e.ctrlKey ? `C-${key}` : e.key); +}); + +addEventListener("rtmessage", (e) => { + const { name, data } = e.detail; + if (name === "items") { + what = data.what || "login"; + pool = data.items.map((item, order) => ({ + ...item, + order, + labelLower: item.label.toLowerCase(), + managerLower: item.manager.toLowerCase(), + })); + filter(); + } else if (name === "type") { + for (const ch of data.text) press(ch); + } else if (name === "key") { + press(data.key); + } +}); + +render(); +send("ready"); diff --git a/passwords/pages/style.css b/passwords/pages/style.css new file mode 100644 index 0000000..b9bb52b --- /dev/null +++ b/passwords/pages/style.css @@ -0,0 +1,109 @@ +:root { + --bg: #1e1e2e; + --fg: #cdd6f4; + --muted: #7f849c; + --line: #313244; + --selected: #45475a; + --match: #f9e2af; + --accent: #89b4fa; +} + +@media (prefers-color-scheme: light) { + :root { + --bg: #eff1f5; + --fg: #4c4f69; + --muted: #8c8fa1; + --line: #ccd0da; + --selected: #dce0e8; + --match: #df8e1d; + --accent: #1e66f5; + } +} + +html, body { + margin: 0; + height: 100%; + overflow: hidden; + background: var(--bg); + color: var(--fg); + font: 13px/1.5 ui-monospace, monospace; +} + +body { + display: flex; + flex-direction: column; +} + +#bar { + display: flex; + gap: 8px; + padding: 8px 12px; + border-bottom: 1px solid var(--line); +} + +.sign { + color: var(--accent); +} + +#prompt { + flex: 1; + white-space: pre; +} + +.cursor { + background: var(--fg); + color: var(--bg); +} + +#count { + color: var(--muted); +} + +#list { + flex: 1; + margin: 0; + padding: 4px 0; + overflow-y: auto; + list-style: none; +} + +#list li { + display: flex; + gap: 10px; + padding: 1px 12px; + white-space: nowrap; + cursor: pointer; +} + +#list li.selected { + background: var(--selected); +} + +.manager { + flex: 0 0 10ch; + color: var(--accent); + overflow: hidden; + text-overflow: ellipsis; +} + +.label { + flex: 1 1 0; + overflow: hidden; + text-overflow: ellipsis; +} + +#list li.empty { + color: var(--muted); + cursor: default; +} + +b { + color: var(--match); +} + +#help { + padding: 4px 12px; + border-top: 1px solid var(--line); + color: var(--muted); + font-size: 11px; +} diff --git a/passwords/riptide-plugin.toml b/passwords/riptide-plugin.toml index 9c876da..98e6993 100644 --- a/passwords/riptide-plugin.toml +++ b/passwords/riptide-plugin.toml @@ -1,6 +1,7 @@ name = "passwords" description = "Fill logins from your password managers; add pass, rbw, bitwarden or keepassxc for one" -riptide = ">=0.4" +# Centred float pages that take every key, for the login picker. +riptide = ">=0.8" [permissions] # Fills login forms on the site you're on, and only that site. It runs no diff --git a/passwords/test/passwords_spec.lua b/passwords/test/passwords_spec.lua index 1cc1000..ddb7e2b 100644 --- a/passwords/test/passwords_spec.lua +++ b/passwords/test/passwords_spec.lua @@ -26,6 +26,26 @@ local function open_login() run("open " .. page("login.html")) wait_for("load_finished", { pattern = "plugin-test.localhost" }) end +-- The open picker, once its logins have arrived. +local function open_picker() + wait_until(function() + local p = passwords().picker() + return p and p.ready + end) + wait(200) + return passwords().picker() +end + +-- The status float's text, one line each. +local function status() + local text = {} + for _, line in ipairs(passwords().status or {}) do + for _, chunk in ipairs(line) do table.insert(text, chunk[1]) end + table.insert(text, "\n") + end + return table.concat(text) +end + local function login(label, saved, username, password) return { label = label, saved = saved, login = { username = username, password = password } } end @@ -33,8 +53,9 @@ end -- A password manager that knows `entries`, answering after `delay` ms. local function fake(entries, delay) return { - find = function(_, cb) + find = function(_, cb, progress) if delay then + progress("reading entries", 1, 2) rt.defer(delay, function() cb(entries) end) else cb(entries) @@ -69,10 +90,46 @@ describe("passwords", function() passwords().register("one", fake({ login("a", { "plugin-test.localhost" }, "a", "pw-a") })) passwords().register("two", fake({ login("b", { "plugin-test.localhost" }, "b", "pw-b") })) run("password-fill-password") - wait(300) - keys("2") + local picker = open_picker() + picker.type("two") + wait(200) + picker.key("Enter") wait_field("pass", "pw-b") assert.equals("", field("user"), "only the password") + assert.equals(nil, passwords().picker(), "the picker closed") + end) + + it("searches the site's logins with :password-search, even for one", function() + passwords().register("one", fake({ login("here", { "plugin-test.localhost" }, "ann", "pw-ann") })) + run("password-search") + local picker = open_picker() + picker.key("A-u") + wait_field("user", "ann") + assert.equals("", field("pass"), "only the username") + wait_until(function() return status():find("✓ Filled the username of here", 1, true) end) + end) + + it("fills nothing when the picker is closed", function() + passwords().register("one", fake({ login("a", { "plugin-test.localhost" }, "a", "pw-a") })) + passwords().register("two", fake({ login("b", { "plugin-test.localhost" }, "b", "pw-b") })) + run("password-fill") + open_picker().key("Escape") + wait_until(function() return passwords().picker() == nil end) + wait(300) + assert.equals("", field("pass")) + end) + + it("shows each password manager's progress while it looks", function() + passwords().register("one", fake({ login("here", { "plugin-test.localhost" }, "ann", "pw-ann") }, 1500)) + passwords().register("two", fake({})) + run("password-fill") + wait_until(function() return status():find("reading entries 1 of 2", 1, true) end) + local during = status() + assert.matches("Logins for plugin%-test%.localhost\n", during) + assert.truthy(during:find(string.rep("█", 8) .. string.rep("░", 8), 1, true), during) + assert.truthy(during:find("✓ two 0 logins", 1, true), during) + wait_field("pass", "pw-ann") + wait_until(function() return status():find("✓ Filled here", 1, true) end) end) it("says when no login is saved for the site", function() diff --git a/rbw/lua/rbw/init.lua b/rbw/lua/rbw/init.lua index bdda33e..0a5a86d 100644 --- a/rbw/lua/rbw/init.lua +++ b/rbw/lua/rbw/init.lua @@ -15,7 +15,9 @@ function M.new(opts) local cmd = command(opts) local backend = {} - function backend.find(host, cb) + function backend.find(host, cb, progress) + progress = progress or function() end + progress("searching") site.run(rt.spawn, { cmd, "search", "--fields", "id,name,user", site.search_term(host) }, nil, function(out) local found = {} for _, line in ipairs(site.lines(out)) do @@ -24,6 +26,7 @@ function M.new(opts) table.insert(found, { id = id, name = name, user = user }) end end + progress("reading entries", 0, #found) site.each(found, function(item, done) site.run(rt.spawn, { cmd, "get", "--raw", item.id }, nil, function(raw) local ok, cipher = pcall(rt.json.decode, raw) @@ -46,6 +49,8 @@ function M.new(opts) end, function() done(nil) end) end, function(entries) cb(entries) + end, function(n, total) + progress("reading entries", n, total) end) end, function(err) cb(nil, err) end) end