nixos-configs/nixosModules/ags/config/services/clipboard.ts

148 lines
4.1 KiB
TypeScript
Raw Normal View History

const { execAsync, subprocess } = Utils;
class Clipboard extends Service {
static {
Service.register(this, {
'clip-added': ['jsobject'],
'history-searched': [],
}, {
clips: ['jsobject'],
});
}
private static _MAX_CLIPS = 100;
// Class Attributes
private _clips_left = 0;
private _clips = new Map<number, { clip: string, isImage: boolean }>();
get clips() {
return this._clips;
}
constructor() {
super();
this._getHistory();
this._watchClipboard();
}
// Public Class Methods
public copyOldItem(key: string | number): void {
execAsync([
'bash', '-c', `cliphist list | grep '^${key}' | cliphist decode | wl-copy`,
]);
}
// Private Class Methods
private _decrementClipsLeft() {
if (--this._clips_left === 0) {
this.emit('history-searched');
chore: update flake.lock Flake Inputs: • Updated input 'astal': 'github:Aylur/astal/004b123' (2024-10-22) → 'github:Aylur/astal/306e649' (2024-10-23) • Updated input 'custom-sidebar-src': 'github:elchininet/custom-sidebar/80942f5' (2024-10-21) → 'github:elchininet/custom-sidebar/5962ac7' (2024-10-24) • Updated input 'discord-overlay': 'github:matt1432/discord-nightly-overlay/256588e' (2024-10-06) → 'github:matt1432/discord-nightly-overlay/e4547ee' (2024-10-23) • Updated input 'gpu-screen-recorder-src': 'git+https://repo.dec05eba.com/gpu-screen-recorder?ref=refs/heads/master&rev=1016597' (2024-10-18) → 'git+https://repo.dec05eba.com/gpu-screen-recorder?ref=refs/heads/master&rev=422f214' (2024-10-22) • Updated input 'gtk-theme-src': 'github:dracula/gtk/e5c95d7' (2024-10-12) → 'github:dracula/gtk/a698492' (2024-10-21) • Updated input 'home-manager': 'github:nix-community/home-manager/1e27f21' (2024-10-20) → 'github:nix-community/home-manager/5ec753a' (2024-10-21) • Updated input 'hyprgrass': 'github:horriblename/hyprgrass/1535962' (2024-10-09) → 'github:horriblename/hyprgrass/76063c5' (2024-10-24) • Updated input 'hyprland': 'github:hyprwm/Hyprland/62ee5cc' (2024-10-19) → 'github:hyprwm/Hyprland/cdac649' (2024-10-23) • Updated input 'jovian': 'github:Jovian-Experiments/Jovian-NixOS/bb69165' (2024-10-17) → 'github:Jovian-Experiments/Jovian-NixOS/82bdda1' (2024-10-21) • Updated input 'material-rounded-theme-src': 'github:Nerwyn/material-rounded-theme/40f93bd' (2024-10-10) → 'github:Nerwyn/material-rounded-theme/c722d1e' (2024-10-23) • Updated input 'material-symbols-src': 'github:beecho01/material-symbols/12c2a76' (2024-10-22) → 'github:beecho01/material-symbols/26224d6' (2024-10-23) • Updated input 'nix-gaming': 'github:fufexan/nix-gaming/177e270' (2024-10-20) → 'github:fufexan/nix-gaming/af2e2e0' (2024-10-24) • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/4c2fcb0' (2024-10-18) → 'github:NixOS/nixpkgs/1997e4a' (2024-10-20) • Updated input 'nixpkgs-wayland': 'github:nix-community/nixpkgs-wayland/7c64c3c' (2024-10-20) → 'github:nix-community/nixpkgs-wayland/7832830' (2024-10-23) • Updated input 'sops-nix': 'github:Mic92/sops-nix/c504fd7' (2024-10-20) → 'github:Mic92/sops-nix/d089e74' (2024-10-23) • Updated input 'spotifyplus-src': 'github:thlucas1/homeassistantcomponent_spotifyplus/2cd0654' (2024-10-05) → 'github:thlucas1/homeassistantcomponent_spotifyplus/188f1ec' (2024-10-22) • Updated input 'tuya-local-src': 'github:make-all/tuya-local/38744c0' (2024-10-21) → 'github:make-all/tuya-local/6ebc191' (2024-10-22) Docker Images: • vegardit/gitea-act-runner dind-latest: sha256:74c7cddafcf4c8cadad1317f043cd7ed9f675617197051d38812b262a7afd82b → sha256:456898884eb289ef4590e654804e05b84866279495768bf12bc91451ca160329 • ghcr.io/linuxserver/bazarr latest: sha256:5baf571c177eda579fbaa080ade2135bce9097bc3c165f92d30e399df64d0bc4 → sha256:5e915ab0bdf0aed036f2bce05abcfa85e4fb748df982cdbca1e600a77e02ab24 • ghcr.io/linuxserver/prowlarr latest: sha256:806916104582ff18024b1234a0c82e113c160037f0bf0e6c076e14528bbab331 → sha256:1e2fffd859ecd260e4aae4798cf5be75938cd602161d21b1d58122afb397c864 • ghcr.io/linuxserver/sabnzbd latest: sha256:ab4fc46cb5e2702f640b28bdc9b460c98b73a9898dd61f7068229f140c26409a → sha256:293517b90ef929178387f07c074c1f88d96d94eac3c1d95944ba2891527c1396 Misc Sources: Vuetorrent: 2.14.1 -> 2.15.0
2024-10-23 23:41:16 -04:00
// this is necessary when not putting a cap on clip amount
// exec(`prlimit --pid ${exec('pgrep ags')} --nofile=1024:`);
}
}
private async _decodeItem(index: string): Promise<string | null> {
try {
const decodedItem = await execAsync([
'bash', '-c', `cliphist list | grep -a ${index} | cliphist decode`,
]);
this._decrementClipsLeft();
return decodedItem;
}
catch (error) {
console.error(error);
this._decrementClipsLeft();
return null;
}
}
private _addClip(newClip: [number, { clip: string, isImage: boolean }]) {
if (
![...this.clips.values()]
.map((c) => c.clip)
.includes(newClip[1].clip)
) {
this._clips.set(...newClip);
this.emit('clip-added', newClip);
}
else {
const oldClip = [...this.clips.entries()]
.find(([_, { clip }]) => clip === newClip[1].clip);
if (oldClip && oldClip[0] < newClip[0]) {
this.clips.delete(oldClip[0]);
this._clips.set(...newClip);
this.emit('clip-added', newClip);
}
}
}
private _getHistory(n = Clipboard._MAX_CLIPS) {
chore: update flake.lock Flake Inputs: • Updated input 'astal': 'github:Aylur/astal/004b123' (2024-10-22) → 'github:Aylur/astal/306e649' (2024-10-23) • Updated input 'custom-sidebar-src': 'github:elchininet/custom-sidebar/80942f5' (2024-10-21) → 'github:elchininet/custom-sidebar/5962ac7' (2024-10-24) • Updated input 'discord-overlay': 'github:matt1432/discord-nightly-overlay/256588e' (2024-10-06) → 'github:matt1432/discord-nightly-overlay/e4547ee' (2024-10-23) • Updated input 'gpu-screen-recorder-src': 'git+https://repo.dec05eba.com/gpu-screen-recorder?ref=refs/heads/master&rev=1016597' (2024-10-18) → 'git+https://repo.dec05eba.com/gpu-screen-recorder?ref=refs/heads/master&rev=422f214' (2024-10-22) • Updated input 'gtk-theme-src': 'github:dracula/gtk/e5c95d7' (2024-10-12) → 'github:dracula/gtk/a698492' (2024-10-21) • Updated input 'home-manager': 'github:nix-community/home-manager/1e27f21' (2024-10-20) → 'github:nix-community/home-manager/5ec753a' (2024-10-21) • Updated input 'hyprgrass': 'github:horriblename/hyprgrass/1535962' (2024-10-09) → 'github:horriblename/hyprgrass/76063c5' (2024-10-24) • Updated input 'hyprland': 'github:hyprwm/Hyprland/62ee5cc' (2024-10-19) → 'github:hyprwm/Hyprland/cdac649' (2024-10-23) • Updated input 'jovian': 'github:Jovian-Experiments/Jovian-NixOS/bb69165' (2024-10-17) → 'github:Jovian-Experiments/Jovian-NixOS/82bdda1' (2024-10-21) • Updated input 'material-rounded-theme-src': 'github:Nerwyn/material-rounded-theme/40f93bd' (2024-10-10) → 'github:Nerwyn/material-rounded-theme/c722d1e' (2024-10-23) • Updated input 'material-symbols-src': 'github:beecho01/material-symbols/12c2a76' (2024-10-22) → 'github:beecho01/material-symbols/26224d6' (2024-10-23) • Updated input 'nix-gaming': 'github:fufexan/nix-gaming/177e270' (2024-10-20) → 'github:fufexan/nix-gaming/af2e2e0' (2024-10-24) • Updated input 'nixpkgs': 'github:NixOS/nixpkgs/4c2fcb0' (2024-10-18) → 'github:NixOS/nixpkgs/1997e4a' (2024-10-20) • Updated input 'nixpkgs-wayland': 'github:nix-community/nixpkgs-wayland/7c64c3c' (2024-10-20) → 'github:nix-community/nixpkgs-wayland/7832830' (2024-10-23) • Updated input 'sops-nix': 'github:Mic92/sops-nix/c504fd7' (2024-10-20) → 'github:Mic92/sops-nix/d089e74' (2024-10-23) • Updated input 'spotifyplus-src': 'github:thlucas1/homeassistantcomponent_spotifyplus/2cd0654' (2024-10-05) → 'github:thlucas1/homeassistantcomponent_spotifyplus/188f1ec' (2024-10-22) • Updated input 'tuya-local-src': 'github:make-all/tuya-local/38744c0' (2024-10-21) → 'github:make-all/tuya-local/6ebc191' (2024-10-22) Docker Images: • vegardit/gitea-act-runner dind-latest: sha256:74c7cddafcf4c8cadad1317f043cd7ed9f675617197051d38812b262a7afd82b → sha256:456898884eb289ef4590e654804e05b84866279495768bf12bc91451ca160329 • ghcr.io/linuxserver/bazarr latest: sha256:5baf571c177eda579fbaa080ade2135bce9097bc3c165f92d30e399df64d0bc4 → sha256:5e915ab0bdf0aed036f2bce05abcfa85e4fb748df982cdbca1e600a77e02ab24 • ghcr.io/linuxserver/prowlarr latest: sha256:806916104582ff18024b1234a0c82e113c160037f0bf0e6c076e14528bbab331 → sha256:1e2fffd859ecd260e4aae4798cf5be75938cd602161d21b1d58122afb397c864 • ghcr.io/linuxserver/sabnzbd latest: sha256:ab4fc46cb5e2702f640b28bdc9b460c98b73a9898dd61f7068229f140c26409a → sha256:293517b90ef929178387f07c074c1f88d96d94eac3c1d95944ba2891527c1396 Misc Sources: Vuetorrent: 2.14.1 -> 2.15.0
2024-10-23 23:41:16 -04:00
// this is necessary when not putting a cap on clip amount
// exec(`prlimit --pid ${exec('pgrep ags')} --nofile=10024:`);
// This command comes from '../../clipboard/script.sh'
execAsync('clipboard-manager').then((out) => {
const rawClips = out.split('\n');
this._clips_left = Math.min(rawClips.length - 1, n);
rawClips.forEach(async(clip, i) => {
if (i > n) {
return;
}
if (clip.includes('img')) {
this._decrementClipsLeft();
const newClip: [number, { clip: string, isImage: boolean }] = [
parseInt((clip.match('[0-9]+') ?? [''])[0]),
{
clip,
isImage: true,
},
];
this._addClip(newClip);
}
else {
const decodedClip = await this._decodeItem(clip);
if (decodedClip) {
const newClip: [number, { clip: string, isImage: boolean }] = [
parseInt(clip),
{
clip: decodedClip,
isImage: false,
},
];
this._addClip(newClip);
}
}
});
}).catch((error) => console.error(error));
}
private _watchClipboard() {
subprocess(
['wl-paste', '--watch', 'echo'],
() => {
this._getHistory(1);
},
() => { /**/ },
);
}
}
const clipboard = new Clipboard();
export default clipboard;