Skip to content
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

Merged
merged 11 commits into from Jun 8, 2017
Merged

Add support for nitrogen on linux #17

merged 11 commits into from Jun 8, 2017

Conversation

fa7ad
Copy link
Contributor

@fa7ad fa7ad commented Sep 1, 2016

This is popular among people who use tiling window managers (specially i3 users)

This is popular among people who use tiling window managers (specially i3 users)
@sindresorhus
Copy link
Owner

Is there any way to get the wallpaper with nitrogen?

@fa7ad
Copy link
Contributor Author

fa7ad commented Sep 2, 2016

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 tail bit and -m 1 is there to make sure it returns only one file name. Nitrogen can be launched from different environments and the configs are stored separately. But for most use cases, the last match is the last used settings and is likely what the user is looking for. but, I think these transformations are better handled in JS. no idea how to implement it though.

@fa7ad
Copy link
Contributor Author

fa7ad commented Sep 6, 2016

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.

@sindresorhus
Copy link
Owner

You could add manual workaround here

if app is nitrogen.

@sindresorhus
Copy link
Owner

ping :)

@fa7ad
Copy link
Contributor Author

fa7ad commented Oct 22, 2016

totally forgot about it, thanks for the ping. gonna make the commit asap 😄

Fahad Hossain added 2 commits October 23, 2016 04:43
@fa7ad
Copy link
Contributor Author

fa7ad commented Oct 23, 2016

I think now you can merge it.

@fa7ad fa7ad closed this Oct 23, 2016
@fa7ad fa7ad reopened this Oct 23, 2016
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`],
Copy link
Owner

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.

Copy link
Owner

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)) {
Copy link
Owner

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
Copy link
Owner

Can you look into the feedback from @Sohail05?

@fa7ad
Copy link
Contributor Author

fa7ad commented Nov 4, 2016

@sindresorhus I will fix those as soon as I get some time.
FWIW, I think this solution is very fragile. I'll try to re-iterate and maybe use node's fs module for get instead of relying on sed.

@sindresorhus
Copy link
Owner

I'll try to re-iterate and maybe use node's fs module for get instead of relying on sed.

👍

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 => {
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you suggest?

Copy link
Owner

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.

Copy link
Contributor Author

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?

Copy link
Owner

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.

Copy link
Contributor Author

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 😟

Copy link
Owner

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, ());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

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?

Copy link
Owner

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.

Copy link
Contributor Author

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Owner

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(() => {});

Copy link
Contributor Author

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'))
Copy link
Owner

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();
Copy link
Owner

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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toString() is moot.

Copy link
Contributor Author

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=', '');
});
Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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.

Fahad Hossain added 3 commits December 6, 2016 15:07
really sorry about this, should've never happened
@fa7ad
Copy link
Contributor Author

fa7ad commented Jan 15, 2017

I think all the changes have been made, please re-check

@fa7ad fa7ad mentioned this pull request Jan 15, 2017
@sindresorhus
Copy link
Owner

You need to fix the promise chaining for the set function too.

@fa7ad
Copy link
Contributor Author

fa7ad commented Mar 18, 2017

@sindresorhus should be okay now. Couldn't test it myself this time.

@fa7ad
Copy link
Contributor Author

fa7ad commented May 6, 2017

BUMP!

@sindresorhus
Copy link
Owner

@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.

@fa7ad
Copy link
Contributor Author

fa7ad commented May 6, 2017

@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 😄

@fa7ad fa7ad mentioned this pull request Jun 2, 2017
3 tasks
@sindresorhus sindresorhus merged commit d12f547 into sindresorhus:master Jun 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants