|
| 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 |
1 | 6 | 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 | +} |
3 | 17 |
|
| 18 | +async function save_tabs_to_temp_file(tabs) { |
4 | 19 | const tab_lines = tabs.map((tab) => { |
5 | 20 | const title = tab.title?.replace(/\n/g, " ") || "No Title"; |
6 | 21 | const url = tab.url || "about:blank"; |
7 | 22 | return `${tab.id}: ${title} (${url})`; |
8 | 23 | }); |
| 24 | + const tempfile = await mktemp("glide_tab_edit.XXXXXX") |
| 25 | + await glide.fs.write(tempfile, tab_lines.join("\n")); |
| 26 | + return tempfile |
| 27 | +} |
9 | 28 |
|
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", [ |
26 | 31 | "--standalone", |
27 | | - temp_filepath, |
| 32 | + tempfile, |
28 | 33 | ]); |
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}`); |
33 | 37 | } |
34 | | - console.log("Edit complete"); |
| 38 | +} |
35 | 39 |
|
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 |
39 | 43 | .split("\n") |
40 | 44 | .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 | +} |
42 | 52 |
|
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 |
49 | 55 | .filter((tab) => tab.id && !tabs_to_keep.includes(tab.id)) |
50 | 56 | .map((tab) => tab.id) |
51 | 57 | .filter((id): id is number => id !== undefined); |
52 | 58 | await browser.tabs.remove(tab_ids_to_close); |
53 | | -}); |
| 59 | +} |
54 | 60 |
|
| 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