From ac8505c42939cfc9411364365eab4a6b12a4dc3d Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Mon, 23 Mar 2020 11:37:39 +0100 Subject: [PATCH] Add frontend configuration Allow configuring of: - time format - indentation of message that are wrapped on a new line --- .cabal.yml-example | 6 ++++++ cli.js | 28 +++++++++++++++++++++++----- neat-screen.js | 9 ++++++--- views.js | 10 +++++++++- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/.cabal.yml-example b/.cabal.yml-example index 4c64f06..50623b4 100644 --- a/.cabal.yml-example +++ b/.cabal.yml-example @@ -2,3 +2,9 @@ cabals: - cabal://bd45fde0ad866d4069af490f0ca9b07110808307872d4b659a4ff7a4ef85315a - 22f7763be0e939160dd04137b66aaac8f2179350eec740e57a656dfdf1f4dc29 - cbl://3d45fde0ad866d4069af490f0ca9b07110808307872d4b659a4ff7a4ef853132 + +frontend: + # or perhaps '%H%M' + messageTimeformat: '%T' + # indent line-wrapped message after: none, time, nick + messageIndent: nick diff --git a/cli.js b/cli.js index 6c747e6..0727f97 100755 --- a/cli.js +++ b/cli.js @@ -15,6 +15,9 @@ var rootdir = Client.getCabalDirectory() var rootconfig = `${rootdir}/config.yml` var archivesdir = `${rootdir}/archives/` +const defaultMessageTimeformat = '%T' +const defaultMessageIndent = 'nick' + var usage = `Usage Create a new cabal: @@ -75,7 +78,15 @@ mkdirp.sync(rootdir) // create a default config in rootdir if it doesn't exist if (!fs.existsSync(rootconfig)) { - saveConfig(rootconfig, { cabals: [], aliases: {}, cache: {} }) + saveConfig(rootconfig, { + cabals: [], + aliases: {}, + cache: {}, + frontend: { + messageTimeformat: defaultMessageTimeformat, + messageIndent: defaultMessageIndent + } + }) } // Attempt to load local or homedir config file @@ -85,6 +96,13 @@ try { if (!config.cabals) { config.cabals = [] } if (!config.aliases) { config.aliases = {} } if (!config.cache) { config.cache = {} } + if (!config.frontend) { config.frontend = {} } + if (!config.frontend.messageTimeformat) { + config.frontend.messageTimeformat = defaultMessageTimeformat + } + if (!config.frontend.messageIndent) { + config.frontend.messageIndent = defaultMessageIndent + } cabalKeys = config.cabals } } catch (e) { @@ -189,7 +207,7 @@ if (!cabalKeys.length) { captureQrCode({ retry: true }).then((key) => { if (key) { console.log('\u0007') // system bell - start([key]) + start([key], config.frontend) } else { console.log('No QR code detected.') process.exit(0) @@ -201,11 +219,11 @@ if (!cabalKeys.length) { console.error('Linux: sudo apt-get install fswebcam') }) } else { - start(cabalKeys) + start(cabalKeys, config.frontend) } } -function start (keys) { +function start (keys, frontendConfig) { if (args.key && args.message) { publishSingleMessage({ key: args.key, @@ -230,7 +248,7 @@ function start (keys) { console.error(`created the cabal: ${chalk.greenBright('cabal://' + client.getCurrentCabal().key)}`) // log to terminal output (stdout is occupied by interface) keys = [client.getCurrentCabal().key] } - if (!args.seed) { frontend({ client }) } else { + if (!args.seed) { frontend({ client, frontendConfig }) } else { keys.forEach((k) => { console.log('Seeding', k) console.log() diff --git a/neat-screen.js b/neat-screen.js index 19998a6..a796471 100644 --- a/neat-screen.js +++ b/neat-screen.js @@ -13,6 +13,7 @@ var welcomeMessage = fs.readFileSync(welcomePath).toString().split('\n') function NeatScreen (props) { if (!(this instanceof NeatScreen)) return new NeatScreen(props) this.client = props.client + this.config = props.frontendConfig this.commander = Commander(this, this.client) var self = this @@ -244,6 +245,8 @@ function NeatScreen (props) { state.mentions = {} state.selectedWindowPane = 'channels' state.windowPanes = [state.selectedWindowPane] + state.config = this.config + state.messageTimeLength = strftime(this.config.messageTimeformat, new Date()).length this.state = state Object.defineProperty(this.state, 'cabal', { @@ -423,7 +426,7 @@ NeatScreen.prototype.formatMessage = function (msg) { var color = keyToColour(msg.key) || colours[5] - var timestamp = `${chalk.dim(formatTime(msg.value.timestamp))}` + var timestamp = `${chalk.dim(formatTime(msg.value.timestamp, this.config.messageTimeformat))}` var authorText = `${chalk.dim('<')}${highlight ? chalk.whiteBright(author) : chalk[color](author)}${chalk.dim('>')}` if (msg.value.type === 'status') { highlight = false // never highlight from status @@ -453,8 +456,8 @@ NeatScreen.prototype.formatMessage = function (msg) { } } -function formatTime (t) { - return strftime('%T', new Date(t)) +function formatTime (t, fmt) { + return strftime(fmt, new Date(t)) } function keyToColour (key) { diff --git a/views.js b/views.js index 5b3d5ea..ed42708 100644 --- a/views.js +++ b/views.js @@ -189,7 +189,15 @@ function renderMessages (state, width, height) { // Character-wrap to area edge var allLines = msgs.reduce(function (accum, msg) { let nickLength = msg.raw.author ? msg.raw.author.length : 0 - accum.push.apply(accum, util.wrapAnsi(msg.formatted, width, nickLength + 8 /* ti:me:msg */ + 4 /* spacing + <> */)) + var indent = 0 + if (state.config.messageIndent === 'time' || + state.config.messageIndent === 'nick') { + indent += state.messageTimeLength + 1 // + space + } + if (state.config.messageIndent === 'nick') { + indent += nickLength + 3 // + space and <> + } + accum.push.apply(accum, util.wrapAnsi(msg.formatted, width, indent)) return accum }, [])