Skip to content

Commit

Permalink
Add frontend configuration
Browse files Browse the repository at this point in the history
Allow configuring of:
  - time format
  - indentation of message that are wrapped on a new line
  • Loading branch information
quite committed Mar 23, 2020
1 parent f3045d7 commit 8a8ae5f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .cabal.yml-example
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 23 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions neat-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion views.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, [])

Expand Down

0 comments on commit 8a8ae5f

Please sign in to comment.