Skip to content

Commit

Permalink
Merge pull request #151 from quite/frontend
Browse files Browse the repository at this point in the history
Add frontend configuration
  • Loading branch information
cblgh authored Mar 24, 2020
2 parents 0a3515a + 366d6e7 commit a914706
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
9 changes: 9 additions & 0 deletions .cabal.yml-example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ cabals:
- cabal://bd45fde0ad866d4069af490f0ca9b07110808307872d4b659a4ff7a4ef85315a
- 22f7763be0e939160dd04137b66aaac8f2179350eec740e57a656dfdf1f4dc29
- cbl://3d45fde0ad866d4069af490f0ca9b07110808307872d4b659a4ff7a4ef853132

frontend:
# timestamp format, using https://github.com/samsonjs/strftime
# a shorter alternative can be: '%H%M'
messageTimeformat: '%T'

# indent messages wrapped onto next line to
# after: none, time, nick
messageIndent: nick
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,9 @@ ctrl+{n,p}
    select channels 1-9
`alt-n`
    tab between the cabals & channels panes

## Configuring

There is a little configuration that can be done to the terminal frontend.
Timestamp format and indentation of line-wrapped messages can be tweaked.
See examples in: [.cabal.yml-example](.cabal.yml-example)
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 @@ -246,6 +247,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 @@ -425,7 +428,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 @@ -455,8 +458,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) {
const 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 a914706

Please sign in to comment.