cliphist update
This commit is contained in:
1 parent
71ac2238eb
commit
07063c5eb7
6 files changed
+222
-75
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import Gtk from "gi://Gtk?version=3.0";
|
||||
const { Gio } = imports.gi;
|
||||
const GLib = imports.gi.GLib;
|
||||
import Box from "types/widgets/box";
|
||||
import { WINDOW_NAME } from "modules/sideleft/main";
|
||||
|
||||
async function getClipboardHistory() {
|
||||
try {
|
||||
const process = new Gio.Subprocess({
|
||||
argv: ['bash', '-c', `${App.configDir}/scripts/cliphist.sh --get`],
|
||||
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
|
||||
});
|
||||
|
||||
process.init(null);
|
||||
const [success, stdout] = await process.communicate_utf8(null, null);
|
||||
if (!success) return [];
|
||||
|
||||
return stdout.split("\n").filter(line => line.trim() !== "");
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function copyById(id) {
|
||||
try {
|
||||
const command = `${App.configDir}/scripts/cliphist.sh --copy-by-id ${id}`;
|
||||
const entry = await Utils.execAsync(command);
|
||||
|
||||
if (!entry) return;
|
||||
|
||||
const process = new Gio.Subprocess({
|
||||
argv: ['bash', '-c', `echo "${entry.trim()}" | wl-copy`],
|
||||
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
|
||||
});
|
||||
|
||||
process.init(null);
|
||||
await process.communicate_utf8(null, null);
|
||||
} catch (error) {
|
||||
// Handle error silently or log to a file if needed
|
||||
}
|
||||
}
|
||||
|
||||
function ClipHistItem(entry) {
|
||||
const [id, ...content] = entry.split("\t");
|
||||
let clickCount = 0;
|
||||
const button = Widget.Button({
|
||||
class_name: "clip_container",
|
||||
child: Widget.Box({
|
||||
children: [
|
||||
Widget.Label({ label: id, class_name: "clip_id", xalign: 0, vpack: "center" }),
|
||||
Widget.Label({ label: "・", class_name: "dot_divider", xalign: 0, vpack: "center" }),
|
||||
Widget.Label({ label: content.join(" ").trim(), class_name: "clip_label", xalign: 0, vpack: "center", truncate: "end" })
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
button.connect("clicked", async () => {
|
||||
try {
|
||||
clickCount++;
|
||||
if (clickCount === 2) {
|
||||
await copyById(id);
|
||||
App.closeWindow(WINDOW_NAME);
|
||||
clickCount = 0;
|
||||
}
|
||||
} catch (error) {
|
||||
// Handle error silently or log to a file if needed
|
||||
}
|
||||
});
|
||||
|
||||
button.connect("focus-out-event", () => {
|
||||
clickCount = 0;
|
||||
});
|
||||
|
||||
return Widget.Box({
|
||||
attribute: { content: content.join(" ").trim() },
|
||||
orientation: Gtk.Orientation.VERTICAL,
|
||||
children: [
|
||||
button,
|
||||
Widget.Separator({ class_name: "clip_divider", orientation: Gtk.Orientation.HORIZONTAL })
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
export const cliphist = () => {
|
||||
const list = Widget.Box({ vertical: true, vexpand: true });
|
||||
const loadingIndicator = Widget.Label({ label: "Loading...", class_name: "loading" });
|
||||
list.children = [loadingIndicator];
|
||||
|
||||
async function repopulate() {
|
||||
try {
|
||||
const entries = await getClipboardHistory();
|
||||
list.children = []; // Clear loading indicator
|
||||
|
||||
if (entries.length > 0) {
|
||||
list.children = entries.map(ClipHistItem);
|
||||
} else {
|
||||
list.children = [Widget.Label({ label: "No history available", class_name: "no_history" })];
|
||||
}
|
||||
} catch (error) {
|
||||
list.children = [Widget.Label({ label: "Error loading history", class_name: "error_loading" })];
|
||||
}
|
||||
}
|
||||
|
||||
repopulate();
|
||||
|
||||
const entry = Widget.Entry({
|
||||
hexpand: true,
|
||||
class_name: "cliphistory_entry",
|
||||
placeholder_text: "Search",
|
||||
on_change: ({ text }) => {
|
||||
const searchText = text.toLowerCase();
|
||||
list.children.forEach((item) => {
|
||||
item.visible = item.attribute.content.toLowerCase().includes(searchText);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return Widget.Box({
|
||||
vertical: true,
|
||||
class_name: "cliphistory_box",
|
||||
margin_top: 14,
|
||||
margin_right: 14,
|
||||
children: [
|
||||
entry,
|
||||
Widget.Separator(),
|
||||
Widget.Scrollable({
|
||||
hscroll: "never",
|
||||
child: list,
|
||||
vexpand: true
|
||||
})
|
||||
],
|
||||
setup: (self) =>
|
||||
self.hook(App, (_, windowName, visible) => {
|
||||
if (windowName !== WINDOW_NAME) return;
|
||||
|
||||
if (visible) {
|
||||
repopulate();
|
||||
entry.text = "";
|
||||
}
|
||||
})
|
||||
});
|
||||
};
|
||||
@@ -4,6 +4,7 @@ import { MaterialIcon } from "icons.ts";
|
||||
import { WeatherBox } from "./weather.ts";
|
||||
import { Media } from "./players.ts";
|
||||
import { Applauncher } from "./applauncher.ts";
|
||||
import { cliphist } from "./cliphist.ts";
|
||||
import { geminiPage } from "./gemini.ts";
|
||||
|
||||
export const WINDOW_NAME = "sideleft";
|
||||
@@ -23,7 +24,14 @@ export function toggleMediaWindow() {
|
||||
shown.setValue("media");
|
||||
}
|
||||
}
|
||||
export function toggleClipsWindow() {
|
||||
if (shown.value == "clips" && sideleft.visible) App.closeWindow(WINDOW_NAME); else {
|
||||
App.openWindow(WINDOW_NAME);
|
||||
shown.setValue("clips");
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.toggleClipsWindow = toggleClipsWindow;
|
||||
globalThis.toggleMediaWindow = toggleMediaWindow;
|
||||
globalThis.toggleAppsWindow = toggleAppsWindow;
|
||||
|
||||
@@ -75,6 +83,7 @@ function Navigation() {
|
||||
weather: WeatherBox(),
|
||||
media: Media(),
|
||||
apps: Applauncher(),
|
||||
clips: cliphist(),
|
||||
gemini: geminiPage
|
||||
},
|
||||
hexpand: true,
|
||||
@@ -104,6 +113,11 @@ function Navigation() {
|
||||
label: "Apps",
|
||||
icon: "search"
|
||||
}),
|
||||
Button({
|
||||
page: "clips",
|
||||
label: "Clipboard",
|
||||
icon: "content_paste_search",
|
||||
}),
|
||||
Button({
|
||||
page: "gemini",
|
||||
label: "Gemini",
|
||||
|
||||
Reference in new issue
Block a user