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

Adding support for actions for notify-send version >=0.8.2 #445

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,25 @@ See full usage on the [project homepage: **`notifu`**](http://www.paralint.com/p

### Usage: `NotifySend`

**Note:** `notify-send` doesn't support the `wait` flag.
**Note:** `notify-send` <0.8.2 doesn't support the `wait` flag.

**Note:** `notify-send` >=0.8.2 supports `action` and `wait` flags.

```javascript
const NotifySend = require('node-notifier').NotifySend;

var notifier = new NotifySend();

notifier.on('ok', () => {
console.log('"OK" was pressed');
});
notifier.on('cancel', () => {
console.log('"Cancel" was pressed');
});
notifier.on('activate', () => {
console.log('notification was clicked');
});

notifier.notify({
title: 'Foo',
message: 'Hello World',
Expand All @@ -363,12 +375,15 @@ notifier.notify({
'app-name': 'node-notifier',
urgency: undefined,
category: undefined,
hint: undefined
hint: undefined,
actions: ['OK', 'Cancel'] // Name of action in lowercase will be used as event name; implicitly adds '--wait' as well.
});
```

See flags and options on the man page [`notify-send(1)`](http://manpages.ubuntu.com/manpages/gutsy/man1/notify-send.1.html)

Run `example/notify-send.js` to see handling of action response. **You must run this example from a real terminal, it doesn't work from inside e.g. VSCode since libnotify will be in `confined` mode then.**

## Thanks to OSS

`node-notifier` is made possible through Open Source Software.
Expand Down
84 changes: 84 additions & 0 deletions example/notify-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const notifier = require('../index');
const path = require('path');

notifier.on('activate', function (notifierObject, options, event) {
console.log(
'clicked:',
JSON.stringify(notifierObject, null, 2),
JSON.stringify(options, null, 2),
JSON.stringify(event, null, 2)
);
});

notifier.on('timeout', function (notifierObject, options) {
// does not work for notify-send
console.log(
'timeout:',
JSON.stringify(notifierObject, null, 2),
JSON.stringify(options, null, 2)
);
});

notifier.on('yes', (nn, options, x) => {
console.log(
'YES',
JSON.stringify(options, null, 2),
JSON.stringify(x, null, 2)
);
});
notifier.on('no', (nn, options) => {
console.log('NO', JSON.stringify(options, null, 2));
});
notifier.on('ok', (nn, options) => {
console.log('OK', JSON.stringify(options, null, 2));
});

const testNotificationWithoutActions = () => {
notifier.notify(
{
title: 'My awesome title',
message: 'Hello from node, Mr. User!',
icon: path.resolve(path.join(__dirname, 'coulson.jpg')), // Absolute path (doesn't work on balloons)
sound: true, // Only Notification Center or Windows Toasters
wait: true // Wait with callback, until user action is taken against notification, does not apply to Windows Toasters as they always wait or notify-send as it does not support the wait option
// actions: ['OK']
},
function (err, response, metadata) {
// Response (is response from notification
// Metadata contains activationType, activationAt, deliveredAt
console.log(
'cb:',
JSON.stringify(err, null, 2),
JSON.stringify(response, null, 2),
JSON.stringify(metadata, null, 2)
);
}
);
};

const testNotificationWithActions = () => {
notifier.notify(
{
title: 'My awesome title',
message: 'Hello from node, Mr. User!',
icon: path.resolve(path.join(__dirname, 'coulson.jpg')), // Absolute path (doesn't work on balloons)
sound: true, // Only Notification Center or Windows Toasters
actions: ['Yes', 'No']
},
function (err, response, metadata) {
// Response is response from notification
// Metadata contains activationType, activationAt, deliveredAt
console.log(
'cb:',
JSON.stringify(err, null, 2),
JSON.stringify(response, null, 2),
JSON.stringify(metadata, null, 2)
);
}
);
};

if (require.main === module) {
setTimeout(testNotificationWithActions, 10);
setTimeout(testNotificationWithoutActions, 10000);
}
33 changes: 23 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ const notifySendFlags = {
h: 'hint',
hint: 'hint',
a: 'app-name',
'app-name': 'app-name'
'app-name': 'app-name',
action: 'action',
A: 'action',
actions: 'action',
w: 'wait',
wait: 'wait'
};

module.exports.command = function (notifier, options, cb) {
Expand All @@ -56,14 +61,13 @@ module.exports.command = function (notifier, options, cb) {
console.info('[notifier options]', options.join(' '));
}

return cp.exec(notifier + ' ' + options.join(' '), function (
error,
stdout,
stderr
) {
if (error) return cb(error);
cb(stderr, stdout);
});
return cp.exec(
notifier + ' ' + options.join(' '),
function (error, stdout, stderr) {
if (error) return cb(error);
cb(stderr, stdout);
}
);
};

module.exports.fileCommand = function (notifier, options, cb) {
Expand Down Expand Up @@ -297,6 +301,7 @@ module.exports.constructArgumentList = function (options, extra) {
const explicitTrue = !!extra.explicitTrue;
const keepNewlines = !!extra.keepNewlines;
const wrapper = extra.wrapper === undefined ? '"' : extra.wrapper;
const arrayArgToMultipleArgs = extra.arrayArgToMultipleArgs || false;

const escapeFn = function escapeFn(arg) {
if (isArray(arg)) {
Expand All @@ -323,7 +328,13 @@ module.exports.constructArgumentList = function (options, extra) {
if (explicitTrue && options[key] === true) {
args.push('-' + keyExtra + key);
} else if (explicitTrue && options[key] === false) continue;
else args.push('-' + keyExtra + key, escapeFn(options[key]));
else {
if (arrayArgToMultipleArgs && isArray(options[key])) {
for (const val of options[key]) {
args.push('-' + keyExtra + key, escapeFn(val));
}
} else args.push('-' + keyExtra + key, escapeFn(options[key]));
}
}
}
return args;
Expand Down Expand Up @@ -539,6 +550,8 @@ function garanteeSemverFormat(version) {
return version;
}

module.exports.garanteeSemverFormat = garanteeSemverFormat;

function sanitizeNotifuTypeArgument(type) {
if (typeof type === 'string' || type instanceof String) {
if (type.toLowerCase() === 'info') return 'info';
Expand Down
Loading