add db control commands

This commit is contained in:
atagen 2026-03-23 11:45:54 +11:00
parent 53be599d91
commit 17967da43e

View file

@ -17,6 +17,10 @@ Usage:
inshellah complete CMD [ARGS...] [--db PATH]
Nushell custom completer. Outputs JSON completion candidates.
Falls back to --help resolution if command is not in the database.
inshellah query CMD [--db PATH]
Print stored completion data for CMD as JSON.
inshellah clear [CMD...] [--db PATH]
Clear the database, or remove specific commands.
inshellah dump [--db PATH]
Show stats and commands in the database.
inshellah manpage FILE Parse a manpage and emit nushell extern
@ -633,6 +637,34 @@ let cmd_complete spans db_path =
Printf.printf "[%s]\n" (String.concat "," !candidates));
close db
let cmd_query cmd db_path =
let db = init db_path in
(match lookup db cmd with
| None ->
Printf.eprintf "not found: %s\n" cmd; close db; exit 1
| Some (data, source) ->
Printf.printf "# source: %s\n%s\n" source data);
close db
let cmd_clear cmds db_path =
let db = init db_path in
(match cmds with
| [] ->
(match Sqlite3.exec db "DELETE FROM completions" with
| Sqlite3.Rc.OK ->
Printf.printf "cleared all commands from %s\n" db_path
| rc ->
Printf.eprintf "error: %s\n" (Sqlite3.Rc.to_string rc); exit 1)
| _ ->
List.iter (fun cmd ->
if has_command db cmd then begin
delete db cmd;
Printf.printf "removed %s\n" cmd
end else
Printf.eprintf "not found: %s\n" cmd
) cmds);
close db
let load_ignorelist path =
try
let ic = open_in path in
@ -669,6 +701,19 @@ let () =
| arg :: rest -> parse_complete_args (arg :: spans) db rest in
let (spans, db_path) = parse_complete_args [] (default_db_path ()) rest in
cmd_complete spans db_path
| "query" :: rest ->
let (cmd, db_path) = match rest with
| [cmd] -> (cmd, default_db_path ())
| [cmd; "--db"; path] -> (cmd, path)
| _ -> Printf.eprintf "error: query CMD [--db PATH]\n"; exit 1 in
cmd_query cmd db_path
| "clear" :: rest ->
let rec parse_clear_args cmds db = function
| [] -> (List.rev cmds, db)
| "--db" :: path :: rest -> parse_clear_args cmds path rest
| cmd :: rest -> parse_clear_args (cmd :: cmds) db rest in
let (cmds, db_path) = parse_clear_args [] (default_db_path ()) rest in
cmd_clear cmds db_path
| "dump" :: rest ->
let db_path = match rest with
| ["--db"; path] -> path