Skip to content

Instantly share code, notes, and snippets.

@enko
Created April 18, 2019 12:41
Show Gist options
  • Save enko/263c00b1772ac919ef717c5a05aad71b to your computer and use it in GitHub Desktop.
Save enko/263c00b1772ac919ef717c5a05aad71b to your computer and use it in GitHub Desktop.
#!/usr/bin/env -S deno --allow-run
import { decode } from "https://deno.land/std/strings/strings.ts";
import * as log from "https://deno.land/std/log/mod.ts";
function baseRandom(lower, upper) {
return lower + Math.floor(Math.random() * (upper - lower + 1));
}
async function main() {
// create subprocess
const p = Deno.run({
args: [
"swaymsg",
"-r",
"-t",
'get_outputs',
],
stdout: "piped",
stderr: "piped",
});
const outputs = JSON.parse(decode(await p.output()));
if (!Array.isArray(outputs)) {
console.log('outputs is no array');
Deno.exit(1);
}
log.debug(`Detected ${outputs.length} outputs`);
const bgProcess = Deno.run({
args: [
'find',
'/home/tschumacher/datenknoten/cloud/bg',
'-type',
'f',
],
stdout: "piped",
stderr: "piped",
});
const images = decode(await bgProcess.output()).split('\n');
log.debug(`Detected ${images.length} backgrounds`)
for (const output of outputs) {
const bg = images[baseRandom(0, images.length - 1)]
const outputDevice = `"${output.make} ${output.model} ${output.serial}"`
log.info(`Changing background to ${bg} on output ${outputDevice}`);
const bgP = Deno.run({
args: [
'swaymsg',
'output',
outputDevice,
'bg',
bg,
'fill',
],
stdout: "piped",
stderr: "piped",
});
await bgP.status();
}
Deno.exit(0);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment