@domicile-desktop/system-apps 0.0.0-stage → 1.1.1
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.
- package/README.md +53 -2
- package/package.json +57 -4
- package/src/app-icons.ts +413 -0
- package/src/app-id-name.ts +10 -0
- package/src/apps-desktop.ts +12 -0
- package/src/bookmark.ts +39 -0
- package/src/content-type.ts +75 -0
- package/src/curl.ts +54 -0
- package/src/data-dirs.ts +29 -0
- package/src/data-url.ts +12 -0
- package/src/describe-apps.ts +110 -0
- package/src/desktop-entry.ts +98 -0
- package/src/environment.ts +38 -0
- package/src/escapes.ts +16 -0
- package/src/exec.ts +60 -0
- package/src/fake-desktop.ts +209 -0
- package/src/favicon.ts +164 -0
- package/src/favicons.ts +117 -0
- package/src/find-apps.ts +21 -0
- package/src/glob.ts +33 -0
- package/src/icon-theme-index.ts +148 -0
- package/src/installed.ts +135 -0
- package/src/mime-apps.ts +82 -0
- package/src/omit.ts +24 -0
- package/src/openers.ts +137 -0
- package/src/opens.ts +43 -0
- package/src/ranked.ts +60 -0
- package/src/results.ts +48 -0
- package/src/supertypes.ts +70 -0
package/src/omit.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Which desktop file IDs a launcher leaves out, as globs.
|
|
2
|
+
|
|
3
|
+
import { globOf } from "./glob";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Whether a desktop file ID is left out, per `patterns`.
|
|
7
|
+
*
|
|
8
|
+
* Each pattern is a glob: `*` matches any run, `?` one character and `[…]`
|
|
9
|
+
* one of a set. A pattern starting with `!` takes an ID back, and the last
|
|
10
|
+
* matching pattern wins, so `["*", "!launcher-*"]` keeps only IDs starting
|
|
11
|
+
* with `launcher-`. Throws on a pattern that is not a glob.
|
|
12
|
+
*/
|
|
13
|
+
export const omitting = (
|
|
14
|
+
patterns: readonly string[],
|
|
15
|
+
): ((id: string) => boolean) => {
|
|
16
|
+
const rules = patterns.map((pattern) => {
|
|
17
|
+
const keeps = pattern.startsWith("!");
|
|
18
|
+
return { glob: globOf(keeps ? pattern.slice(1) : pattern), keeps };
|
|
19
|
+
});
|
|
20
|
+
return (id) => {
|
|
21
|
+
const last = rules.findLast(({ glob }) => glob.test(id));
|
|
22
|
+
return last !== undefined && !last.keeps;
|
|
23
|
+
};
|
|
24
|
+
};
|
package/src/openers.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// The applications that open a file, its default first, and the argv each
|
|
2
|
+
// opens it with.
|
|
3
|
+
|
|
4
|
+
import type { Option, Result } from "@cprussin/option-result";
|
|
5
|
+
import { None, Ok } from "@cprussin/option-result";
|
|
6
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
7
|
+
import type { AppsDesktop } from "./apps-desktop";
|
|
8
|
+
|
|
9
|
+
import { contentTypeOf } from "./content-type";
|
|
10
|
+
import { dataDirs } from "./data-dirs";
|
|
11
|
+
import type { DesktopEntry } from "./desktop-entry";
|
|
12
|
+
import { desktopEnvironment, setVariable } from "./environment";
|
|
13
|
+
import { commandOf } from "./exec";
|
|
14
|
+
import { installedApp, installedApps } from "./installed";
|
|
15
|
+
import { defaultApps } from "./mime-apps";
|
|
16
|
+
import { allOk } from "./results";
|
|
17
|
+
import { supertypesOf } from "./supertypes";
|
|
18
|
+
|
|
19
|
+
/** What can open a file. */
|
|
20
|
+
export type Openers = {
|
|
21
|
+
/** The file's content type, by name. Nothing when no glob matches. */
|
|
22
|
+
contentType: Option<string>;
|
|
23
|
+
/**
|
|
24
|
+
* The installed applications for the type and its supertypes (see
|
|
25
|
+
* `./supertypes`), nearest first: every type's defaults from
|
|
26
|
+
* `mimeapps.list` in their order (see `./mime-apps`), then, type by type,
|
|
27
|
+
* the rest whose `MimeType` lists it, by name. The first is the one to
|
|
28
|
+
* open it with.
|
|
29
|
+
*/
|
|
30
|
+
apps: DesktopEntry[];
|
|
31
|
+
/** The argv that opens the file with `app`. */
|
|
32
|
+
command: (app: DesktopEntry) => string[];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* What can open `path`: relative to home, as every system call's path is, or
|
|
37
|
+
* absolute.
|
|
38
|
+
*
|
|
39
|
+
* The application `domicile open` would run comes first, as
|
|
40
|
+
* `domicile open --which` says, so this agrees with what opening the file
|
|
41
|
+
* does. The rest come from `MimeType`, the defaults and subclasses only:
|
|
42
|
+
* aliases and `mimeapps.list`'s added and removed associations are not read.
|
|
43
|
+
* A launcher falls back to `xdg-open` when this offers nothing.
|
|
44
|
+
*/
|
|
45
|
+
export const openers = async (
|
|
46
|
+
domicile: AppsDesktop,
|
|
47
|
+
path: string,
|
|
48
|
+
): Promise<Result<Openers, SystemError>> =>
|
|
49
|
+
(await desktopEnvironment(domicile)).andThenAsync(async (variables) => {
|
|
50
|
+
const file = path.startsWith("/") ? path : `${homeOf(variables)}/${path}`;
|
|
51
|
+
return (await dataDirs(domicile)).andThenAsync(async (dirs) =>
|
|
52
|
+
(await contentTypeOf(domicile, dirs, path)).andThenAsync(
|
|
53
|
+
async (contentType) =>
|
|
54
|
+
(
|
|
55
|
+
await contentType.match({
|
|
56
|
+
None: () => Promise.resolve(Ok<DesktopEntry[], SystemError>([])),
|
|
57
|
+
Some: (type) => appsFor(domicile, dirs, type),
|
|
58
|
+
})
|
|
59
|
+
).andThenAsync(async (apps) =>
|
|
60
|
+
(await openedWith(domicile, dirs, file)).map((first) => ({
|
|
61
|
+
apps: first.match({
|
|
62
|
+
None: () => apps,
|
|
63
|
+
Some: (app) => [app, ...apps.filter(({ id }) => id !== app.id)],
|
|
64
|
+
}),
|
|
65
|
+
command: (app: DesktopEntry) => commandFor(app, file),
|
|
66
|
+
contentType,
|
|
67
|
+
})),
|
|
68
|
+
),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The application `domicile open` would run for `file`, or nothing when it
|
|
75
|
+
* would run none. The `domicile` is the running desktop's own, first on its
|
|
76
|
+
* `PATH`.
|
|
77
|
+
*/
|
|
78
|
+
const openedWith = async (
|
|
79
|
+
domicile: AppsDesktop,
|
|
80
|
+
dirs: readonly string[],
|
|
81
|
+
file: string,
|
|
82
|
+
): Promise<Result<Option<DesktopEntry>, SystemError>> =>
|
|
83
|
+
(
|
|
84
|
+
await domicile.processes.run(["domicile", "open", "--which", file])
|
|
85
|
+
).andThenAsync(({ code, stdout }) => {
|
|
86
|
+
const id = stdout.trim();
|
|
87
|
+
return code !== 0 || id === ""
|
|
88
|
+
? Promise.resolve(Ok(None<DesktopEntry>()))
|
|
89
|
+
: installedApp(domicile, dirs, id);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/** The installed applications for `contentType`, in `Openers.apps`'s order. */
|
|
93
|
+
const appsFor = async (
|
|
94
|
+
domicile: AppsDesktop,
|
|
95
|
+
dirs: readonly string[],
|
|
96
|
+
contentType: string,
|
|
97
|
+
): Promise<Result<DesktopEntry[], SystemError>> =>
|
|
98
|
+
(await installedApps(domicile, dirs)).andThenAsync(async (installed) =>
|
|
99
|
+
(await supertypesOf(domicile, dirs, contentType)).andThenAsync(
|
|
100
|
+
async (supertypes) => {
|
|
101
|
+
const types = [contentType, ...supertypes];
|
|
102
|
+
return allOk(
|
|
103
|
+
await Promise.all(types.map((type) => defaultApps(domicile, type))),
|
|
104
|
+
).map((defaults) => {
|
|
105
|
+
const byId = new Map(installed.map((app) => [app.id, app]));
|
|
106
|
+
const first = defaults.flat().flatMap((id) => {
|
|
107
|
+
const app = byId.get(id);
|
|
108
|
+
return app === undefined ? [] : [app];
|
|
109
|
+
});
|
|
110
|
+
const rest = types.flatMap((type) =>
|
|
111
|
+
installed
|
|
112
|
+
.filter((app) => app.mimeTypes.includes(type))
|
|
113
|
+
.toSorted((a, b) => a.name.localeCompare(b.name)),
|
|
114
|
+
);
|
|
115
|
+
return [...new Set([...first, ...rest])];
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const homeOf = (variables: ReadonlyMap<string, string>): string => {
|
|
122
|
+
const home = setVariable(variables, "HOME");
|
|
123
|
+
if (home === undefined) {
|
|
124
|
+
throw new Error("the desktop has no HOME to open a file under");
|
|
125
|
+
} else {
|
|
126
|
+
return home;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** `app`'s argv for `file`. It was read once already, so it reads again. */
|
|
131
|
+
const commandFor = (app: DesktopEntry, file: string): string[] =>
|
|
132
|
+
commandOf(app.exec, file).match({
|
|
133
|
+
None: () => {
|
|
134
|
+
throw new Error(`${app.id}'s Exec could not be read again`);
|
|
135
|
+
},
|
|
136
|
+
Some: (command) => command,
|
|
137
|
+
});
|
package/src/opens.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Whether opening a file would run anything, before opening it, so a shell
|
|
2
|
+
// can say when nothing would.
|
|
3
|
+
|
|
4
|
+
import type { Result } from "@cprussin/option-result";
|
|
5
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
6
|
+
import type { AppsDesktop } from "./apps-desktop";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Whether an installed application opens `path`, as `domicile open` would:
|
|
10
|
+
* `domicile open --which` names one. `path` is relative to home, as every
|
|
11
|
+
* system call's path is, or absolute.
|
|
12
|
+
*/
|
|
13
|
+
export const opens = async (
|
|
14
|
+
domicile: Pick<AppsDesktop, "processes">,
|
|
15
|
+
path: string,
|
|
16
|
+
): Promise<Result<boolean, SystemError>> =>
|
|
17
|
+
(await domicile.processes.run(["sh", "-c", WHICH, NAME, path])).map(
|
|
18
|
+
({ code, stdout }) => code === 0 && stdout.trim() !== "",
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Asks `domicile open --which` of `$1`, prefixing `$HOME` to a relative path,
|
|
23
|
+
* since the page cannot read `$HOME`. `$1` is quoted, never interpolated, so
|
|
24
|
+
* a file name cannot inject a command. See `open-command.ts` in window
|
|
25
|
+
* management.
|
|
26
|
+
*/
|
|
27
|
+
const WHICH =
|
|
28
|
+
'case $1 in /*) exec domicile open --which "$1" ;; *) exec domicile open --which "$HOME/$1" ;; esac';
|
|
29
|
+
|
|
30
|
+
/** `$0` for the script, which prefixes `sh` error messages. */
|
|
31
|
+
const NAME = "domicile-opens";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* That no app opens the file `name`, for a shell to say so, naming the kind
|
|
35
|
+
* of file by its extension.
|
|
36
|
+
*/
|
|
37
|
+
export const nothingOpens = (name: string): string => {
|
|
38
|
+
const base = name.slice(name.lastIndexOf("/") + 1);
|
|
39
|
+
const dot = base.lastIndexOf(".");
|
|
40
|
+
return dot <= 0 || dot === base.length - 1
|
|
41
|
+
? "No app on this computer can open this file."
|
|
42
|
+
: `No app on this computer can open ${base.slice(dot).toLowerCase()} files.`;
|
|
43
|
+
};
|
package/src/ranked.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// The launcher's matching rule, shared by applications and bookmarks so their
|
|
2
|
+
// rows agree.
|
|
3
|
+
|
|
4
|
+
/** How to match and order one kind of row. */
|
|
5
|
+
export type Ranking<T> = {
|
|
6
|
+
/** The text every query word must appear in, in lower case. */
|
|
7
|
+
words: (item: T) => string;
|
|
8
|
+
name: (item: T) => string;
|
|
9
|
+
/** Orders equal names, so the order does not depend on a listing's. */
|
|
10
|
+
tieBreak: (item: T) => string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Up to `limit` of `items` matching `query`, best first.
|
|
15
|
+
*
|
|
16
|
+
* Every query word must appear, case-insensitively, in the item's words.
|
|
17
|
+
* Names that start with the query rank first, then sort by name.
|
|
18
|
+
*/
|
|
19
|
+
export const ranked = <T>(
|
|
20
|
+
items: readonly T[],
|
|
21
|
+
query: string,
|
|
22
|
+
limit: number,
|
|
23
|
+
ranking: Ranking<T>,
|
|
24
|
+
): T[] => {
|
|
25
|
+
const typed = query.trim().toLowerCase();
|
|
26
|
+
const words = typed.split(/\s+/).filter((word) => word !== "");
|
|
27
|
+
return items
|
|
28
|
+
.filter((item) => {
|
|
29
|
+
const text = ranking.words(item);
|
|
30
|
+
return words.every((word) => text.includes(word));
|
|
31
|
+
})
|
|
32
|
+
.map((item) => {
|
|
33
|
+
const name = ranking.name(item).toLowerCase();
|
|
34
|
+
return {
|
|
35
|
+
item,
|
|
36
|
+
key: [
|
|
37
|
+
name.startsWith(typed) ? 0 : 1,
|
|
38
|
+
name,
|
|
39
|
+
ranking.tieBreak(item),
|
|
40
|
+
] as const,
|
|
41
|
+
};
|
|
42
|
+
})
|
|
43
|
+
.toSorted((a, b) => compared(a.key, b.key))
|
|
44
|
+
.slice(0, limit)
|
|
45
|
+
.map(({ item }) => item);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Compares keys element by element, strings by UTF-16 code unit. */
|
|
49
|
+
const compared = (
|
|
50
|
+
a: readonly [number, string, string],
|
|
51
|
+
b: readonly [number, string, string],
|
|
52
|
+
): number => a[0] - b[0] || byCodeUnit(a[1], b[1]) || byCodeUnit(a[2], b[2]);
|
|
53
|
+
|
|
54
|
+
const byCodeUnit = (a: string, b: string): number => {
|
|
55
|
+
if (a < b) {
|
|
56
|
+
return -1;
|
|
57
|
+
} else {
|
|
58
|
+
return a > b ? 1 : 0;
|
|
59
|
+
}
|
|
60
|
+
};
|
package/src/results.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Combining the `Result`s of desktop calls made side by side.
|
|
2
|
+
|
|
3
|
+
import type { Result } from "@cprussin/option-result";
|
|
4
|
+
import { Ok } from "@cprussin/option-result";
|
|
5
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
6
|
+
import { SystemErrorKind } from "@domicile-desktop/sdk/system";
|
|
7
|
+
|
|
8
|
+
/** Every value, or the first error. */
|
|
9
|
+
export const allOk = <
|
|
10
|
+
T extends NonNullable<unknown>,
|
|
11
|
+
E extends NonNullable<unknown>,
|
|
12
|
+
>(
|
|
13
|
+
results: readonly Result<T, E>[],
|
|
14
|
+
): Result<T[], E> =>
|
|
15
|
+
results.reduce<Result<T[], E>>(
|
|
16
|
+
(all, each) =>
|
|
17
|
+
all.andThen((values) => each.map((value) => [...values, value])),
|
|
18
|
+
Ok([]),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `result`, with `absent` in place of an error that means the path is not
|
|
23
|
+
* there to read: missing, of the other kind, or unreadable. The spec says to
|
|
24
|
+
* skip those. Any other error, such as a locked desktop, is kept.
|
|
25
|
+
*/
|
|
26
|
+
export const orAbsent = <T extends NonNullable<unknown>>(
|
|
27
|
+
result: Result<T, SystemError>,
|
|
28
|
+
absent: T,
|
|
29
|
+
): Result<T, SystemError> =>
|
|
30
|
+
result.orElse((error) => {
|
|
31
|
+
switch (error.kind) {
|
|
32
|
+
case SystemErrorKind.NotFound:
|
|
33
|
+
case SystemErrorKind.NotADirectory:
|
|
34
|
+
case SystemErrorKind.IsADirectory:
|
|
35
|
+
case SystemErrorKind.PermissionDenied: {
|
|
36
|
+
return Ok(absent);
|
|
37
|
+
}
|
|
38
|
+
case SystemErrorKind.AlreadyExists:
|
|
39
|
+
case SystemErrorKind.InvalidInput:
|
|
40
|
+
case SystemErrorKind.Locked:
|
|
41
|
+
case SystemErrorKind.Dbus:
|
|
42
|
+
case SystemErrorKind.Canceled:
|
|
43
|
+
case SystemErrorKind.CompositorGone:
|
|
44
|
+
case SystemErrorKind.Other: {
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// A content type's supertypes, from shared-mime-info's `mime/subclasses`. See
|
|
2
|
+
// https://specifications.freedesktop.org/shared-mime-info-spec/latest/.
|
|
3
|
+
|
|
4
|
+
import type { Result } from "@cprussin/option-result";
|
|
5
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
6
|
+
import type { AppsDesktop } from "./apps-desktop";
|
|
7
|
+
|
|
8
|
+
import { allOk, orAbsent } from "./results";
|
|
9
|
+
|
|
10
|
+
/** The type the spec makes every other text/* type a subclass of. */
|
|
11
|
+
const TEXT = "text/plain";
|
|
12
|
+
|
|
13
|
+
/** One `subclasses` line: a type and one of its parents. */
|
|
14
|
+
type Subclass = { type: string; parent: string };
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `contentType`'s supertypes, nearest first, without `contentType` itself.
|
|
18
|
+
*
|
|
19
|
+
* Reads `mime/subclasses` under each of `dataDirs`, missing ones skipped, and
|
|
20
|
+
* follows each parent to its own. Every text/* type is also a text/plain.
|
|
21
|
+
*/
|
|
22
|
+
export const supertypesOf = async (
|
|
23
|
+
domicile: AppsDesktop,
|
|
24
|
+
dataDirs: readonly string[],
|
|
25
|
+
contentType: string,
|
|
26
|
+
): Promise<Result<string[], SystemError>> =>
|
|
27
|
+
allOk(
|
|
28
|
+
await Promise.all(
|
|
29
|
+
dataDirs.map(async (dir) =>
|
|
30
|
+
orAbsent(
|
|
31
|
+
await domicile.files.readText(`${dir}/mime/subclasses`),
|
|
32
|
+
"",
|
|
33
|
+
).map(subclassesIn),
|
|
34
|
+
),
|
|
35
|
+
),
|
|
36
|
+
).map((subclasses) =>
|
|
37
|
+
ancestorsOf(subclasses.flat(), [contentType], [contentType]).slice(1),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/** `seen`, then the parents of `level`'s types, then theirs, each once. */
|
|
41
|
+
const ancestorsOf = (
|
|
42
|
+
subclasses: readonly Subclass[],
|
|
43
|
+
level: readonly string[],
|
|
44
|
+
seen: readonly string[],
|
|
45
|
+
): readonly string[] => {
|
|
46
|
+
const parents = [
|
|
47
|
+
...new Set(level.flatMap((type) => parentsOf(subclasses, type))),
|
|
48
|
+
].filter((type) => !seen.includes(type));
|
|
49
|
+
return parents.length === 0
|
|
50
|
+
? seen
|
|
51
|
+
: ancestorsOf(subclasses, parents, [...seen, ...parents]);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** `type`'s own parents: those `subclasses` lists, then text/plain for text. */
|
|
55
|
+
const parentsOf = (subclasses: readonly Subclass[], type: string): string[] => [
|
|
56
|
+
...subclasses
|
|
57
|
+
.filter((subclass) => subclass.type === type)
|
|
58
|
+
.map(({ parent }) => parent),
|
|
59
|
+
...(type.startsWith("text/") && type !== TEXT ? [TEXT] : []),
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
/** The pairs `text` lists: `type parent`. */
|
|
63
|
+
const subclassesIn = (text: string): Subclass[] =>
|
|
64
|
+
text
|
|
65
|
+
.split("\n")
|
|
66
|
+
.filter((line) => line !== "")
|
|
67
|
+
.map((line) => {
|
|
68
|
+
const [type = "", parent = ""] = line.split(" ");
|
|
69
|
+
return { parent, type };
|
|
70
|
+
});
|