@tacone/prosey 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.
- package/README.md +63 -12
- package/bin/prosey +113235 -6858
- package/package.json +3 -3
- package/src/config-command.test.ts +50 -0
- package/src/config.test.ts +3 -3
- package/src/config.ts +12 -0
- package/src/debug.test.ts +87 -0
- package/src/debug.ts +47 -5
- package/src/default-config.toml +24 -1
- package/src/format.test.ts +17 -1
- package/src/index.ts +130 -27
- package/src/pager.test.ts +65 -0
- package/src/pager.ts +23 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, test, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { hasCommand, detectPager } from "./pager";
|
|
3
|
+
|
|
4
|
+
const ORIGINAL_PROSEY_PAGER = process.env.PROSEY_PAGER;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
delete process.env.PROSEY_PAGER;
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
if (ORIGINAL_PROSEY_PAGER !== undefined) {
|
|
12
|
+
process.env.PROSEY_PAGER = ORIGINAL_PROSEY_PAGER;
|
|
13
|
+
} else {
|
|
14
|
+
delete process.env.PROSEY_PAGER;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("hasCommand", () => {
|
|
19
|
+
test("returns true for existing command", () => {
|
|
20
|
+
expect(hasCommand("sh")).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("returns false for nonexistent command", () => {
|
|
24
|
+
expect(hasCommand("nonexistent-cmd-12345")).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("detectPager", () => {
|
|
29
|
+
test("uses PROSEY_PAGER env var", () => {
|
|
30
|
+
process.env.PROSEY_PAGER = "custom-pager";
|
|
31
|
+
expect(detectPager()).toBe("custom-pager");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("env var takes precedence over config pager", () => {
|
|
35
|
+
process.env.PROSEY_PAGER = "env-pager";
|
|
36
|
+
expect(detectPager("cfg-pager")).toBe("env-pager");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('"auto" env var falls through to config', () => {
|
|
40
|
+
process.env.PROSEY_PAGER = "auto";
|
|
41
|
+
expect(detectPager("cfg-pager")).toBe("cfg-pager");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("empty env var falls through to config", () => {
|
|
45
|
+
process.env.PROSEY_PAGER = "";
|
|
46
|
+
expect(detectPager("cfg-pager")).toBe("cfg-pager");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('"auto" config falls through to auto-detection', () => {
|
|
50
|
+
process.env.PROSEY_PAGER = "auto";
|
|
51
|
+
const pager = detectPager("auto");
|
|
52
|
+
// Falls through to auto-detect: one of bat/glow/mdcat/less or null
|
|
53
|
+
expect(pager === null || typeof pager === "string").toBe(true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("returns null when no pager is configured or available", () => {
|
|
57
|
+
const pager = detectPager();
|
|
58
|
+
// In CI or minimal environments, no pagers may be installed
|
|
59
|
+
// If a pager IS found, it should be one of the expected ones
|
|
60
|
+
if (pager !== null) {
|
|
61
|
+
const known = ["bat -lmd", "glow", "mdcat -l -p", "less"];
|
|
62
|
+
expect(known).toContain(pager);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
package/src/pager.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
export function hasCommand(cmd: string): boolean {
|
|
4
|
+
try {
|
|
5
|
+
execSync(`command -v ${cmd} 2>/dev/null`, { stdio: "ignore" });
|
|
6
|
+
return true;
|
|
7
|
+
} catch {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function detectPager(cfgPager?: string): string | null {
|
|
13
|
+
const env = process.env.PROSEY_PAGER;
|
|
14
|
+
if (env !== undefined && env !== "" && env !== "auto") return env;
|
|
15
|
+
|
|
16
|
+
if (cfgPager !== undefined && cfgPager !== "" && cfgPager !== "auto") return cfgPager;
|
|
17
|
+
|
|
18
|
+
if (hasCommand("bat")) return "bat -lmd";
|
|
19
|
+
if (hasCommand("glow")) return "glow";
|
|
20
|
+
if (hasCommand("mdcat")) return "mdcat -l -p";
|
|
21
|
+
if (hasCommand("less")) return "less";
|
|
22
|
+
return null;
|
|
23
|
+
}
|