Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions clippath/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cleanupSchedule": "1h",
"dailyHour": 3,
"wslMode": null,
"shortcut": "Ctrl+Shift+V",
"imageFormat": "bmp"
}
254 changes: 254 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
"test": "bun test"
},
"devDependencies": {
"@biomejs/biome": "^2.3.15",
"@biomejs/biome": "^2.4.11",
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
},
"dependencies": {
"jpeg-js": "^0.4.4"
}
}
18 changes: 10 additions & 8 deletions src/app/hotkey-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import { existsSync } from "node:fs";
import { extractClipboardDIB } from "../clipboard/extract.ts";
import { hasClipboardImage } from "../clipboard/monitor.ts";
import { saveDibAsBmp } from "../image/temp-files.ts";
import { getConfig } from "../config.ts";
import { saveDibAsImage } from "../image/temp-files.ts";
import { pasteString } from "../input/clipboard-paste.ts";
import { getPathForTerminal } from "../wsl.ts";

let cachedBmpPath: string | null = null;
let cachedImagePath: string | null = null;

/** Clear the cached path (e.g. after cleanup deletes all files). */
export function clearCachedPath(): void {
cachedBmpPath = null;
cachedImagePath = null;
}

/** Handle the Ctrl+Shift+V hotkey press. */
Expand All @@ -28,25 +29,26 @@ export function handleHotkey(): void {
if (dibData) {
console.log(`[hotkey] DIB data extracted, size=${dibData.length} bytes`);
try {
cachedBmpPath = saveDibAsBmp(dibData);
console.log(`[hotkey] Saved: ${cachedBmpPath}`);
const format = getConfig().imageFormat;
cachedImagePath = saveDibAsImage(dibData, format);
console.log(`[hotkey] Saved: ${cachedImagePath}`);
} catch (e) {
console.error("[hotkey] Error saving image:", e);
}
}
}

// Use cached path if available and file still exists
if (cachedBmpPath && existsSync(cachedBmpPath)) {
const pathToType = getPathForTerminal(cachedBmpPath);
if (cachedImagePath && existsSync(cachedImagePath)) {
const pathToType = getPathForTerminal(cachedImagePath);
console.log(`[hotkey] Pasting: ${pathToType}`);

setTimeout(() => {
const ok = pasteString(pathToType);
console.log(`[hotkey] pasteString result: ${ok}`);
}, 0);
} else {
cachedBmpPath = null;
cachedImagePath = null;
console.log("[hotkey] No image available.");
}
}
9 changes: 9 additions & 0 deletions src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("config", () => {
expect(config).toHaveProperty("cleanupSchedule");
expect(config).toHaveProperty("dailyHour");
expect(config).toHaveProperty("wslMode");
expect(config).toHaveProperty("imageFormat");
});

it("getConfig returns the same object as loadConfig produced", () => {
Expand Down Expand Up @@ -52,6 +53,13 @@ describe("config", () => {
expect(getConfig().cleanupSchedule).toBe(schedule);
}
});

it("updateConfig supports all imageFormat values", () => {
for (const format of ["bmp", "png", "jpeg"] as const) {
updateConfig({ imageFormat: format });
expect(getConfig().imageFormat).toBe(format);
}
});
});

describe("errors", () => {
Expand Down Expand Up @@ -79,6 +87,7 @@ describe("config", () => {
expect(config.cleanupSchedule).not.toBeUndefined();
expect(config.dailyHour).not.toBeUndefined();
expect(config.wslMode !== undefined).toBe(true);
expect(config.imageFormat).not.toBeUndefined();
});
});
});
Loading