theme-set.js 3.6 KB

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