2 var objectAssign = require('object-assign')
4 module.exports = function () {
5 return ThemeSetProto.newThemeSet()
10 ThemeSetProto.baseTheme = require('./base-theme.js')
12 ThemeSetProto.newTheme = function (parent, theme) {
15 parent = this.baseTheme
17 return objectAssign({}, parent, theme)
20 ThemeSetProto.getThemeNames = function () {
21 return Object.keys(this.themes)
24 ThemeSetProto.addTheme = function (name, parent, theme) {
25 this.themes[name] = this.newTheme(parent, theme)
28 ThemeSetProto.addToAllThemes = function (theme) {
29 var themes = this.themes
30 Object.keys(themes).forEach(function (name) {
31 objectAssign(themes[name], theme)
33 objectAssign(this.baseTheme, theme)
36 ThemeSetProto.getTheme = function (name) {
37 if (!this.themes[name]) throw this.newMissingThemeError(name)
38 return this.themes[name]
41 ThemeSetProto.setDefault = function (opts, name) {
46 var platform = opts.platform == null ? 'fallback' : opts.platform
47 var hasUnicode = !!opts.hasUnicode
48 var hasColor = !!opts.hasColor
49 if (!this.defaults[platform]) this.defaults[platform] = {true: {}, false: {}}
50 this.defaults[platform][hasUnicode][hasColor] = name
53 ThemeSetProto.getDefault = function (opts) {
55 var platformName = opts.platform || process.platform
56 var platform = this.defaults[platformName] || this.defaults.fallback
57 var hasUnicode = !!opts.hasUnicode
58 var hasColor = !!opts.hasColor
59 if (!platform) throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor)
60 if (!platform[hasUnicode][hasColor]) {
61 if (hasUnicode && hasColor && platform[!hasUnicode][hasColor]) {
63 } else if (hasUnicode && hasColor && platform[hasUnicode][!hasColor]) {
65 } else if (hasUnicode && hasColor && platform[!hasUnicode][!hasColor]) {
68 } else if (hasUnicode && !hasColor && platform[!hasUnicode][hasColor]) {
70 } else if (!hasUnicode && hasColor && platform[hasUnicode][!hasColor]) {
72 } else if (platform === this.defaults.fallback) {
73 throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor)
76 if (platform[hasUnicode][hasColor]) {
77 return this.getTheme(platform[hasUnicode][hasColor])
79 return this.getDefault(objectAssign({}, opts, {platform: 'fallback'}))
83 ThemeSetProto.newMissingThemeError = function newMissingThemeError (name) {
84 var err = new Error('Could not find a gauge theme named "' + name + '"')
85 Error.captureStackTrace.call(err, newMissingThemeError)
87 err.code = 'EMISSINGTHEME'
91 ThemeSetProto.newMissingDefaultThemeError = function newMissingDefaultThemeError (platformName, hasUnicode, hasColor) {
93 'Could not find a gauge theme for your platform/unicode/color use combo:\n' +
94 ' platform = ' + platformName + '\n' +
95 ' hasUnicode = ' + hasUnicode + '\n' +
96 ' hasColor = ' + hasColor)
97 Error.captureStackTrace.call(err, newMissingDefaultThemeError)
98 err.platform = platformName
99 err.hasUnicode = hasUnicode
100 err.hasColor = hasColor
101 err.code = 'EMISSINGTHEME'
105 ThemeSetProto.newThemeSet = function () {
106 var themeset = function (opts) {
107 return themeset.getDefault(opts)
109 return objectAssign(themeset, ThemeSetProto, {
110 themes: objectAssign({}, this.themes),
111 baseTheme: objectAssign({}, this.baseTheme),
112 defaults: JSON.parse(JSON.stringify(this.defaults || {}))