Separate regexp generation

This commit is contained in:
Anton Medvedev 2018-12-15 12:48:37 +07:00
parent 935857230d
commit beedbfd36a
2 changed files with 22 additions and 7 deletions

18
fx.js
View File

@ -164,7 +164,7 @@ module.exports = function start(filename, source) {
search.on('submit', function (pattern) {
let regex
const m = pattern.match(/^\/(.*)\/?([gimuy]*)$/)
let m = pattern.match(/^\/(.*)\/([gimuy]*)$/)
if (m) {
try {
regex = new RegExp(m[1], m[2])
@ -172,6 +172,14 @@ module.exports = function start(filename, source) {
// Wrong regexp.
}
}
m = pattern.match(/^\/(.*)$/)
if (m) {
try {
regex = new RegExp(m[1], 'gi')
} catch (e) {
// Wrong regexp.
}
}
highlight = regex
if (highlight) {
@ -452,7 +460,13 @@ module.exports = function start(filename, source) {
for (let [k, v] of index) {
if (v === currentPath) {
const y = box.getScreenNumber(k)
let y = box.getScreenNumber(k)
// Scroll one line up for better view and make sure it's not negative.
if (--y < 0) {
y = 0
}
box.scrollTo(y)
screen.render()
}

View File

@ -8,6 +8,7 @@ function print(input, options = {}) {
let row = 0
function format(text, style, path) {
text = JSON.stringify(text)
if (!highlight) {
return style(text)
}
@ -32,20 +33,20 @@ function print(input, options = {}) {
}
if (v === null) {
return format('null', config.null, path)
return format(v, config.null, path)
}
if (typeof v === 'number' && Number.isFinite(v)) {
return format(v.toString(), config.number, path)
return format(v, config.number, path)
}
if (typeof v === 'boolean') {
return format(v.toString(), config.boolean, path)
return format(v, config.boolean, path)
}
if (typeof v === 'string') {
return format(JSON.stringify(v), config.string, path)
return format(v, config.string, path)
}
if (Array.isArray(v)) {
@ -83,7 +84,7 @@ function print(input, options = {}) {
output += eol()
let i = 0
for (let [key, value] of entries) {
const part = format(JSON.stringify(key), config.key, path + '.' + key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
const part = format(key, config.key, path + '.' + key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
output += indent(part, config.space)
output += i++ < len - 1 ? config.comma(',') : ''
output += eol()