This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite theme support for greater extensibility
- Loading branch information
Showing
8 changed files
with
280 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
var spin = require('./spin.js') | ||
var progressBar = require('./progress-bar.js') | ||
|
||
module.exports = { | ||
activityIndicator: function (values, theme, width) { | ||
if (values.spun == null) return | ||
return spin(theme, values.spun) | ||
}, | ||
progressbar: function (values, theme, width) { | ||
if (values.completed == null) return | ||
return progressBar(theme, width, values.completed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict' | ||
var test = require('tap').test | ||
var ThemeSet = require('../theme-set.js') | ||
|
||
var themes = new ThemeSet() | ||
|
||
test('setup', function (t) { | ||
themes.addTheme('fallback', {id: 0}) | ||
themes.addTheme('test1', {id: 1}) | ||
themes.addTheme('test2', {id: 2}) | ||
themes.addTheme('test3', {id: 3}) | ||
themes.addTheme('test4', {id: 4}) | ||
themes.addTheme('testz', themes.getTheme('fallback'), {id: 'z'}) | ||
themes.setDefault('fallback') | ||
themes.setDefault({platform: 'aa', hasUnicode: false, hasColor: false}, 'test1') | ||
themes.setDefault({platform: 'bb', hasUnicode: true, hasColor: true}, 'test2') | ||
themes.setDefault({platform: 'ab', hasUnicode: false, hasColor: true}, 'test3') | ||
themes.setDefault({platform: 'ba', hasUnicode: true, hasColor: false}, 'test4') | ||
|
||
themes.setDefault({platform: 'zz', hasUnicode: false, hasColor: false}, 'test1') | ||
themes.setDefault({platform: 'zz', hasUnicode: true, hasColor: true}, 'test2') | ||
themes.setDefault({platform: 'zz', hasUnicode: false, hasColor: true}, 'test3') | ||
themes.setDefault({platform: 'zz', hasUnicode: true, hasColor: false}, 'test4') | ||
t.done() | ||
}) | ||
|
||
test('themeset', function (t) { | ||
t.is(themes().id, 0, 'fallback') | ||
|
||
t.is(themes({platform: 'aa'}).id, 1, 'aa ff') | ||
t.is(themes({platform: 'aa', hasUnicode: true}).id, 1, 'aa tf') | ||
t.is(themes({platform: 'aa', hasColor: true}).id, 1, 'aa ft') | ||
t.is(themes({platform: 'aa', hasUnicode: true, hasColor: true}).id, 1, 'aa tt') | ||
t.is(themes({platform: 'bb'}).id, 0, 'bb ff') | ||
t.is(themes({platform: 'bb', hasUnicode: true}).id, 0, 'bb tf') | ||
t.is(themes({platform: 'bb', hasColor: true}).id, 0, 'bb ft') | ||
t.is(themes({platform: 'bb', hasUnicode: true, hasColor: true}).id, 2, 'bb tt') | ||
|
||
t.is(themes({platform: 'ab'}).id, 0, 'ab ff') | ||
t.is(themes({platform: 'ab', hasUnicode: true}).id, 0, 'ab tf') | ||
t.is(themes({platform: 'ab', hasColor: true}).id, 3, 'ab ft') | ||
t.is(themes({platform: 'ab', hasUnicode: true, hasColor: true}).id, 3, 'ab tt') | ||
|
||
t.is(themes({platform: 'ba'}).id, 0, 'ba ff') | ||
t.is(themes({platform: 'ba', hasUnicode: true}).id, 4, 'ba tf') | ||
t.is(themes({platform: 'ba', hasColor: true}).id, 0, 'ba ft') | ||
t.is(themes({platform: 'ba', hasUnicode: true, hasColor: true}).id, 4, 'ba tt') | ||
|
||
t.is(themes({platform: 'zz'}).id, 1, 'zz ff') | ||
t.is(themes({platform: 'zz', hasUnicode: true}).id, 4, 'zz tf') | ||
t.is(themes({platform: 'zz', hasColor: true}).id, 3, 'zz ft') | ||
t.is(themes({platform: 'zz', hasUnicode: true, hasColor: true}).id, 2, 'zz tt') | ||
|
||
try { | ||
themes.getTheme('does not exist') | ||
t.fail('missing theme') | ||
} catch (ex) { | ||
t.is(ex.code, 'EMISSINGTHEME', 'missing theme') | ||
} | ||
|
||
t.is(themes.getTheme('testz').id, 'z', 'testz') | ||
|
||
var empty = new ThemeSet() | ||
|
||
try { | ||
empty() | ||
t.fail('no themes') | ||
} catch (ex) { | ||
t.is(ex.code, 'EMISSINGTHEME', 'no themes') | ||
} | ||
|
||
empty.addTheme('exists', {id: 'exists'}) | ||
empty.setDefault({hasUnicode: true, hasColor: true}, 'exists') | ||
try { | ||
empty() | ||
t.fail('no fallback') | ||
} catch (ex) { | ||
t.is(ex.code, 'EMISSINGTHEME', 'no fallback') | ||
} | ||
t.done() | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict' | ||
var objectAssign = require('object-assign') | ||
var baseTheme = require('./base-theme.js') | ||
|
||
module.exports = function () { | ||
return ThemeSetProto.newThemeSet() | ||
} | ||
|
||
var ThemeSetProto = {} | ||
|
||
ThemeSetProto.newTheme = function (parent, theme) { | ||
if (!theme) { | ||
theme = parent | ||
parent = baseTheme | ||
} | ||
return objectAssign({}, parent, theme) | ||
} | ||
|
||
ThemeSetProto.addTheme = function (name, parent, theme) { | ||
this.themes[name] = this.newTheme(parent, theme) | ||
} | ||
|
||
ThemeSetProto.getTheme = function (name) { | ||
if (!this.themes[name]) throw this.newMissingThemeError(name) | ||
return this.themes[name] | ||
} | ||
|
||
ThemeSetProto.setDefault = function (opts, name) { | ||
if (name == null) { | ||
name = opts | ||
opts = {} | ||
} | ||
var platform = opts.platform == null ? 'fallback' : opts.platform | ||
var hasUnicode = !!opts.hasUnicode | ||
var hasColor = !!opts.hasColor | ||
if (!this.defaults[platform]) this.defaults[platform] = {true: {}, false: {}} | ||
this.defaults[platform][hasUnicode][hasColor] = name | ||
} | ||
|
||
ThemeSetProto.getDefault = function (opts) { | ||
if (!opts) opts = {} | ||
var platformName = opts.platform || process.platform | ||
var platform = this.defaults[platformName] || this.defaults.fallback | ||
var hasUnicode = !!opts.hasUnicode | ||
var hasColor = !!opts.hasColor | ||
if (!platform) throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor) | ||
if (!platform[hasUnicode][hasColor]) { | ||
if (hasUnicode && hasColor && platform[!hasUnicode][hasColor]) { | ||
hasUnicode = false | ||
} else if (hasUnicode && hasColor && platform[hasUnicode][!hasColor]) { | ||
hasColor = false | ||
} else if (hasUnicode && hasColor && platform[!hasUnicode][!hasColor]) { | ||
hasUnicode = false | ||
hasColor = false | ||
} else if (hasUnicode && !hasColor && platform[!hasUnicode][hasColor]) { | ||
hasUnicode = false | ||
} else if (!hasUnicode && hasColor && platform[hasUnicode][!hasColor]) { | ||
hasColor = false | ||
} else if (platform === this.defaults.fallback) { | ||
throw this.newMissingDefaultThemeError(platformName, hasUnicode, hasColor) | ||
} | ||
} | ||
if (platform[hasUnicode][hasColor]) { | ||
return this.getTheme(platform[hasUnicode][hasColor]) | ||
} else { | ||
return this.getDefault(objectAssign({}, opts, {platform: 'fallback'})) | ||
} | ||
} | ||
|
||
ThemeSetProto.newMissingThemeError = function newMissingThemeError (name) { | ||
var err = new Error('Could not find a gauge theme named "' + name + '"') | ||
Error.captureStackTrace.call(err, newMissingThemeError) | ||
err.theme = name | ||
err.code = 'EMISSINGTHEME' | ||
return err | ||
} | ||
|
||
ThemeSetProto.newMissingDefaultThemeError = function newMissingDefaultThemeError (platformName, hasUnicode, hasColor) { | ||
var err = new Error( | ||
'Could not find a gauge theme for your platform/unicode/color use combo:\n' + | ||
' platform = ' + platformName + '\n' + | ||
' hasUnicode = ' + hasUnicode + '\n' + | ||
' hasColor = ' + hasColor) | ||
Error.captureStackTrace.call(err, newMissingDefaultThemeError) | ||
err.platform = platformName | ||
err.hasUnicode = hasUnicode | ||
err.hasColor = hasColor | ||
err.code = 'EMISSINGTHEME' | ||
return err | ||
} | ||
|
||
ThemeSetProto.newThemeSet = function () { | ||
var themeset = function (opts) { | ||
return themeset.getDefault(opts) | ||
} | ||
return objectAssign(themeset, ThemeSetProto, {themes: {}, defaults: {}}) | ||
} | ||
|
Oops, something went wrong.