Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
Added command.js
Browse files Browse the repository at this point in the history
  • Loading branch information
chalkpe committed May 1, 2017
1 parent 6948cad commit 7b9b2c7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# welcome.js [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
# welcome.js [![CircleCI]](https://circleci.com/gh/ChalkPE/welcome.js) [![JavaScript Style Guide]](https://standardjs.com)
Welcome message for shells

![Screenshot](http://i.imgur.com/DPzwll8.png)
![Screenshot]

## License
[MIT License](LICENSE)

[Screenshot]: http://i.imgur.com/DPzwll8.png
[CircleCI]: https://circleci.com/gh/ChalkPE/welcome.js.svg?style=svg
[JavaScript Style Guide]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
12 changes: 12 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const chalk = require('chalk')
const table = require('./table')

module.exports = ({ input, params, flags, output, options }) => {
let inputs = [chalk.bold.green('$'), chalk.green(input)]
if (output.table) output = table(...output.table, options)

if (params) inputs.push(...params)
if (flags) inputs.push(...flags.map(x => `--${x}`))

return [inputs.join(' '), ...output]
}
File renamed without changes.
25 changes: 15 additions & 10 deletions table.js → lib/table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// const fs = require('mz/fs')
const chalk = require('chalk')
const { max, ceil, floor } = Math
const multibytes = /[가-힣ㄱ-ㅎㅏ-ㅣ]/g
const maxLength = process.stdout.columns
Expand All @@ -8,27 +8,32 @@ const sum = arr => arr.reduce((a, b) => a + b, 0)
const len = str => str ? str.replace(multibytes, '콩콩').length : 0
const pad = (str, n) => space(ceil(n / 2)) + str + space(floor(n / 2))

const table = async (arr, size) => {
const table = (arr, size, options) => {
const chunks = [...Array(ceil(arr.length / size))]
.map((_, i) => arr.slice(i * size, ++i * size))
.map(c => c.concat(...space(size)).slice(0, size))
.map(c => [...c, ...space(size)].slice(0, size))

const lengths = [...Array(size)]
.map((_, i) => 2 + max(...chunks.map(c => len(c[i]))))

if (size > 1 && size + sum(lengths) >= maxLength) return table(arr, size - 1)
const exceeds = size + sum(lengths) >= maxLength
if (exceeds && size > 1) return table(arr, size - 1, options)

const highlight = str =>
options.highlight.some(h => str.includes(h)) && chalk.bold.blue(str)

let padded = chunks
.map(c => c.map((str, i) => pad(str, lengths[i] - len(str))))
.map(c => c.map(str => (options.highlight && highlight(str)) || str))

const border = ([left, filler, centre, right]) =>
[left, lengths.map(n => filler.repeat(n)).join(centre), right].join('')

// const coloured = await fs.readFile('./assets/favorites.txt')

const lines = chunks
.map(c => c.map((str, i) => pad(str, lengths[i] - len(str))))
const lines = padded
.map(c => [null, ...c, null].join('│'))
.reduce((res, c, i) => res.concat(border(i ? '├─┼┤' : '┌─┬┐'), c), [])
.reduce((res, c, i) => [...res, border(i ? '├─┼┤' : '┌─┬┐'), c], [])

return lines.concat(border('└─┴┘'))
return [...lines, border('└─┴┘')]
}

module.exports = table
24 changes: 17 additions & 7 deletions plugins/dimibob.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const os = require('os')
const fs = require('mz/fs')
const path = require('path')
const axios = require('axios')
const moment = require('moment')
const table = require('../table')
const URL = 'http://dimigo.in/pages/dimibob_getdata.php'

const url = 'http://dimigo.in/pages/dimibob_getdata.php'
const file = path.resolve(__dirname, '..', 'assets', 'favorites.txt')

const fields = 'breakfast lunch dinner snack'.split(' ')
const mealTypes = Object.assign({}, ...fields.map(meal => ({ [meal]: meal })))
Expand All @@ -24,15 +28,21 @@ module.exports = async options => {
const params = { d: now.format('YYYYMMDD') }
const myOptions = Object.assign(options, { params })

const { data } = await axios.get(URL, myOptions)
const { data } = await axios.get(url, myOptions)
if (typeof data !== 'object' || !data[meal]) throw new Error('not found')

const meals = data[meal].split(/[ */]/)
const list = data[meal].split(/[ */]/).filter(x => x)
const favorites = (await fs.readFile(file, 'utf-8'))
.split(os.EOL).map(item => item.trim()).filter(x => x)

return { command: {
const command = {
input: 'dimibob',
output: { table: [list, Math.ceil(list.length / 2)] },

params: [params.d],
flags: flags.filter(x => x),
output: table(meals, Math.ceil(meals.length / 2))
} }
options: { highlight: favorites }
}

return { command }
}
16 changes: 3 additions & 13 deletions welcome.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const chalk = require('chalk')
const powerline = require('./powerline')
const command = require('./lib/command')
const powerline = require('./lib/powerline')

module.exports = async plugins => {
const logs = []
Expand All @@ -8,17 +8,7 @@ module.exports = async plugins => {
const results = await Promise.all(plugins)
results.forEach(result => {
if (result.powerline) powerlines.push(result.powerline)
if (result.command) {
let { input, params, flags, output } = result.command
if (!Array.isArray(output)) output = [output]

let inputs = [chalk.bold.green('$'), chalk.green(input)]

if (params) inputs.push(...params)
if (flags) inputs.push(...flags.map(x => `--${x}`))

logs.push(inputs.join(' '), ...output)
}
if (result.command) logs.push(...command(result.command))
})

console.log(powerline(powerlines))
Expand Down

0 comments on commit 7b9b2c7

Please sign in to comment.