mirror of
https://github.com/antonmedv/fx
synced 2024-11-17 09:25:32 +00:00
Implement for advance highlighting
This commit is contained in:
parent
3c891babf7
commit
bb79c9812d
45
print.js
45
print.js
@ -2,27 +2,36 @@
|
|||||||
const indent = require('indent-string')
|
const indent = require('indent-string')
|
||||||
const config = require('./config')
|
const config = require('./config')
|
||||||
|
|
||||||
|
function format(value, style, highlightStyle, regexp, transform = x => x) {
|
||||||
|
if (!regexp) {
|
||||||
|
return config.string(transform(value))
|
||||||
|
}
|
||||||
|
const marked = value
|
||||||
|
.replace(regexp, s => '<highlight>' + s + '<highlight>')
|
||||||
|
|
||||||
|
return transform(marked)
|
||||||
|
.split(/<highlight>/g)
|
||||||
|
.map((s, i) => i % 2 !== 0 ? highlightStyle(s) : style(s))
|
||||||
|
.join('')
|
||||||
|
}
|
||||||
|
|
||||||
function print(input, options = {}) {
|
function print(input, options = {}) {
|
||||||
const {expanded, highlight, currentPath} = options
|
const {expanded, highlight, currentPath} = options
|
||||||
const index = new Map()
|
const index = new Map()
|
||||||
let row = 0
|
let row = 0
|
||||||
|
|
||||||
function format(text, style, path) {
|
|
||||||
text = JSON.stringify(text)
|
|
||||||
if (!highlight) {
|
|
||||||
return style(text)
|
|
||||||
}
|
|
||||||
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
|
|
||||||
return text
|
|
||||||
.replace(highlight, s => '<fx>' + s + '<fx>')
|
|
||||||
.split(/<fx>/g)
|
|
||||||
.map((s, i) => i % 2 !== 0 ? highlightStyle(s) : style(s))
|
|
||||||
.join('')
|
|
||||||
}
|
|
||||||
|
|
||||||
function doPrint(v, path = '') {
|
function doPrint(v, path = '') {
|
||||||
index.set(row, path)
|
index.set(row, path)
|
||||||
|
|
||||||
|
// Code for highlighting parts become cumbersome.
|
||||||
|
// Maybe we should refactor this part.
|
||||||
|
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
|
||||||
|
const formatStyle = (v, style) => format(JSON.stringify(v), style, highlightStyle, highlight)
|
||||||
|
const formatText = (v, style, path) => {
|
||||||
|
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
|
||||||
|
return format(v, style, highlightStyle, highlight, JSON.stringify)
|
||||||
|
}
|
||||||
|
|
||||||
const eol = () => {
|
const eol = () => {
|
||||||
row++
|
row++
|
||||||
return '\n'
|
return '\n'
|
||||||
@ -33,20 +42,20 @@ function print(input, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (v === null) {
|
if (v === null) {
|
||||||
return format(v, config.null, path)
|
return formatStyle(v, config.null)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof v === 'number' && Number.isFinite(v)) {
|
if (typeof v === 'number' && Number.isFinite(v)) {
|
||||||
return format(v, config.number, path)
|
return formatStyle(v, config.number)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof v === 'boolean') {
|
if (typeof v === 'boolean') {
|
||||||
return format(v, config.boolean, path)
|
return formatStyle(v, config.boolean)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof v === 'string') {
|
if (typeof v === 'string') {
|
||||||
return format(v, config.string, path)
|
return formatText(v, config.string, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(v)) {
|
if (Array.isArray(v)) {
|
||||||
@ -84,7 +93,7 @@ function print(input, options = {}) {
|
|||||||
output += eol()
|
output += eol()
|
||||||
let i = 0
|
let i = 0
|
||||||
for (let [key, value] of entries) {
|
for (let [key, value] of entries) {
|
||||||
const part = format(key, config.key, path + '.' + key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
|
const part = formatText(key, config.key, path + '.' + key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
|
||||||
output += indent(part, config.space)
|
output += indent(part, config.space)
|
||||||
output += i++ < len - 1 ? config.comma(',') : ''
|
output += i++ < len - 1 ? config.comma(',') : ''
|
||||||
output += eol()
|
output += eol()
|
||||||
|
Loading…
Reference in New Issue
Block a user