diff options
Diffstat (limited to 'lib/search.js')
-rw-r--r-- | lib/search.js | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/search.js b/lib/search.js index 45e436a0c..71888835c 100644 --- a/lib/search.js +++ b/lib/search.js @@ -108,16 +108,22 @@ function getWords (data) { function filterWords (data, args, notArgs) { var words = data.words for (var i = 0, l = args.length; i < l; i ++) { - if (words.indexOf(args[i]) === -1) { - return false - } + if (!match(words, args[i])) return false } for (var i = 0, l = notArgs.length; i < l; i ++) { - if (words.indexOf(notArgs[i]) !== -1) return false + if (match(words, notArgs[i])) return false } return true } +function match (words, arg) { + if (arg.charAt(0) === "/" && arg.slice(-1) === "/") { + arg = new RegExp(arg.substr(1, arg.length - 2)) + return words.match(arg) + } + return words.indexOf(arg) !== -1 +} + function prettify (data, args) { try { var tty = require("tty") @@ -201,8 +207,17 @@ function addColorMarker (str, arg, i) { var m = i % cl + 1 , markStart = String.fromCharCode(m) , markEnd = String.fromCharCode(0) - , pieces = str.toLowerCase().split(arg.toLowerCase()) + + if (arg.charAt(0) === "/" && arg.slice(-1) === "/") { + return str.replace( new RegExp(arg.substr(1, arg.length - 2), "gi") + , function (bit) { return markStart + bit + markEnd } ) + + } + + // just a normal string, do the split/map thing + var pieces = str.toLowerCase().split(arg.toLowerCase()) , p = 0 + return pieces.map(function (piece, i) { piece = str.substr(p, piece.length) var mark = markStart @@ -211,8 +226,8 @@ function addColorMarker (str, arg, i) { p += piece.length + arg.length return piece + mark }).join("") - return str.split(arg).join(mark) } + function colorize (line) { for (var i = 0; i < cl; i ++) { var m = i + 1 |