Repository navigation
Expand file tree
/
Copy pathsystem.ts
More file actions
135 lines (123 loc) · 4.24 KB
/
Copy pathsystem.ts
File metadata and controls
135 lines (123 loc) · 4.24 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Environment, installed tools, the background job and the history folder.
import { exec } from "@backslash/api";
import {
detectSession,
type Env,
type Family,
type Grabber,
grabberCandidates,
normalizeLanguages,
ocrScript,
osFamily,
parseLanguages,
parseVersion,
type Session,
SESSION_ENV,
systemdRunArgs,
} from "./lib.ts";
const env = (key: string): string | undefined => {
try {
return Deno.env.get(key);
} catch {
return undefined;
}
};
export const HOME = env("HOME") ?? "";
export const HISTORY = `${HOME}/.cache/backslash/ocr`;
const sessionEnv = (): Env => Object.fromEntries(SESSION_ENV.map((k) => [k, env(k)]));
export const session = (): Session => detectSession(sessionEnv());
/** Whether a program can be started: installed and allowed. */
async function installed(program: string, args: string[]): Promise<boolean> {
try {
await new Deno.Command(program, {
args,
stdin: "null",
stdout: "null",
stderr: "null",
signal: AbortSignal.timeout(3000),
}).output();
return true;
} catch (e) {
return !(e instanceof Deno.errors.NotFound || e instanceof Deno.errors.PermissionDenied ||
e instanceof Deno.errors.NotCapable);
}
}
export interface Setup {
session: Session;
grabber?: Grabber;
slurp: boolean;
tesseract?: string;
languages: string[];
}
/** What is installed: the area grabber, tesseract (its version) and its languages. */
export async function setup(): Promise<Setup> {
const s = session();
const candidates = grabberCandidates(s);
const [found, slurp, version, langs] = await Promise.all([
Promise.all(
candidates.map((c) => installed(c, c === "import" ? ["-version"] : c === "grim" ? ["-h"] : ["--version"])),
),
installed("slurp", ["-h"]),
exec("tesseract", ["--version"]).catch((e) => e instanceof Deno.errors.NotFound ? undefined : String(e)),
exec("tesseract", ["--list-langs"]).catch(() => ""),
]);
return {
session: s,
grabber: candidates.find((_, i) => found[i]),
slurp,
tesseract: version === undefined ? undefined : parseVersion(version) ?? "installed",
languages: parseLanguages(langs),
};
}
export async function family(): Promise<Family | undefined> {
return osFamily(await Deno.readTextFile("/etc/os-release").catch(() => ""));
}
/** The languages to read: the preference, else English. */
export const preferredLanguages = (prefs: Record<string, unknown>) =>
normalizeLanguages(String(prefs.languages ?? "")) ?? "eng";
export class Missing extends Error {}
/** Starts the selection in the background, once the launcher is hidden. */
export async function recognize(languages: string, o: { known?: Setup; wait?: number } = {}): Promise<void> {
const s = o.known ?? await setup();
if (!s.tesseract) throw new Missing("Tesseract is not installed");
if (!s.grabber) throw new Missing("No screenshot tool to select an area");
if (s.grabber === "grim" && !s.slurp) throw new Missing("slurp is needed to select an area");
const absent = languages.split("+").filter((l) => s.languages.length && !s.languages.includes(l));
if (absent.length) throw new Missing(`Tesseract has no ${absent.join(", ")} model installed`);
const script = ocrScript({
tool: s.grabber,
languages,
historyDir: HISTORY,
wayland: s.session.wayland,
wait: o.wait,
});
try {
await exec("systemd-run", systemdRunArgs(`backslash-ocr-${Date.now()}`, script, sessionEnv()));
} catch (e) {
if (String(e).includes("Failed to connect to bus")) {
throw new Error("No systemd user session: the selection runs in the background with systemd-run --user");
}
throw e;
}
}
export interface Recognized {
path: string;
text: string;
date: number;
}
/** Recognized texts, newest first. */
export async function history(): Promise<Recognized[]> {
const out: Recognized[] = [];
try {
for await (const e of Deno.readDir(HISTORY)) {
const m = e.name.match(/^(\d+)\.txt$/);
if (!e.isFile || !m) continue;
const path = `${HISTORY}/${e.name}`;
const text = (await Deno.readTextFile(path).catch(() => "")).trim();
if (text) out.push({ path, text, date: Number(m[1]) * 1000 });
}
} catch {
// Nothing recognized yet.
}
return out.sort((a, b) => b.date - a.date);
}