From 07063c5eb708ebca339f89d73e7f929acf198cb9 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Tue, 16 Jul 2024 17:14:23 +0545 Subject: [PATCH] cliphist update --- config/ags/main.ts | 4 +- config/ags/modules/cliphist.ts | 95 ++++++---------- config/ags/modules/sideleft/cliphist.ts | 142 ++++++++++++++++++++++++ config/ags/modules/sideleft/main.ts | 14 +++ config/ags/scripts/cliphist.sh | 20 ++++ config/ags/style.css | 22 ++-- 6 files changed, 222 insertions(+), 75 deletions(-) create mode 100644 config/ags/modules/sideleft/cliphist.ts create mode 100755 config/ags/scripts/cliphist.sh diff --git a/config/ags/main.ts b/config/ags/main.ts index 3a74aeee..7626872b 100644 --- a/config/ags/main.ts +++ b/config/ags/main.ts @@ -4,7 +4,7 @@ import Gdk from "gi://Gdk"; // widgets import { Bar, BarCornerTopLeft, BarCornerTopRight } from "./modules/bar.ts"; import { Notifications } from "./modules/notificationPopups.ts"; -import { cliphist } from "./modules/cliphist.ts"; +//import { cliphist } from "./modules/cliphist.ts"; import { sideright } from "./modules/sideright/main.ts"; import { sideleft } from "./modules/sideleft/main.ts"; import {} from "apps/settings/main.ts"; @@ -27,7 +27,7 @@ const Windows = () => [ forMonitors(Notifications), forMonitors(BarCornerTopLeft), forMonitors(BarCornerTopRight), - cliphist, + //cliphist, sideright, cheatsheet, sideleft diff --git a/config/ags/modules/cliphist.ts b/config/ags/modules/cliphist.ts index 93e20fdf..ee870081 100644 --- a/config/ags/modules/cliphist.ts +++ b/config/ags/modules/cliphist.ts @@ -2,64 +2,22 @@ const WINDOW_NAME = "cliphist"; import popupwindow from "./misc/popupwindow.ts"; import Box from "types/widgets/box.js"; import Gtk from "gi://Gtk?version=3.0"; -const { Gio } = imports.gi; type EntryObject = { - original: string; - text: string; - index: number; + id: string; + content: string; + entry: string; }; -async function getClipboardHistory(): Promise { - try { - const process = new Gio.Subprocess({ - argv: ['bash', '-c', 'cliphist list | iconv -f UTF-8 -t UTF-8//IGNORE'], - flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE - }); - - process.init(null); - - const [success, stdout, stderr] = process.communicate_utf8(null, null); - if (!success) { - return []; - } - - const sanitizedOutput = stdout.replace(/[^\u0000-\u007F]/g, ""); - - return sanitizedOutput - .split("\n") - .filter((line) => line.trim() !== "") - .map((entry) => { - const [index, text] = entry.split("\t").map((s) => s.trim()); - return { original: entry, text, index: Number(index) }; - }); - } catch { - return []; - } -} - -async function copyById(id: number) { - try { - const entry = await Utils.execAsync(`cliphist decode ${id}`); - - 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); - process.communicate_utf8(null, null); - } catch {} -} - -function ClipHistItem(entry: EntryObject) { +function ClipHistItem(entry: string) { + let [id, ...content] = entry.split("\t"); let clickCount = 0; - const button = Widget.Button({ + let button = Widget.Button({ class_name: "clip_container", child: Widget.Box({ children: [ Widget.Label({ - label: entry.index.toString(), + label: id, class_name: "clip_id", xalign: 0, vpack: "center" @@ -71,7 +29,7 @@ function ClipHistItem(entry: EntryObject) { vpack: "center" }), Widget.Label({ - label: entry.text, + label: content.join(" ").trim(), class_name: "clip_label", xalign: 0, vpack: "center", @@ -81,11 +39,11 @@ function ClipHistItem(entry: EntryObject) { }) }); - button.connect("clicked", async () => { + button.connect("clicked", () => { clickCount++; - if (clickCount === 1) { + if (clickCount === 2) { App.closeWindow(WINDOW_NAME); - await copyById(entry.index); + Utils.execAsync(`${App.configDir}/scripts/cliphist.sh --copy-by-id ${id}`); clickCount = 0; } }); @@ -95,7 +53,7 @@ function ClipHistItem(entry: EntryObject) { }); return Widget.Box({ - attribute: { content: entry.text }, + attribute: { content: content.join(" ").trim() }, orientation: Gtk.Orientation.VERTICAL, children: [ button, @@ -108,6 +66,9 @@ function ClipHistItem(entry: EntryObject) { } function ClipHistWidget({ width = 500, height = 500, spacing = 12 }) { + let output: string; + let entries: string[]; + let clipHistItems: EntryObject[]; let widgets: Box[]; const list = Widget.Box({ @@ -116,20 +77,31 @@ function ClipHistWidget({ width = 500, height = 500, spacing = 12 }) { }); async function repopulate() { - const clipHistItems = await getClipboardHistory(); - widgets = clipHistItems.map((item) => ClipHistItem(item)); + output = await Utils.execAsync(`${App.configDir}/scripts/cliphist.sh --get`) + .then((str) => str) + .catch((err) => { + print(err); + return ""; + }); + entries = output.split("\n").filter((line) => line.trim() !== ""); + clipHistItems = entries.map((entry) => { + let [id, ...content] = entry.split("\t"); + return { id: id.trim(), content: content.join(" ").trim(), entry: entry }; + }); + widgets = clipHistItems.map((item) => ClipHistItem(item.entry)); list.children = widgets; } + repopulate(); const entry = Widget.Entry({ hexpand: true, class_name: "cliphistory_entry", placeholder_text: "Search", + on_change: ({ text }) => { - const searchText = text.toLowerCase(); + const searchText = (text ?? "").toLowerCase(); widgets.forEach((item) => { - const content = item.attribute.content.toLowerCase(); - item.visible = content.includes(searchText); + item.visible = item.attribute.content.toLowerCase().includes(searchText); }); } }); @@ -144,7 +116,7 @@ function ClipHistWidget({ width = 500, height = 500, spacing = 12 }) { Widget.Separator(), Widget.Scrollable({ hscroll: "never", - css: `min-width: ${width}px; min-height: ${height}px;`, + css: `min-width: ${width}px;` + `min-height: ${height}px;`, child: list }) ], @@ -162,6 +134,7 @@ function ClipHistWidget({ width = 500, height = 500, spacing = 12 }) { export const cliphist = popupwindow({ name: WINDOW_NAME, + class_name: "cliphistory", visible: false, keymode: "exclusive", @@ -170,5 +143,5 @@ export const cliphist = popupwindow({ height: 500, spacing: 0 }), - anchor: ["bottom", "right"] + anchor: ["top", "right"] }); diff --git a/config/ags/modules/sideleft/cliphist.ts b/config/ags/modules/sideleft/cliphist.ts new file mode 100644 index 00000000..41193b6b --- /dev/null +++ b/config/ags/modules/sideleft/cliphist.ts @@ -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 = ""; + } + }) + }); +}; diff --git a/config/ags/modules/sideleft/main.ts b/config/ags/modules/sideleft/main.ts index 8f748b72..e4a24659 100644 --- a/config/ags/modules/sideleft/main.ts +++ b/config/ags/modules/sideleft/main.ts @@ -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", diff --git a/config/ags/scripts/cliphist.sh b/config/ags/scripts/cliphist.sh new file mode 100755 index 00000000..79ee6613 --- /dev/null +++ b/config/ags/scripts/cliphist.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +get() { + clip_list=$(cliphist list) + + while IFS= read -r line; do + echo "$line" | iconv -t UTF-8 + done <<<"$clip_list" +} + +copy_by_id() { + id=$1 + cliphist decode $id | wl-copy +} + +if [[ "$1" == "--get" ]]; then + get +elif [[ "$1" == "--copy-by-id" ]]; then + { copy_by_id "$2"; } +fi diff --git a/config/ags/style.css b/config/ags/style.css index 74b0651e..d6d64e77 100644 --- a/config/ags/style.css +++ b/config/ags/style.css @@ -106,7 +106,7 @@ separator { .outer-workspace { background-color: @surfaceContainerHigh; - border-radius: 20px; + border-radius: 16px; } .workspace { @@ -121,10 +121,10 @@ separator { } .workspace.first { - border-radius: 20px 0 0 20px; + border-radius: 16px 0 0 16px; } .workspace.last { - border-radius: 0 20px 20px 0; + border-radius: 0 16px 16px 0; } .workspace.normal, @@ -134,7 +134,7 @@ separator { } .workspace button.normal:hover { - border-radius: 20px; + border-radius: 16px; background-color: @secondary; color: @surfaceDim; } @@ -145,11 +145,11 @@ separator { font-weight: bold; padding-left: 4px; padding-right: 4px; - border-radius: 20px; + border-radius: 16px; } .workspace button.focused:hover { - border-radius: 20px; + border-radius: 16px; color: @primary; background-color: mix(@onSurface, @surface, 0.85); } @@ -159,17 +159,17 @@ separator { } .workspaces button.occupied:hover { - border-radius: 20px; + border-radius: 16px; background-color: mix(@onSurface, @surface, 0.85); color: @surfaceDim; } .workspace.occupied-single { - border-radius: 20px; + border-radius: 16px; } .workspace.occupied-start { - border-radius: 20px 0 0 20px; + border-radius: 16px 0 0 16px; } .workspace.occupied-middle { @@ -177,7 +177,7 @@ separator { } .workspace.occupied-end { - border-radius: 0px 20px 20px 0px; + border-radius: 0px 16px 16px 0px; } /* ----------------------------------------------------- @@ -302,8 +302,6 @@ window.notification-popups box.notifications { .cliphistory_box { border-radius: 24px; background-color: @background; - border: 2px solid; - border-color: @outlineVariant; } .cliphistory_box separator {