From f574116339311f879a1573109becf0120366d7d9 Mon Sep 17 00:00:00 2001 From: John Wright Date: Fri, 17 Jun 2016 17:24:45 +0100 Subject: [PATCH 1/6] Using growl with a CLI ``` growl --help ``` When developing: ``` node ./bin/growl.js --help ``` ... or ... ``` npm link growl --help ``` --- .gitignore | 2 ++ bin/growl.js | 28 ++++++++++++++++++++++++++++ package.json | 8 +++++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100755 bin/growl.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3351ce2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.log +node_modules diff --git a/bin/growl.js b/bin/growl.js new file mode 100755 index 0000000..32f0726 --- /dev/null +++ b/bin/growl.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node + +var growl = require('../lib/growl') + , program = require('commander') + , pkg = require('../package.json'); + +program + .version(pkg.version) + .option('-t --title ', 'Notifcation title') + .option('-a --app <name>', 'Application name') + .option('-s --sticky', 'Whether or not the notication should remain until closed') + .option('-i --image <image>', 'Auto-detects the context:\n\t\t\tpath to an icon sets --iconpath\n\t\t\tpath to an image sets --image\n\t\t\tcapitalized word sets --appIcon\n\t\t\tfilename uses extname as --icon\n\t\t\totherwise treated as --icon') + .option('-p --priority <priority>', 'Priority for the notification (default is 0)') + .option('-e --exec <command>', 'manually specify a shell command instead\n\t\t\tappends message to end of shell command\n\t\t\tor, replaces %s with message\n\t\t\toptionally prepends title (example: title: message)\n\t\t\texamples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"') + .arguments('<message>') + .action(notify) + .parse(process.argv); + +if (program.args.length < 1) program.help(); + +function notify(message) { + var options = {}; + ['title', 'app', 'sticky', 'image', 'priority', 'exec'].forEach(function (option) { + if (program[option]) options[option] = program[option]; + }); + console.log(options, message); + growl(message, options); +} diff --git a/package.json b/package.json index 962c7fa..61a4981 100644 --- a/package.json +++ b/package.json @@ -11,5 +11,11 @@ "url": "git://github.com/tj/node-growl.git" }, "main": "./lib/growl.js", - "license": "MIT" + "bin": { + "growl": "./bin/growl.js" + }, + "license": "MIT", + "dependencies": { + "commander": "^2.9.0" + } } From fa9bb6c9a7c814571615427a4a2bb6725640a139 Mon Sep 17 00:00:00 2001 From: John Wright <johngeorge.wright@gmail.com> Date: Fri, 17 Jun 2016 17:47:24 +0100 Subject: [PATCH 2/6] Removing logging --- bin/growl.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/growl.js b/bin/growl.js index 32f0726..5437a5f 100755 --- a/bin/growl.js +++ b/bin/growl.js @@ -23,6 +23,5 @@ function notify(message) { ['title', 'app', 'sticky', 'image', 'priority', 'exec'].forEach(function (option) { if (program[option]) options[option] = program[option]; }); - console.log(options, message); growl(message, options); } From 9eca24d6b31047dfc51addb481eb16309d4bc246 Mon Sep 17 00:00:00 2001 From: John Wright <johngeorge.wright@gmail.com> Date: Mon, 20 Jun 2016 09:21:16 +0100 Subject: [PATCH 3/6] :art: --- bin/growl.js | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/bin/growl.js b/bin/growl.js index 5437a5f..0c9e12a 100755 --- a/bin/growl.js +++ b/bin/growl.js @@ -2,16 +2,44 @@ var growl = require('../lib/growl') , program = require('commander') - , pkg = require('../package.json'); + , pkg = require('../package.json') + , NEW_LINE = '\n\t\t\t\t'; program .version(pkg.version) - .option('-t --title <title>', 'Notifcation title') - .option('-a --app <name>', 'Application name') - .option('-s --sticky', 'Whether or not the notication should remain until closed') - .option('-i --image <image>', 'Auto-detects the context:\n\t\t\tpath to an icon sets --iconpath\n\t\t\tpath to an image sets --image\n\t\t\tcapitalized word sets --appIcon\n\t\t\tfilename uses extname as --icon\n\t\t\totherwise treated as --icon') - .option('-p --priority <priority>', 'Priority for the notification (default is 0)') - .option('-e --exec <command>', 'manually specify a shell command instead\n\t\t\tappends message to end of shell command\n\t\t\tor, replaces %s with message\n\t\t\toptionally prepends title (example: title: message)\n\t\t\texamples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"') + .option( + '-t --title <title>', + 'Notifcation title' + ) + .option( + '-a --app <name>', + 'Application name' + ) + .option( + '-s --sticky', + 'Whether or not the notication should remain until closed' + ) + .option( + '-i --image <image>', + 'Auto-detects the context:' + NEW_LINE + + 'path to an icon sets --iconpath' + NEW_LINE + + 'path to an image sets --image' + NEW_LINE + + 'capitalized word sets --appIcon' + NEW_LINE + + 'filename uses extname as --icon' + NEW_LINE + + 'otherwise treated as --icon' + ) + .option( + '-p --priority <priority>', + 'Priority for the notification (default is 0)' + ) + .option( + '-e --exec <command>', + 'manually specify a shell command instead' + NEW_LINE + + 'appends message to end of shell command' + NEW_LINE + + 'or, replaces %s with message' + NEW_LINE + + 'optionally prepends title (example: title: message)' + NEW_LINE + + 'examples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"' + ) .arguments('<message>') .action(notify) .parse(process.argv); @@ -19,9 +47,10 @@ program if (program.args.length < 1) program.help(); function notify(message) { - var options = {}; - ['title', 'app', 'sticky', 'image', 'priority', 'exec'].forEach(function (option) { + var keys = ['title', 'app', 'sticky', 'image', 'priority', 'exec']; + var options = keys.reduce(function (options, option) { if (program[option]) options[option] = program[option]; - }); + return options; + }, {}); growl(message, options); } From e033c6e0407c9340c1c3b522f00485c80fbe95d9 Mon Sep 17 00:00:00 2001 From: John Wright <johngeorge.wright@gmail.com> Date: Mon, 20 Jun 2016 09:26:29 +0100 Subject: [PATCH 4/6] Spacing --- bin/growl.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/bin/growl.js b/bin/growl.js index 0c9e12a..21f4c95 100755 --- a/bin/growl.js +++ b/bin/growl.js @@ -3,7 +3,7 @@ var growl = require('../lib/growl') , program = require('commander') , pkg = require('../package.json') - , NEW_LINE = '\n\t\t\t\t'; + , NEW_LINE = '\n '; program .version(pkg.version) @@ -22,11 +22,11 @@ program .option( '-i --image <image>', 'Auto-detects the context:' + NEW_LINE + - 'path to an icon sets --iconpath' + NEW_LINE + - 'path to an image sets --image' + NEW_LINE + - 'capitalized word sets --appIcon' + NEW_LINE + - 'filename uses extname as --icon' + NEW_LINE + - 'otherwise treated as --icon' + '- path to an icon sets --iconpath' + NEW_LINE + + '- path to an image sets --image' + NEW_LINE + + '- capitalized word sets --appIcon' + NEW_LINE + + '- filename uses extname as --icon' + NEW_LINE + + '- otherwise treated as --icon' ) .option( '-p --priority <priority>', @@ -34,11 +34,11 @@ program ) .option( '-e --exec <command>', - 'manually specify a shell command instead' + NEW_LINE + - 'appends message to end of shell command' + NEW_LINE + - 'or, replaces %s with message' + NEW_LINE + - 'optionally prepends title (example: title: message)' + NEW_LINE + - 'examples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"' + 'Manually specify a shell command instead' + NEW_LINE + + '- appends message to end of shell command' + NEW_LINE + + '- or, replaces %s with message' + NEW_LINE + + '- optionally prepends title (example: title: message)' + NEW_LINE + + '- examples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"' ) .arguments('<message>') .action(notify) From 1b164487ded9bb57cd892628dd0614f95fee358f Mon Sep 17 00:00:00 2001 From: John Wright <johngeorge.wright@gmail.com> Date: Mon, 20 Jun 2016 09:49:37 +0100 Subject: [PATCH 5/6] Progmatically picking CLI options --- bin/growl.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/growl.js b/bin/growl.js index 21f4c95..f2be68f 100755 --- a/bin/growl.js +++ b/bin/growl.js @@ -47,9 +47,9 @@ program if (program.args.length < 1) program.help(); function notify(message) { - var keys = ['title', 'app', 'sticky', 'image', 'priority', 'exec']; - var options = keys.reduce(function (options, option) { - if (program[option]) options[option] = program[option]; + var options = program.options.reduce(function (options, option) { + var name = option.name(); + if (program[name]) options[name] = program[name]; return options; }, {}); growl(message, options); From 3b7d8748c1163b64ed0fc411ee9cb1476d8fd6f4 Mon Sep 17 00:00:00 2001 From: John Wright <johngeorge.wright@gmail.com> Date: Mon, 20 Jun 2016 10:01:11 +0100 Subject: [PATCH 6/6] Separating lines function --- bin/growl.js | 56 +++++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/bin/growl.js b/bin/growl.js index f2be68f..df6a4bc 100755 --- a/bin/growl.js +++ b/bin/growl.js @@ -7,39 +7,25 @@ var growl = require('../lib/growl') program .version(pkg.version) - .option( - '-t --title <title>', - 'Notifcation title' - ) - .option( - '-a --app <name>', - 'Application name' - ) - .option( - '-s --sticky', - 'Whether or not the notication should remain until closed' - ) - .option( - '-i --image <image>', - 'Auto-detects the context:' + NEW_LINE + - '- path to an icon sets --iconpath' + NEW_LINE + - '- path to an image sets --image' + NEW_LINE + - '- capitalized word sets --appIcon' + NEW_LINE + - '- filename uses extname as --icon' + NEW_LINE + - '- otherwise treated as --icon' - ) - .option( - '-p --priority <priority>', - 'Priority for the notification (default is 0)' - ) - .option( - '-e --exec <command>', - 'Manually specify a shell command instead' + NEW_LINE + - '- appends message to end of shell command' + NEW_LINE + - '- or, replaces %s with message' + NEW_LINE + - '- optionally prepends title (example: title: message)' + NEW_LINE + - '- examples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"' - ) + .option('-t --title <title>', 'Notifcation title') + .option('-a --app <name>', 'Application name') + .option('-s --sticky', 'Whether or not the notication should remain until closed') + .option('-i --image <image>', lines( + 'Auto-detects the context:' + , '- path to an icon sets --iconpath' + , '- path to an image sets --image' + , '- capitalized word sets --appIcon' + , '- filename uses extname as --icon' + , '- otherwise treated as --icon' + )) + .option('-p --priority <priority>', 'Priority for the notification (default is 0)') + .option('-e --exec <command>', lines( + 'Manually specify a shell command instead' + , '- appends message to end of shell command' + , '- or, replaces %s with message' + , '- optionally prepends title (example: title: message)' + , '- examples: --exec "tmux display-message" --exec "echo \'%s\' > messages.log"' + )) .arguments('<message>') .action(notify) .parse(process.argv); @@ -54,3 +40,7 @@ function notify(message) { }, {}); growl(message, options); } + +function lines() { + return Array.prototype.join.call(arguments, NEW_LINE); +}