aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/aerospace.js
blob: 5b403b1de9287d651049f87eee4c78052a9583d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as Uebersicht from "uebersicht";
import * as Settings from "./settings";
import * as Utils from "./utils";

/**
 * Switches to the specified workspace.
 * @param {number} index - The index of the workspace to switch to.
 */
export async function goToSpace(index) {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  await Uebersicht.run(`${aerospacePath} workspace ${index}`);
}

/**
 * Focuses the window with the specified ID.
 * @param {number} id - The ID of the window to focus.
 */
export async function focusWindow(id) {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  await Uebersicht.run(`${aerospacePath} focus --window-id ${id}`);
}

/**
 * Retrieves the list of workspaces for the specified display.
 * @param {number} displayId - The ID of the display.
 * @returns {Promise<Object[]>} The list of workspaces.
 */
export async function getSpaces(displayId) {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  const json = await Uebersicht.run(
    `${aerospacePath} list-workspaces --monitor ${displayId} --json --format "%{workspace} %{monitor-appkit-nsscreen-screens-id}"`
  );
  return Utils.parseJson(json);
}

/**
 * Retrieves the currently focused workspace.
 * @returns {Promise<Object>} The focused workspace.
 */
export async function getFocusedSpace() {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  const json = await Uebersicht.run(
    `${aerospacePath} list-workspaces --focused --json`
  );
  return Utils.parseJson(json);
}

/**
 * Retrieves the list of windows for the specified workspace.
 * @param {number} workspaceId - The ID of the workspace.
 * @returns {Promise<Object[]>} The list of windows.
 */
export async function getWindows(workspaceId) {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  const json = await Uebersicht.run(
    `${aerospacePath} list-windows --workspace ${workspaceId} --json`
  );
  const cleanedJson = json.replace(/\\\n/g, "").replace(/00000/g, "0");
  return Utils.parseJson(cleanedJson);
}

/**
 * Retrieves the currently focused window.
 * @returns {Promise<Object>} The focused window.
 */
export async function getFocusedWindow() {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  const json = await Uebersicht.run(
    `${aerospacePath} list-windows --focused --json`
  );
  return Utils.parseJson(json);
}

/**
 * Retrieves the list of displays.
 * @returns {Promise<Object[]>} The list of displays.
 */
export async function getDisplays() {
  const settings = Settings.get();
  const { aerospacePath = "/opt/homebrew/bin/aerospace" } = settings.global;
  const json = await Uebersicht.run(`${aerospacePath} list-monitors --json`);
  return Utils.parseJson(json);
}

/**
 * Retrieves the custom display index for the specified display.
 * @param {Object} display - The display object.
 * @returns {number} The custom display index.
 */
export function getDisplayIndex(item) {
  const settings = Settings.get();
  const { customAeroSpaceDisplayIndexes } = settings.spacesDisplay;
  const nativeId = item["monitor-appkit-nsscreen-screens-id"];
  const customId = customAeroSpaceDisplayIndexes[nativeId];
  return customId || nativeId;
}