Skip to content

Instantly share code, notes, and snippets.

@Buslowicz
Last active October 4, 2017 09:15
Show Gist options
  • Save Buslowicz/62cc3331cf6f2fded55c23a51e21fbf7 to your computer and use it in GitHub Desktop.
Save Buslowicz/62cc3331cf6f2fded55c23a51e21fbf7 to your computer and use it in GitHub Desktop.
Generates a CUID and copies it to clipboard.
const St = imports.gi.St;
const Main = imports.ui.main;
const Shell = imports.gi.Shell;
const Clipboard = St.Clipboard.get_default();
const CLIPBOARD_TYPE = St.ClipboardType.CLIPBOARD;
const namespace = 'cuid';
const blockSize = 4;
const base = 36;
const discreteValues = Math.pow(base, blockSize);
let c = 0;
let button;
function init() {
button = new St.Bin({
style_class: 'panel-button',
reactive: true,
can_focus: true,
x_fill: true,
y_fill: false,
track_hover: true
});
let icon = new St.Icon({ icon_name: 'preferences-system-privacy-symbolic',
style_class: 'system-status-icon' });
button.set_child(icon);
button.connect('button-press-event', function() {
Clipboard.set_text(CLIPBOARD_TYPE, cuid());
});
}
function enable() {
Main.panel._rightBox.insert_child_at_index(button, 0);
}
function disable() {
Main.panel._rightBox.remove_child(button);
}
function pad(num, size) {
const s = "000000000" + num;
return s.substr(s.length-size);
}
function randomBlock() {
return pad((Math.random() * discreteValues << 0).toString(base), blockSize);
}
function safeCounter() {
c = (c < discreteValues) ? c : 0;
c++;
return c - 1;
}
function cuid() {
const letter = 'c';
const timestamp = (new Date().getTime()).toString(base);
const fingerprint = nodePrint();
const random = randomBlock() + randomBlock();
const counter = pad(safeCounter().toString(base), blockSize);
return (letter + timestamp + counter + fingerprint + random);
}
function nodePrint() {
const padding = 2;
const hostname = 'x1';
const length = hostname.length;
const pid = pad((21396).toString(36), 2);
const hostId = pad(
hostname
.split('')
.reduce(function (prev, char) {
return +prev + char.charCodeAt(0);
}, +length + 36)
.toString(36),
2
);
return pid + hostId;
}
{
"description": "Generates a CUID and copy it to clipboard",
"name": "CUID Generator",
"shell-version": [
"3.24.2"
],
"url": "https://gist.github.com/Draccoz/62cc3331cf6f2fded55c23a51e21fbf7",
"uuid": "cuid-generator@dbsoft.com.pl",
"version": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment