Skip to content

Commit

Permalink
Forgive errors to user
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Dec 15, 2018
1 parent a696018 commit 225239b
Showing 1 changed file with 56 additions and 42 deletions.
98 changes: 56 additions & 42 deletions fx.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ module.exports = function start(filename, source) {

input.on('submit', function () {
if (autocomplete.hidden) {
apply()
const code = input.getValue()
if (/^\//.test(code)) {
// Forgive a mistake to the user. This looks like user wanted to search something.
apply('')
applyPattern(code)
} else {
apply(code)
}
} else {
// Autocomplete selected
let code = input.getValue()
Expand All @@ -118,7 +125,8 @@ module.exports = function start(filename, source) {

input.on('cancel', function () {
if (autocomplete.hidden) {
apply()
const code = input.getValue()
apply(code)
} else {
// Autocomplete not selected
autocomplete.hide()
Expand Down Expand Up @@ -163,42 +171,7 @@ module.exports = function start(filename, source) {
})

search.on('submit', function (pattern) {
let regex
let m = pattern.match(/^\/(.*)\/([gimuy]*)$/)
if (m) {
try {
regex = new RegExp(m[1], m[2])
} catch (e) {
// Wrong regexp.
}
} else {
m = pattern.match(/^\/(.*)$/)
if (m) {
try {
regex = new RegExp(m[1], 'gi')
} catch (e) {
// Wrong regexp.
}
}
}
highlight = regex

if (highlight) {
findGen = find(json, highlight)
findNext()
} else {
findGen = null
currentPath = null
}

search.hide()
search.setValue('')

box.height = '100%'
box.focus()

program.cursorPos(0, 0)
render()
applyPattern(pattern)
})

search.on('cancel', function () {
Expand Down Expand Up @@ -365,9 +338,7 @@ module.exports = function start(filename, source) {
return [n, line]
}

function apply() {
const code = input.getValue()

function apply(code) {
if (code && code.length !== 0) {
try {
json = reduce(source, code)
Expand Down Expand Up @@ -432,7 +403,11 @@ module.exports = function start(filename, source) {
if (code && code.length !== 0) {
try {
const pretender = reduce(source, code)
if (typeof pretender !== 'undefined' && typeof pretender !== 'function') {
if (
typeof pretender !== 'undefined'
&& typeof pretender !== 'function'
&& !(pretender instanceof RegExp)
) {
json = pretender
}
} catch (e) {
Expand All @@ -447,6 +422,45 @@ module.exports = function start(filename, source) {
render()
}

function applyPattern(pattern) {
let regex
let m = pattern.match(/^\/(.*)\/([gimuy]*)$/)
if (m) {
try {
regex = new RegExp(m[1], m[2])
} catch (e) {
// Wrong regexp.
}
} else {
m = pattern.match(/^\/(.*)$/)
if (m) {
try {
regex = new RegExp(m[1], 'gi')
} catch (e) {
// Wrong regexp.
}
}
}
highlight = regex

if (highlight) {
findGen = find(json, highlight)
findNext()
} else {
findGen = null
currentPath = null
}

search.hide()
search.setValue('')

box.height = '100%'
box.focus()

program.cursorPos(0, 0)
render()
}

function findNext() {
if (!findGen) {
return
Expand Down

0 comments on commit 225239b

Please sign in to comment.