New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for nitrogen on linux #17
Conversation
This is popular among people who use tiling window managers (specially i3 users)
Is there any way to get the wallpaper with |
Not that I know of, besides reading the config file. A simple way to do that would be, grep -m 1 -oP '(?<=file=).*' ~/.config/nitrogen/bg-saved.cfg Or, if you want more portability, sed -n 's/^file=//p' ~/.config/nitrogen/bg-saved.cfg | tail -n 1 ^ the |
Do you have any suggestions on how a different command can be used to get values? With the current API, that doesn't seem to be possible. |
You could add manual workaround here Line 126 in 72f0843 app is nitrogen.
|
ping :) |
totally forgot about it, thanks for the ping. gonna make the commit asap 😄 |
Temporary workaround for #18
I think now you can merge it. |
lib/linux.js
Outdated
@@ -32,6 +44,12 @@ const appsList = [ | |||
set: ['--bg-scale', '%s'] | |||
}, | |||
{ | |||
cmd: 'nitrogen', | |||
set: ['--set-zoom-fill', '--save', '%s'], | |||
get: ['-n', 's/^file=//p', `${process.env.HOME}/.config/nitrogen/bg-saved.cfg`], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a long flag for -n
? If yes, use that instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use require('os').homedir()
instead of the environment variable. Should also use path.join
.
lib/linux.js
Outdated
@@ -124,6 +130,10 @@ exports.get = function get() { | |||
|
|||
const app = availableApps.find(app => app.get); | |||
|
|||
if (/nitrogen/.test(app.cmd)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (app.cmd === 'nitrogen') {
@sindresorhus I will fix those as soon as I get some time. |
👍 |
lib/linux.js
Outdated
@@ -159,5 +173,13 @@ exports.set = function set(imagePath) { | |||
|
|||
params[params.length - 1] = params[params.length - 1].replace('%s', path.resolve(imagePath)); | |||
|
|||
if (app.cmd === 'gsettings') { | |||
isCinnamon().then(cinnamon => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work. This check is async, but you're assigning to the outside scope which will be done by the time this is executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing it so it doesn't assign to the outside scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so checking it synchronously shouldn't be a problem right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exported method is async, so it shouldn't contain any synchronous calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm out of ideas on how to do it properly 😟
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just promises. You can do anything you would do synchronously. You just need to chain the isCinnamon()
before cp.execFile
and return the whole chain.
lib/linux.js
Outdated
@@ -123,15 +113,39 @@ function setAvailableApps() { | |||
}); | |||
} | |||
|
|||
function isCinnamon() { | |||
const args = ['writable', 'org.cinnamon.desktop.background', 'picture-uri']; | |||
// output = childProcess.execFile('gsettings', args, ()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to maintain line length :P or are you talking about the comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment. Don't leave commented out code in there. That's what git is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah sorry about that, i thought i deleted the comments
lib/linux.js
Outdated
return cp.execFile('gsettings', args) | ||
.then(out => (out.toString().trim() === 'true')) | ||
.catch(err => { | ||
err.toString(); // do nothing, this error is expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should simply be:
.catch(() => {});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i cant reproduce rn, but i think your linter complains about that if there's nothing caught
lib/linux.js
Outdated
const args = ['writable', 'org.cinnamon.desktop.background', 'picture-uri']; | ||
// output = childProcess.execFile('gsettings', args, ()); | ||
return cp.execFile('gsettings', args) | ||
.then(out => (out.toString().trim() === 'true')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.then(out => out.trim() === 'true')
lib/linux.js
Outdated
if (/nitrogen/.test(app.cmd)) { | ||
app.cmd = '/bin/sed'; | ||
if (app.cmd === 'nitrogen') { | ||
const homeDir = require('os').homedir(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't require inline. require
should be at the top of the file.
lib/linux.js
Outdated
const homeDir = require('os').homedir(); | ||
const configFile = path.join(homeDir, '.config/nitrogen/bg-saved.cfg'); | ||
return fs.readFile(configFile, 'utf8').then(config => { | ||
config = config.toString().trim().split('\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.toString()
is moot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, will fix
lib/linux.js
Outdated
config = config.toString().trim().split('\n'); | ||
const wallpaper = config.find(line => /file=/.test(line)); | ||
return wallpaper.replace('file=', ''); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could simply be:
return fs.readFile(configFile, 'utf8').then(config =>
config.trim().split('\n').find(line => /file=/.test(line)).replace('file=', '')
);
I don't know what the format is like, but I assume you want to remove file=
from the start of the line, so it should be /^file=/
. Same with replace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the config file has multiple lines, I just want the one with file=
.
there can be whitespace leading file
, so ^file
would be invalid in that situation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want the one with file=.
That's already covered by the line splitting and .find
.
here can be whitespace leading file
.trim()
it before checking then. file=
is too loose as if the filename was foofile=bar.jpg
it would match. Quite unlikely, but I rather be safe.
really sorry about this, should've never happened
I think all the changes have been made, please re-check |
You need to fix the promise chaining for the |
@sindresorhus should be okay now. Couldn't test it myself this time. |
BUMP! |
@fa7ad I'm not super confident in the changes, especially since you haven't tested it, but I'll try to give it a thorough review. |
@sindresorhus yeah sorry about that. I don't use GNOME or Cinnamon anymore. So, I'm not able to physically test it. Maybe someone who uses those systems can help out. But the changes are minimal and should work. Do give it a review though 😄 |
This is popular among people who use tiling window managers (specially i3 users)