Skip to content

Commit

Permalink
Add support for nitrogen on linux (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
fa7ad authored and sindresorhus committed Jun 8, 2017
1 parent 6f1b164 commit d12f547
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions lib/linux.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';
const path = require('path');
const homeDir = require('os').homedir();
const childProcess = require('child_process');
const fileSystem = require('fs');
const pify = require('pify');

const cp = pify(childProcess);
const fs = pify(fileSystem);

const appsList = [
{
Expand Down Expand Up @@ -31,6 +34,12 @@ const appsList = [
cmd: 'feh',
set: ['--bg-scale', '%s']
},
{
cmd: 'nitrogen',
set: ['--set-zoom-fill', '--save', '%s'],
get: [],
transform: imagePath => imagePath.trim().split('\n').slice(-1)[0]
},
{
cmd: 'xfconf-query',
set: ['-c xfce4-desktop',
Expand Down Expand Up @@ -63,18 +72,6 @@ const appsList = [
'/org/mate/desktop/background/picture-filename'
],
transform: imagePath => imagePath.slice(1, -1)
},
{
cmd: 'gsettings',
set: ['set',
'org.cinnamon.desktop.background',
'picture-uri',
'file://%s'
],
get: ['get',
'org.cinnamon.desktop.background',
'picture-uri'
]
}
];

Expand All @@ -100,12 +97,12 @@ function setAvailableApps() {
throw new Error('None of the apps were found');
}

// it may return aliases so we only want the real path
// It may return aliases so we only want the real path
// and only the executable name. i.e. 'foo' from /bin/foo
stdout = stdout.split('\n');

stdout.forEach(el => {
// it's an alias
// It's an alias
if (el[0] !== path.sep) {
return;
}
Expand All @@ -117,13 +114,46 @@ function setAvailableApps() {
});
}

function isCinnamon() {
const args = ['writable', 'org.cinnamon.desktop.background', 'picture-uri'];

return cp.execFile('gsettings', args)
.then(out => out.trim() === 'true')
.catch(() => {});
}

exports.get = function get() {
if (!availableApps) {
return setAvailableApps().then(get);
}

const app = availableApps.find(app => app.get);

if (app.cmd === 'nitrogen') {
const configFile = path.join(homeDir, '.config/nitrogen/bg-saved.cfg');
return fs.readFile(configFile, 'utf8').then(config =>
config.trim().split('\n').find(line => line.startsWith('file=')).replace('file=', '')
);
}

if (app.cmd === 'gsettings') {
return isCinnamon().then(cinnamon => {
const getArgs = app.get.slice();
if (cinnamon) {
getArgs[1] = 'org.cinnamon.desktop.background';
}
return cp.execFile(app.cmd, getArgs).then(stdout => {
stdout = stdout.trim();

if (typeof app.transform === 'function') {
return app.transform(stdout);
}

return stdout;
});
});
}

return cp.execFile(app.cmd, app.get).then(stdout => {
stdout = stdout.trim();

Expand All @@ -149,5 +179,14 @@ exports.set = function set(imagePath) {

params[params.length - 1] = params[params.length - 1].replace('%s', path.resolve(imagePath));

if (app.cmd === 'gsettings') {
return isCinnamon().then(cinnamon => {
if (cinnamon) {
params[1] = 'org.cinnamon.desktop.background';
}
return cp.execFile(app.cmd, params);
});
}

return cp.execFile(app.cmd, params);
};

0 comments on commit d12f547

Please sign in to comment.