feat(ags clipboard): add wl-paste --watch
All checks were successful
Discord / discord commits (push) Has been skipped
All checks were successful
Discord / discord commits (push) Has been skipped
This commit is contained in:
parent
0a5c8d03bd
commit
aa979f0fe4
2 changed files with 74 additions and 45 deletions
|
@ -1,27 +1,42 @@
|
||||||
const { execAsync } = Utils;
|
const { execAsync, subprocess } = Utils;
|
||||||
|
|
||||||
|
|
||||||
// TODO: add wl-paste --watch to not have to get history every time
|
|
||||||
const MAX_CLIPS = 100;
|
|
||||||
|
|
||||||
class Clipboard extends Service {
|
class Clipboard extends Service {
|
||||||
static {
|
static {
|
||||||
Service.register(this, {
|
Service.register(this, {
|
||||||
'clip-added': ['string'],
|
'clip-added': ['jsobject'],
|
||||||
'history-searched': [],
|
'history-searched': [],
|
||||||
}, {
|
}, {
|
||||||
clips: ['jsobject'],
|
clips: ['jsobject'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static _MAX_CLIPS = 100;
|
||||||
|
|
||||||
|
// Class Attributes
|
||||||
private _clips_left = 0;
|
private _clips_left = 0;
|
||||||
|
|
||||||
private _clips: Map<number, { clip: string; isImage: boolean }> = new Map();
|
private _clips: Map<number, { clip: string; isImage: boolean }> = new Map();
|
||||||
|
|
||||||
get clips() {
|
get clips() {
|
||||||
return this._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() {
|
private _decrementClipsLeft() {
|
||||||
if (--this._clips_left === 0) {
|
if (--this._clips_left === 0) {
|
||||||
this.emit('history-searched');
|
this.emit('history-searched');
|
||||||
|
@ -48,56 +63,65 @@ class Clipboard extends Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public copyOldItem(key: string | number): void {
|
private _getHistory(n = Clipboard._MAX_CLIPS) {
|
||||||
execAsync([
|
|
||||||
'bash', '-c', `cliphist list | grep ${key} | cliphist decode | wl-copy`,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getHistory() {
|
|
||||||
this._clips = new Map();
|
this._clips = new Map();
|
||||||
|
|
||||||
// FIXME: this is necessary when not putting a cap on clip amount
|
// FIXME: this is necessary when not putting a cap on clip amount
|
||||||
// exec(`prlimit --pid ${exec('pgrep ags')} --nofile=10024:`);
|
// exec(`prlimit --pid ${exec('pgrep ags')} --nofile=10024:`);
|
||||||
|
|
||||||
// This command comes from '../../clipboard/script.sh'
|
// This command comes from '../../clipboard/script.sh'
|
||||||
execAsync('clipboard-manager')
|
execAsync('clipboard-manager').then((out) => {
|
||||||
.then((out) => {
|
|
||||||
const rawClips = out.split('\n');
|
const rawClips = out.split('\n');
|
||||||
|
|
||||||
this._clips_left = Math.min(rawClips.length - 1, MAX_CLIPS);
|
this._clips_left = Math.min(rawClips.length - 1, n);
|
||||||
|
|
||||||
rawClips.forEach(async (clip, i) => {
|
rawClips.forEach(async (clip, i) => {
|
||||||
if (i > MAX_CLIPS) {
|
if (i > n) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clip.includes('img')) {
|
if (clip.includes('img')) {
|
||||||
this._decrementClipsLeft();
|
this._decrementClipsLeft();
|
||||||
this._clips.set(
|
|
||||||
|
const newClip: [number, {clip: string; isImage: boolean;}] = [
|
||||||
parseInt((clip.match('[0-9]+') ?? [''])[0]),
|
parseInt((clip.match('[0-9]+') ?? [''])[0]),
|
||||||
{
|
{
|
||||||
clip,
|
clip,
|
||||||
isImage: true,
|
isImage: true,
|
||||||
},
|
},
|
||||||
);
|
];
|
||||||
|
|
||||||
|
this._clips.set(...newClip);
|
||||||
|
this.emit('clip-added', newClip);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const decodedClip = await this._decodeItem(clip);
|
const decodedClip = await this._decodeItem(clip);
|
||||||
|
|
||||||
if (decodedClip) {
|
if (decodedClip) {
|
||||||
this._clips.set(
|
const newClip: [number, {clip: string; isImage: boolean;}] = [
|
||||||
parseInt(clip),
|
parseInt(clip),
|
||||||
{
|
{
|
||||||
clip: decodedClip,
|
clip: decodedClip,
|
||||||
isImage: false,
|
isImage: false,
|
||||||
},
|
},
|
||||||
);
|
];
|
||||||
|
|
||||||
|
this._clips.set(...newClip);
|
||||||
|
this.emit('clip-added', newClip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
}).catch((error) => console.error(error));
|
||||||
.catch((error) => console.error(error));
|
}
|
||||||
|
|
||||||
|
private _watchClipboard() {
|
||||||
|
subprocess(
|
||||||
|
['wl-paste', '--watch', 'echo'],
|
||||||
|
() => {
|
||||||
|
this._getHistory(1);
|
||||||
|
},
|
||||||
|
() => {/**/},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,16 +60,21 @@ export default () => {
|
||||||
App.closeWindow('win-clipboard');
|
App.closeWindow('win-clipboard');
|
||||||
},
|
},
|
||||||
|
|
||||||
init_rows: (list) => {
|
setup_list: (list) => {
|
||||||
Clipboard.getHistory();
|
|
||||||
|
|
||||||
const CONNECT_ID = Clipboard.connect('history-searched', () => {
|
const CONNECT_ID = Clipboard.connect('history-searched', () => {
|
||||||
|
// Do every clip that existed before this widget
|
||||||
list.get_children().forEach((row) => {
|
list.get_children().forEach((row) => {
|
||||||
row.destroy();
|
row.destroy();
|
||||||
});
|
});
|
||||||
Clipboard.clips.forEach((clip, key) => {
|
Clipboard.clips.forEach((clip, key) => {
|
||||||
makeItem(list, key, clip.clip, clip.isImage);
|
makeItem(list, key, clip.clip, clip.isImage);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Setup connection for new clips
|
||||||
|
Clipboard.connect('clip-added', (_, [key, clip]) => {
|
||||||
|
makeItem(list, key, clip.clip, clip.isImage);
|
||||||
|
});
|
||||||
|
|
||||||
Clipboard.disconnect(CONNECT_ID);
|
Clipboard.disconnect(CONNECT_ID);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue