• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3module.exports = function () {
4  return ThemeSetProto.newThemeSet()
5}
6
7var ThemeSetProto = {}
8
9ThemeSetProto.baseTheme = require('./base-theme.js')
10
11ThemeSetProto.newTheme = function (parent, theme) {
12  if (!theme) {
13    theme = parent
14    parent = this.baseTheme
15  }
16  return Object.assign({}, parent, theme)
17}
18
19ThemeSetProto.getThemeNames = function () {
20  return Object.keys(this.themes)
21}
22
23ThemeSetProto.addTheme = function (name, parent, theme) {
24  this.themes[name] = this.newTheme(parent, theme)
25}
26
27ThemeSetProto.addToAllThemes = function (theme) {
28  var themes = this.themes
29  Object.keys(themes).forEach(function (name) {
30    Object.assign(themes[name], theme)
31  })
32  Object.assign(this.baseTheme, theme)
33}
34
35ThemeSetProto.getTheme = function (name) {
36  if (!this.themes[name]) {
37    throw this.newMissingThemeError(name)
38  }
39  return this.themes[name]
40}
41
42ThemeSetProto.setDefault = function (opts, name) {
43  if (name == null) {
44    name = opts
45    opts = {}
46  }
47  var platform = opts.platform == null ? 'fallback' : opts.platform
48  var hasUnicode = !!opts.hasUnicode
49  var hasColor = !!opts.hasColor
50  if (!this.defaults[platform]) {
51    this.defaults[platform] = { true: {}, false: {} }
52  }
53  this.defaults[platform][hasUnicode][hasColor] = name
54}
55
56ThemeSetProto.getDefault = function (opts) {
57  if (!opts) {
58    opts = {}
59  }
60  var platformName = opts.platform || process.platform
61  var platform = this.defaults[platformName] || this.defaults.fallback
62  var hasUnicode = !!opts.hasUnicode
63  var hasColor = !!opts.hasColor
64  if (!platform) {
65    throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor)
66  }
67  if (!platform[hasUnicode][hasColor]) {
68    if (hasUnicode && hasColor && platform[!hasUnicode][hasColor]) {
69      hasUnicode = false
70    } else if (hasUnicode && hasColor && platform[hasUnicode][!hasColor]) {
71      hasColor = false
72    } else if (hasUnicode && hasColor && platform[!hasUnicode][!hasColor]) {
73      hasUnicode = false
74      hasColor = false
75    } else if (hasUnicode && !hasColor && platform[!hasUnicode][hasColor]) {
76      hasUnicode = false
77    } else if (!hasUnicode && hasColor && platform[hasUnicode][!hasColor]) {
78      hasColor = false
79    } else if (platform === this.defaults.fallback) {
80      throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor)
81    }
82  }
83  if (platform[hasUnicode][hasColor]) {
84    return this.getTheme(platform[hasUnicode][hasColor])
85  } else {
86    return this.getDefault(Object.assign({}, opts, { platform: 'fallback' }))
87  }
88}
89
90ThemeSetProto.newMissingThemeError = function newMissingThemeError (name) {
91  var err = new Error('Could not find a gauge theme named "' + name + '"')
92  Error.captureStackTrace.call(err, newMissingThemeError)
93  err.theme = name
94  err.code = 'EMISSINGTHEME'
95  return err
96}
97
98ThemeSetProto.newMissingDefaultThemeError =
99  function newMissingDefaultThemeError (platformName, hasUnicode, hasColor) {
100    var err = new Error(
101      'Could not find a gauge theme for your platform/unicode/color use combo:\n' +
102    '    platform = ' + platformName + '\n' +
103    '    hasUnicode = ' + hasUnicode + '\n' +
104    '    hasColor = ' + hasColor)
105    Error.captureStackTrace.call(err, newMissingDefaultThemeError)
106    err.platform = platformName
107    err.hasUnicode = hasUnicode
108    err.hasColor = hasColor
109    err.code = 'EMISSINGTHEME'
110    return err
111  }
112
113ThemeSetProto.newThemeSet = function () {
114  var themeset = function (opts) {
115    return themeset.getDefault(opts)
116  }
117  return Object.assign(themeset, ThemeSetProto, {
118    themes: Object.assign({}, this.themes),
119    baseTheme: Object.assign({}, this.baseTheme),
120    defaults: JSON.parse(JSON.stringify(this.defaults || {})),
121  })
122}
123