From abd7cfb714577f7fc7be514b9bd762e9bffb2b5d Mon Sep 17 00:00:00 2001 From: jash Date: Wed, 24 Oct 2018 00:43:20 +0200 Subject: [PATCH] added small help text system with simple formatting --- .../generators/app/help.json | 36 ++++++++++++++++ .../generators/app/index.js | 21 ++++++--- .../generators/app/lib/html2ansi.js | 43 +++++++++++++++++++ install/generator-cyphernode/package.json | 1 + 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 install/generator-cyphernode/generators/app/help.json create mode 100644 install/generator-cyphernode/generators/app/lib/html2ansi.js diff --git a/install/generator-cyphernode/generators/app/help.json b/install/generator-cyphernode/generators/app/help.json new file mode 100644 index 0000000..a2ece9f --- /dev/null +++ b/install/generator-cyphernode/generators/app/help.json @@ -0,0 +1,36 @@ +{ + "features": "", + "net": "", + "username": "", + "xpub": "", + "derivation_path": "", + "gatekeeper_clientkeyspassword": "", + "gatekeeper_recreatekeys": "", + "gatekeeper_edit_ipwhitelist": "", + "gatekeeper_ipwhitelist": "", + "gatekeeper_edit_apiproperties": "", + "gatekeeper_apiproperties": "", + "bitcoin_mode": "", + "bitcoin_node_ip": "", + "bitcoin_rpcuser": "", + "bitcoin_rpcpassword": "", + "bitcoin_prune": "", + "bitcoin_uacomment": "", + "lightning_implementation": "", + "lightning_external_ip": "", + "lightning_nodename": "", + "lightning_nodecolor": "", + "electrum_implementation": "", + "proxy_datapath": "", + "bitcoin_datapath": "", + "lightning_datapath": "", + "bitcoin_expose": "", + "docker_mode": "", + "__default__": "Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam" +} + + + + + + \ No newline at end of file diff --git a/install/generator-cyphernode/generators/app/index.js b/install/generator-cyphernode/generators/app/index.js index 49a5fc6..622b837 100644 --- a/install/generator-cyphernode/generators/app/index.js +++ b/install/generator-cyphernode/generators/app/index.js @@ -1,18 +1,18 @@ 'use strict'; const Generator = require('yeoman-generator'); const chalk = require('chalk'); +const wrap = require('wrap-ansi'); +const html2ansi = require('./lib/html2ansi.js'); const fs = require('fs'); const validator = require('validator'); const path = require("path"); -const featureChoices = require(path.join(__dirname, "features.json")); const coinstring = require('coinstring'); const Archive = require('./lib/archive.js'); const ApiKey = require('./lib/apikey.js'); -const help = require('./lib/help.js'); +const featureChoices = require('./features.json'); const uaCommentRegexp = /^[a-zA-Z0-9 \.,:_\-\?\/@]+$/; // TODO: look for spec of unsafe chars const userRegexp = /^[a-zA-Z0-9\._\-]+$/; - const reset = '\u001B8\u001B[u'; const clear = '\u001Bc'; @@ -203,6 +203,10 @@ module.exports = class extends Generator { this.props.enablehelp = r.enablehelp; + if( this.props.enablehelp ) { + this.help = require('./help.json'); + } + let prompts = []; for( let m of prompters ) { prompts = prompts.concat(m.prompts(this)); @@ -404,13 +408,18 @@ module.exports = class extends Generator { } _getHelp( topic ) { - if( !this.props.enablehelp ) + if( !this.props.enablehelp || !this.help ) { return ''; - const helpText = help.text( topic ); + } + + // TODO: remove default later: + const helpText = this.help[topic] || this.help['__default__']; + if( !helpText ||helpText === '' ) { return ''; } - return "\n\n"+helpText+"\n\n"; + + return "\n\n"+wrap( html2ansi(helpText),82 )+"\n\n"; } }; diff --git a/install/generator-cyphernode/generators/app/lib/html2ansi.js b/install/generator-cyphernode/generators/app/lib/html2ansi.js new file mode 100644 index 0000000..44f24ee --- /dev/null +++ b/install/generator-cyphernode/generators/app/lib/html2ansi.js @@ -0,0 +1,43 @@ +const parse5 = require('parse5'); +const chalk = require('chalk'); + +const options = { + scriptingEnabled: false +} + +const convert = function(data){ + + // recursively flatten + let v = data.childNodes && data.childNodes.length? + data.childNodes.map(d=> convert(d)).join(''): + data.value?data.value:''; + + switch(data.tagName){ + case 'br': + v += '\n' + break + case 'font': + if( data.attrs && data.attrs.length ) { + for( let attr of data.attrs ) { + if( attr.name === 'color' && /^#[a-f0-9]{6}$/.test(attr.value) ) { + v = chalk.hex(attr.value)(v); + } + if( attr.name === 'bold' && attr.value === 'true' ) { + v = chalk.bold(v); + } + if( attr.name === 'italic' && attr.value === 'true' ) { + v = chalk.italic(v); + } + if( attr.name === 'strikethrough' && attr.value === 'true' ) { + v = chalk.strikethrough(v); + } + } + } + break; + } + return v; +} + +module.exports = function(html){ + return convert(parse5.parseFragment(html, options)); +} diff --git a/install/generator-cyphernode/package.json b/install/generator-cyphernode/package.json index 01ac1dc..a5c047d 100644 --- a/install/generator-cyphernode/package.json +++ b/install/generator-cyphernode/package.json @@ -23,6 +23,7 @@ "@rauschma/stringio": "^1.4.0", "chalk": "^2.1.0", "coinstring": "^2.3.0", + "parse5": "^5.1.0", "validator": "^10.8.0", "wrap-ansi": "^4.0.0", "yeoman-generator": "^2.0.1"