betterdisplaycli 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/Device.ts +132 -37
  3. package/src/get.ts +15 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "betterdisplaycli",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "TypeScript bindings for `betterdisplaycli`.",
5
5
  "author": "Lucas Garron <code@garron.net>",
6
6
  "license": "MIT",
package/src/Device.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  import { PrintableShellCommand } from "printable-shell-command";
2
- import { print, type QuietOption } from "./get";
2
+ import {
3
+ type DetachOption,
4
+ print,
5
+ type QuietOption,
6
+ shellOutSilentOrDetach,
7
+ } from "./get";
3
8
  import { isNotUndefined, ResolutionInfo } from "./ResolutionInfo";
4
9
 
5
10
  type BOOLEAN_SETTING = "connected" | "hiDPI" | "notch";
6
11
  type STRING_SETTING = "resolution";
12
+ type FLOAT_SETTING = "brightness";
13
+ type INTEGER_SETTING = "bit-depth" | "rotation";
7
14
 
8
15
  export type NumberString = string;
9
16
 
@@ -76,34 +83,40 @@ class SingleDisplay extends Device {
76
83
  set: async (
77
84
  settingName: BOOLEAN_SETTING,
78
85
  on: boolean,
79
- options?: QuietOption,
86
+ options?: QuietOption & DetachOption,
80
87
  ): Promise<void> => {
81
- await print(
82
- new PrintableShellCommand("betterdisplaycli", [
83
- "set",
84
- `--name=${this.info.name}`,
85
- `--${settingName}=${on ? "on" : "off"}`,
86
- ]),
87
- { argumentLineWrapping: "inline" },
88
- options,
89
- ).shellOut({ print: false });
88
+ await shellOutSilentOrDetach(
89
+ print(
90
+ new PrintableShellCommand("betterdisplaycli", [
91
+ "set",
92
+ `--name=${this.info.name}`,
93
+ `--${settingName}=${on ? "on" : "off"}`,
94
+ ]),
95
+ { argumentLineWrapping: "inline" },
96
+ options,
97
+ ),
98
+ );
90
99
  },
91
100
 
92
101
  toggle: async (
93
102
  settingName: BOOLEAN_SETTING,
94
- options?: QuietOption,
103
+ options?: QuietOption & DetachOption,
95
104
  ): Promise<void> => {
96
- await print(
97
- new PrintableShellCommand("betterdisplaycli", [
98
- "toggle",
99
- `--name=${this.info.name}`,
100
- `--${settingName}`,
101
- ]),
102
- { argumentLineWrapping: "inline" },
105
+ await shellOutSilentOrDetach(
106
+ print(
107
+ new PrintableShellCommand("betterdisplaycli", [
108
+ "toggle",
109
+ `--name=${this.info.name}`,
110
+ `--${settingName}`,
111
+ ]),
112
+ { argumentLineWrapping: "inline" },
113
+ options,
114
+ ),
103
115
  options,
104
- ).shellOut({ print: false });
116
+ );
105
117
  },
106
118
  };
119
+
107
120
  string = {
108
121
  get: async (
109
122
  settingName: STRING_SETTING,
@@ -123,17 +136,91 @@ class SingleDisplay extends Device {
123
136
  set: async (
124
137
  settingName: STRING_SETTING,
125
138
  value: string,
139
+ options?: QuietOption & DetachOption,
140
+ ): Promise<void> => {
141
+ await shellOutSilentOrDetach(
142
+ print(
143
+ new PrintableShellCommand("betterdisplaycli", [
144
+ "set",
145
+ `--name=${this.info.name}`,
146
+ `--${settingName}=${value}`,
147
+ ]),
148
+ { argumentLineWrapping: "inline" },
149
+ options,
150
+ ),
151
+ );
152
+ },
153
+ };
154
+
155
+ float = {
156
+ get: async (
157
+ settingName: FLOAT_SETTING,
126
158
  options?: QuietOption,
159
+ ): Promise<number> => {
160
+ const value = await print(
161
+ new PrintableShellCommand("betterdisplaycli", [
162
+ "get",
163
+ `--name=${this.info.name}`,
164
+ `--${settingName}`,
165
+ ]),
166
+ { argumentLineWrapping: "inline" },
167
+ options,
168
+ ).text({ trimTrailingNewlines: "single-required" });
169
+ return parseFloat(value);
170
+ },
171
+
172
+ set: async (
173
+ settingName: FLOAT_SETTING,
174
+ value: number,
175
+ options?: QuietOption & DetachOption,
127
176
  ): Promise<void> => {
128
- await print(
177
+ await shellOutSilentOrDetach(
178
+ print(
179
+ new PrintableShellCommand("betterdisplaycli", [
180
+ "set",
181
+ `--name=${this.info.name}`,
182
+ `--${settingName}=${value}`,
183
+ ]),
184
+ { argumentLineWrapping: "inline" },
185
+ options,
186
+ ),
187
+ );
188
+ },
189
+ };
190
+
191
+ int = {
192
+ get: async (
193
+ settingName: INTEGER_SETTING,
194
+ options?: QuietOption,
195
+ ): Promise<number> => {
196
+ const value = await print(
129
197
  new PrintableShellCommand("betterdisplaycli", [
130
- "set",
198
+ "get",
131
199
  `--name=${this.info.name}`,
132
- `--${settingName}=${value}`,
200
+ `--${settingName}`,
133
201
  ]),
134
202
  { argumentLineWrapping: "inline" },
135
203
  options,
136
- ).shellOut({ print: false });
204
+ ).text({ trimTrailingNewlines: "single-required" });
205
+ return parseInt(value, 10);
206
+ },
207
+
208
+ set: async (
209
+ settingName: INTEGER_SETTING,
210
+ value: number,
211
+ options?: QuietOption & DetachOption,
212
+ ): Promise<void> => {
213
+ await shellOutSilentOrDetach(
214
+ print(
215
+ new PrintableShellCommand("betterdisplaycli", [
216
+ "set",
217
+ `--name=${this.info.name}`,
218
+ `--${settingName}=${value}`,
219
+ ]),
220
+ { argumentLineWrapping: "inline" },
221
+ options,
222
+ ),
223
+ );
137
224
  },
138
225
  };
139
226
 
@@ -141,17 +228,23 @@ class SingleDisplay extends Device {
141
228
  get: async (): Promise<ResolutionInfo> => {
142
229
  return ResolutionInfo.fromString(await this.string.get("resolution"));
143
230
  },
144
- // The return value indicates if any changes were needed (and performed)
231
+ /**
232
+ * The return value indicates if any changes were
233
+ *
234
+ * - needed and
235
+ * - performed successfully (non-detached) or initiated successfully (detached).
236
+ */
145
237
  set: async (
146
238
  resolutionInfo: ResolutionInfo,
147
- options?: QuietOption,
239
+ options?: QuietOption & DetachOption,
148
240
  ): Promise<boolean> => {
149
241
  const currentResolution = await this.resolution.get();
150
242
 
151
243
  const args: string[] = [];
244
+ // TODO: implement batch changes.
152
245
  if (
153
246
  resolutionInfo.width !== currentResolution.width ||
154
- resolutionInfo.width !== currentResolution.height
247
+ resolutionInfo.height !== currentResolution.height
155
248
  ) {
156
249
  args.push(`--resolution=${resolutionInfo.logicalResolutionString()}`);
157
250
  }
@@ -159,7 +252,7 @@ class SingleDisplay extends Device {
159
252
  isNotUndefined(resolutionInfo.hiDPI) &&
160
253
  resolutionInfo.hiDPI !== currentResolution.hiDPI
161
254
  ) {
162
- args.push(`--hiDPI=${resolutionInfo.hiDPI}`);
255
+ args.push(`--hiDPI=${resolutionInfo.hiDPI ? "on" : "off"}`);
163
256
  }
164
257
  if (
165
258
  isNotUndefined(resolutionInfo.notch) &&
@@ -173,15 +266,17 @@ class SingleDisplay extends Device {
173
266
  return false;
174
267
  }
175
268
 
176
- await print(
177
- new PrintableShellCommand("betterdisplaycli", [
178
- "set",
179
- `--name=${this.info.name}`,
180
- ...args,
181
- ]),
182
- { argumentLineWrapping: "inline" },
183
- options,
184
- ).shellOut({ print: false });
269
+ await shellOutSilentOrDetach(
270
+ print(
271
+ new PrintableShellCommand("betterdisplaycli", [
272
+ "set",
273
+ `--name=${this.info.name}`,
274
+ ...args,
275
+ ]),
276
+ { argumentLineWrapping: "inline" },
277
+ options,
278
+ ),
279
+ );
185
280
  return true;
186
281
  },
187
282
  };
package/src/get.ts CHANGED
@@ -14,6 +14,10 @@ export interface QuietOption {
14
14
  quiet?: boolean;
15
15
  }
16
16
 
17
+ export interface DetachOption {
18
+ detach?: boolean;
19
+ }
20
+
17
21
  export function print(
18
22
  command: PrintableShellCommand,
19
23
  printOptions?: Parameters<PrintableShellCommand["print"]>[0],
@@ -25,6 +29,17 @@ export function print(
25
29
  return command;
26
30
  }
27
31
 
32
+ export async function shellOutSilentOrDetach(
33
+ command: PrintableShellCommand,
34
+ options?: DetachOption,
35
+ ): Promise<void> {
36
+ if (options?.detach) {
37
+ command.spawnDetached();
38
+ } else {
39
+ await command.shellOut({ print: false });
40
+ }
41
+ }
42
+
28
43
  type GetDeviceOptions = {
29
44
  ignoreDisplayGroups?: true;
30
45
  } & QuietOption;