You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

theme-set.js 3.6KB

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