users/ryan/modules/simple-bar/simple-bar-source/lib/aerospace.js
8c57a511a35702ddf354f3cb52e04228a36bee82
· 3.4 KB · 102 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import * as Settings from "./settings"; |
| 3 | import * as Utils from "./utils"; |
| 4 | |
| 5 | /** |
| 6 | * Switches to the specified workspace. |
| 7 | * @param {number} index - The index of the workspace to switch to. |
| 8 | */ |
| 9 | export async function goToSpace(index) { |
| 10 | const settings = Settings.get(); |
| 11 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 12 | await Uebersicht.run(`${aerospacePath} workspace ${index}`); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Focuses the window with the specified ID. |
| 17 | * @param {number} id - The ID of the window to focus. |
| 18 | */ |
| 19 | export async function focusWindow(id) { |
| 20 | const settings = Settings.get(); |
| 21 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 22 | await Uebersicht.run(`${aerospacePath} focus --window-id ${id}`); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Retrieves the list of workspaces for the specified display. |
| 27 | * @param {number} displayId - The ID of the display. |
| 28 | * @returns {Promise<Object[]>} The list of workspaces. |
| 29 | */ |
| 30 | export async function getSpaces(displayId) { |
| 31 | const settings = Settings.get(); |
| 32 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 33 | const json = await Uebersicht.run( |
| 34 | `${aerospacePath} list-workspaces --monitor ${displayId} --json --format "%{workspace} %{monitor-appkit-nsscreen-screens-id}"` |
| 35 | ); |
| 36 | return Utils.parseJson(json); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Retrieves the currently focused workspace. |
| 41 | * @returns {Promise<Object>} The focused workspace. |
| 42 | */ |
| 43 | export async function getFocusedSpace() { |
| 44 | const settings = Settings.get(); |
| 45 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 46 | const json = await Uebersicht.run( |
| 47 | `${aerospacePath} list-workspaces --focused --json` |
| 48 | ); |
| 49 | return Utils.parseJson(json); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Retrieves the list of windows for the specified workspace. |
| 54 | * @param {number} workspaceId - The ID of the workspace. |
| 55 | * @returns {Promise<Object[]>} The list of windows. |
| 56 | */ |
| 57 | export async function getWindows(workspaceId) { |
| 58 | const settings = Settings.get(); |
| 59 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 60 | const json = await Uebersicht.run( |
| 61 | `${aerospacePath} list-windows --workspace ${workspaceId} --json` |
| 62 | ); |
| 63 | const cleanedJson = json.replace(/\\\n/g, "").replace(/00000/g, "0"); |
| 64 | return Utils.parseJson(cleanedJson); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Retrieves the currently focused window. |
| 69 | * @returns {Promise<Object>} The focused window. |
| 70 | */ |
| 71 | export async function getFocusedWindow() { |
| 72 | const settings = Settings.get(); |
| 73 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 74 | const json = await Uebersicht.run( |
| 75 | `${aerospacePath} list-windows --focused --json` |
| 76 | ); |
| 77 | return Utils.parseJson(json); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Retrieves the list of displays. |
| 82 | * @returns {Promise<Object[]>} The list of displays. |
| 83 | */ |
| 84 | export async function getDisplays() { |
| 85 | const settings = Settings.get(); |
| 86 | const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global; |
| 87 | const json = await Uebersicht.run(`${aerospacePath} list-monitors --json`); |
| 88 | return Utils.parseJson(json); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Retrieves the custom display index for the specified display. |
| 93 | * @param {Object} display - The display object. |
| 94 | * @returns {number} The custom display index. |
| 95 | */ |
| 96 | export function getDisplayIndex(item) { |
| 97 | const settings = Settings.get(); |
| 98 | const { customAeroSpaceDisplayIndexes } = settings.spacesDisplay; |
| 99 | const nativeId = item["monitor-appkit-nsscreen-screens-id"]; |
| 100 | const customId = customAeroSpaceDisplayIndexes[nativeId]; |
| 101 | return customId || nativeId; |
| 102 | } |