summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/node_modules/ansi-align/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-04-03 15:43:32 +0200
committerFlorian Dold <florian.dold@gmail.com>2019-04-03 15:45:57 +0200
commit71e285b94c7edaa43aa8115965cf5a36b8e0f80a (patch)
tree7d4aa9d0d5aff686b106cd5da72ba77960c4af43 /deps/node/deps/npm/node_modules/ansi-align/index.js
parent7dadf9356b4f3f4137ce982ea5bb960283116e9a (diff)
downloadakono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.tar.gz
akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.tar.bz2
akono-71e285b94c7edaa43aa8115965cf5a36b8e0f80a.zip
Node.js v11.13.0
Diffstat (limited to 'deps/node/deps/npm/node_modules/ansi-align/index.js')
-rw-r--r--deps/node/deps/npm/node_modules/ansi-align/index.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/deps/node/deps/npm/node_modules/ansi-align/index.js b/deps/node/deps/npm/node_modules/ansi-align/index.js
new file mode 100644
index 00000000..67fa826d
--- /dev/null
+++ b/deps/node/deps/npm/node_modules/ansi-align/index.js
@@ -0,0 +1,61 @@
+'use strict'
+
+const stringWidth = require('string-width')
+
+function ansiAlign (text, opts) {
+ if (!text) return text
+
+ opts = opts || {}
+ const align = opts.align || 'center'
+
+ // short-circuit `align: 'left'` as no-op
+ if (align === 'left') return text
+
+ const split = opts.split || '\n'
+ const pad = opts.pad || ' '
+ const widthDiffFn = align !== 'right' ? halfDiff : fullDiff
+
+ let returnString = false
+ if (!Array.isArray(text)) {
+ returnString = true
+ text = String(text).split(split)
+ }
+
+ let width
+ let maxWidth = 0
+ text = text.map(function (str) {
+ str = String(str)
+ width = stringWidth(str)
+ maxWidth = Math.max(width, maxWidth)
+ return {
+ str,
+ width
+ }
+ }).map(function (obj) {
+ return new Array(widthDiffFn(maxWidth, obj.width) + 1).join(pad) + obj.str
+ })
+
+ return returnString ? text.join(split) : text
+}
+
+ansiAlign.left = function left (text) {
+ return ansiAlign(text, { align: 'left' })
+}
+
+ansiAlign.center = function center (text) {
+ return ansiAlign(text, { align: 'center' })
+}
+
+ansiAlign.right = function right (text) {
+ return ansiAlign(text, { align: 'right' })
+}
+
+module.exports = ansiAlign
+
+function halfDiff (maxWidth, curWidth) {
+ return Math.floor((maxWidth - curWidth) / 2)
+}
+
+function fullDiff (maxWidth, curWidth) {
+ return maxWidth - curWidth
+}