/
themes.js
executable file
·94 lines (89 loc) · 2.96 KB
/
themes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* eslint-disablemax-len:0 */
const chalk = require('chalk')
const _ = require('lodash')
const fs = require('fs')
const glob = require('glob')
const noon = require('noon')
let TDIR = null
let themeDirExists = null
try {
fs.statSync('themes')
themeDirExists = true
} catch (e) {
if (e.code === 'ENOENT') themeDirExists = false
}
themeDirExists ? TDIR = 'themes/' : TDIR = `${process.env.NODE_PATH}/iloa/themes/`
/**
* The themes module provides useful repetitive theme tasks
* @module Themes
*/
/**
* Loads theme
* @public
* @param {string} theme The name of the theme
* @return {Object} load The style to use
*/
exports.loadTheme = (theme) => {
let dirExists = null
let load = null
try {
fs.statSync('themes')
dirExists = true
} catch (e) {
if (e.code === 'ENOENT') dirExists = false
}
const CFILE = `${process.env.HOME}/.iloa.noon`
const config = noon.load(CFILE)
if (!dirExists && config.verbose) console.log(chalk.white(`${process.cwd()}/themes does not exist, falling back to ${process.env.NODE_PATH}/iloa/themes.`))
load = noon.load(`${TDIR}${theme}.noon`)
return load
}
/**
* Gets themes for list command
* @public
* @return {Array} List of theme names
*/
exports.getThemes = () => {
const list = []
let dirExists = null
let files = []
try {
fs.statSync('themes')
dirExists = true
} catch (e) {
if (e.code === 'ENOENT') dirExists = false
}
const CFILE = `${process.env.HOME}/.iloa.noon`
const config = noon.load(CFILE)
if (!dirExists && config.verbose) console.log(chalk.white(`${process.cwd()}/themes does not exist, falling back to ${process.env.NODE_PATH}/iloa/themes.`))
files = glob.sync(`${TDIR}*.noon`)
_.each(files, (path) => {
const name = path.replace(/[a-z0-9/_.]*themes\//, '').replace(/\.noon/, '')
list.push(name)
})
return list
}
/**
* Prints label, connector, and content
* @public
* @param {Object} theme The style to use
* @param {string} direction 'down' or 'right'
* @param {string} text The label text
* @param {string} [content] The text the label points at
* @return {string} The stylized string to log
*/
exports.label = (theme, direction, text, content) => {
const pstyle = _.get(chalk, theme.prefix.style)
const tstyle = _.get(chalk, theme.text.style)
const sstyle = _.get(chalk, theme.suffix.style)
const cnstyle = _.get(chalk, theme.connector.style)
const ctstyle = _.get(chalk, theme.content.style)
let label = `${pstyle(theme.prefix.str)}${tstyle(text)}${sstyle(theme.suffix.str)}`
if (direction === 'right') {
content !== null && content !== undefined ? label = `${label}${cnstyle(theme.connector.str)}${ctstyle(content)}` : label = `${label}`
} else if (direction === 'down') {
content !== null && content !== undefined ? label = `${label}\n${cnstyle(theme.connector.str)}${ctstyle(content)}` : label = `${label}`
} else { throw new Error("Unsupported label direction, use 'down' or 'right'.") }
console.log(label)
return label
}