diff options
Diffstat (limited to 'users/ryan/modules/simple-bar/simple-bar-source/lib/yabai.js')
| -rwxr-xr-x | users/ryan/modules/simple-bar/simple-bar-source/lib/yabai.js | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/users/ryan/modules/simple-bar/simple-bar-source/lib/yabai.js b/users/ryan/modules/simple-bar/simple-bar-source/lib/yabai.js new file mode 100755 index 0000000..1a56068 --- /dev/null +++ b/users/ryan/modules/simple-bar/simple-bar-source/lib/yabai.js | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | import * as Uebersicht from "uebersicht"; | ||
| 2 | import * as Settings from "./settings"; | ||
| 3 | import * as Utils from "./utils"; | ||
| 4 | |||
| 5 | /** | ||
| 6 | * Focuses on the specified space. | ||
| 7 | * @param {number} index - The index of the space to focus on. | ||
| 8 | */ | ||
| 9 | export async function goToSpace(index) { | ||
| 10 | const settings = Settings.get(); | ||
| 11 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 12 | await Uebersicht.run(`${yabaiPath} -m space --focus ${index}`); | ||
| 13 | } | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Renames the specified space. | ||
| 17 | * @param {number} index - The index of the space to rename. | ||
| 18 | * @param {string} label - The new label for the space. | ||
| 19 | */ | ||
| 20 | export async function renameSpace(index, label) { | ||
| 21 | const settings = Settings.get(); | ||
| 22 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 23 | await Uebersicht.run(`${yabaiPath} -m space ${index} --label "${label}"`); | ||
| 24 | } | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Creates a new space on the specified display. | ||
| 28 | * @param {number} displayId - The ID of the display to create the space on. | ||
| 29 | */ | ||
| 30 | export async function createSpace(displayId) { | ||
| 31 | const settings = Settings.get(); | ||
| 32 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 33 | await focusDisplay(displayId); | ||
| 34 | await Uebersicht.run(`${yabaiPath} -m space --create`); | ||
| 35 | await Utils.softRefresh(); | ||
| 36 | } | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Removes the specified space from the specified display. | ||
| 40 | * @param {number} index - The index of the space to remove. | ||
| 41 | * @param {number} displayId - The ID of the display to remove the space from. | ||
| 42 | */ | ||
| 43 | export async function removeSpace(index, displayId) { | ||
| 44 | const settings = Settings.get(); | ||
| 45 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 46 | await focusDisplay(displayId); | ||
| 47 | await Uebersicht.run(`${yabaiPath} -m space ${index} --destroy`); | ||
| 48 | await Utils.softRefresh(); | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Swaps the specified space with the space in the given direction. | ||
| 53 | * @param {number} index - The index of the space to swap. | ||
| 54 | * @param {string} direction - The direction to swap the space ("left" or "right"). | ||
| 55 | */ | ||
| 56 | export async function swapSpace(index, direction) { | ||
| 57 | const settings = Settings.get(); | ||
| 58 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 59 | const action = direction === "left" ? index - 1 : index + 1; | ||
| 60 | await Uebersicht.run(`${yabaiPath} -m space ${index} --swap ${action}`); | ||
| 61 | await Utils.softRefresh(); | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Focuses on the specified window. | ||
| 66 | * @param {number} id - The ID of the window to focus on. | ||
| 67 | */ | ||
| 68 | export async function focusWindow(id) { | ||
| 69 | const settings = Settings.get(); | ||
| 70 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 71 | await Uebersicht.run(`${yabaiPath} -m window --focus ${id}`); | ||
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Retrieves the list of spaces. | ||
| 76 | * @returns {Promise<Object[]>} A promise that resolves to the list of spaces. | ||
| 77 | */ | ||
| 78 | export async function getSpaces() { | ||
| 79 | const settings = Settings.get(); | ||
| 80 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 81 | const json = await Uebersicht.run(`${yabaiPath} -m query --spaces`); | ||
| 82 | return Utils.parseJson(json); | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * Retrieves the list of windows. | ||
| 87 | * @returns {Promise<Object[]>} A promise that resolves to the list of windows. | ||
| 88 | */ | ||
| 89 | export async function getWindows() { | ||
| 90 | const settings = Settings.get(); | ||
| 91 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 92 | const json = await Uebersicht.run(`${yabaiPath} -m query --windows`); | ||
| 93 | const cleanedJson = json.replace(/\\\n/g, "").replace(/00000/g, "0"); | ||
| 94 | return Utils.parseJson(cleanedJson); | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Retrieves the list of displays. | ||
| 99 | * @returns {Promise<Object[]>} A promise that resolves to the list of displays. | ||
| 100 | */ | ||
| 101 | export async function getDisplays() { | ||
| 102 | const settings = Settings.get(); | ||
| 103 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 104 | const json = await Uebersicht.run(`${yabaiPath} -m query --displays`); | ||
| 105 | return Utils.parseJson(json); | ||
| 106 | } | ||
| 107 | |||
| 108 | /** | ||
| 109 | * Focus the given display. | ||
| 110 | * | ||
| 111 | * Silently ignores the error thrown when the display is already focused, | ||
| 112 | * which is a known and harmless yabai behavior. | ||
| 113 | * | ||
| 114 | * @param {number|string} displayId - The ID of the display to focus. | ||
| 115 | */ | ||
| 116 | async function focusDisplay(displayId) { | ||
| 117 | const settings = Settings.get(); | ||
| 118 | const { yabaiPath = "/opt/homebrew/bin/yabai" } = settings.global; | ||
| 119 | try { | ||
| 120 | await Uebersicht.run(`${yabaiPath} -m display --focus ${displayId}`); | ||
| 121 | } catch (e) { | ||
| 122 | const message = e?.toString?.() || ""; | ||
| 123 | if (!message.includes("cannot focus an already focused display")) { | ||
| 124 | // eslint-disable-next-line no-console | ||
| 125 | console.warn("Failed to focus display:", message); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
