Skip to content

Commit caa0c1d

Browse files
Break tab_edit into logical pieces
1 parent 09023d6 commit caa0c1d

1 file changed

Lines changed: 44 additions & 34 deletions

File tree

glide/gnarly-tab-edit.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
1+
// TODO: Reorder tabs by changing line order (including pinned tabes)
2+
// - Pinned tabs should not be closeable
3+
// TODO: Update url of a tab
4+
// TODO: Open new tab for new lines that didn't exist before
5+
// TODO: Pin/unpin tabs
16
glide.excmds.create({ name: "tab_edit", description: "Edit tabs in a text editor" }, async () => {
2-
const tabs = await browser.tabs.query({ pinned: false });
7+
const tabs = await get_list_of_current_tabs()
8+
const tempfile = await save_tabs_to_temp_file(tabs)
9+
await let_user_edit_file_and_wait_for_exit(tempfile)
10+
const tabs_to_keep = await get_list_of_tab_ids_from_file(tempfile)
11+
await close_unwanted_tabs(tabs, tabs_to_keep)
12+
});
13+
14+
async function get_list_of_current_tabs() {
15+
return await browser.tabs.query({ pinned: false })
16+
}
317

18+
async function save_tabs_to_temp_file(tabs) {
419
const tab_lines = tabs.map((tab) => {
520
const title = tab.title?.replace(/\n/g, " ") || "No Title";
621
const url = tab.url || "about:blank";
722
return `${tab.id}: ${title} (${url})`;
823
});
24+
const tempfile = await mktemp("glide_tab_edit.XXXXXX")
25+
await glide.fs.write(tempfile, tab_lines.join("\n"));
26+
return tempfile
27+
}
928

10-
const mktempcmd = await glide.process.execute("mktemp", ["-t", "glide_tab_edit.XXXXXX"]);
11-
12-
let stdout = "";
13-
for await (const chunk of mktempcmd.stdout) {
14-
stdout += chunk;
15-
}
16-
const temp_filepath = stdout.trim();
17-
18-
tab_lines.unshift("// Delete the corresponding lines to close the tabs");
19-
tab_lines.unshift("// vim: ft=qute-tab-edit");
20-
tab_lines.unshift("");
21-
await glide.fs.write(temp_filepath, tab_lines.join("\n"));
22-
23-
console.log("Temp file created at:", temp_filepath);
24-
25-
const editcmd = await glide.process.execute("gnome-text-editor", [
29+
async function let_user_edit_file_and_wait_for_exit(tempfile) {
30+
const edit_cmd = await glide.process.execute("gnome-text-editor", [
2631
"--standalone",
27-
temp_filepath,
32+
tempfile,
2833
]);
29-
30-
const cp = await editcmd.wait();
31-
if (cp.exit_code !== 0) {
32-
throw new Error(`Editor command failed with exit code ${cp.exit_code}`);
34+
const edit_result = await edit_cmd.wait();
35+
if (edit_result.exit_code !== 0) {
36+
throw new Error(`Editor command failed with exit code ${edit_result.exit_code}`);
3337
}
34-
console.log("Edit complete");
38+
}
3539

36-
// read the edited file
37-
const edited_content = await glide.fs.read(temp_filepath, "utf8");
38-
const edited_lines = edited_content
40+
async function get_list_of_tab_ids_from_file(tempfile) {
41+
const edited_content = await glide.fs.read(tempfile, "utf8")
42+
const tabs_to_keep = edited_content
3943
.split("\n")
4044
.filter((line) => line.trim().length > 0)
41-
.filter((line) => !line.startsWith("//"));
45+
.filter((line) => !line.startsWith("//"))
46+
.map((line) => {
47+
const tab_id = line.split(":")[0]
48+
return Number(tab_id)
49+
})
50+
return tabs_to_keep
51+
}
4252

43-
const tabs_to_keep = edited_lines.map((line) => {
44-
const tab_id = line.split(":")[0];
45-
return Number(tab_id);
46-
});
47-
48-
const tab_ids_to_close = tabs
53+
async function close_unwanted_tabs(current_tabs, tabs_to_keep) {
54+
const tab_ids_to_close = current_tabs
4955
.filter((tab) => tab.id && !tabs_to_keep.includes(tab.id))
5056
.map((tab) => tab.id)
5157
.filter((id): id is number => id !== undefined);
5258
await browser.tabs.remove(tab_ids_to_close);
53-
});
59+
}
5460

61+
async function mktemp(template) {
62+
const mktemp_cmd = await glide.process.execute("mktemp", ["-t", template]);
63+
return (await mktemp_cmd.stdout.text()).trim();
64+
}

0 commit comments

Comments
 (0)