Skip to content

Commit

Permalink
highlight word
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Dec 25, 2013
1 parent b85c64a commit 0090580
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.iciba = function(data) {
if (data.sent && data.sent.length) {
log();
data.sent.forEach(function(item, i) {
log((i + 1 + ". ").faint + item.orig.grey);
log((i + 1 + ". ").faint + highlight(item.orig, data.key));
log(' ' + item.trans.cyan);
});
}
Expand Down Expand Up @@ -76,7 +76,7 @@ exports.youdao = function(data) {
if (data.web && data.web.length) {
log();
data.web.forEach(function(item, i) {
log((i + 1 + ". ").faint + item.key.grey);
log((i + 1 + ". ").faint + highlight(item.key, data.query));
log(' ' + item.value.join(',').cyan);
});
}
Expand All @@ -93,3 +93,8 @@ function log(message, indentNum) {
}
console.log(indent, message || '');
}

function highlight(string, key) {
string = string.replace(new RegExp('(.*)(' + key + ')(.*)', 'gi'), '$1$2' + '$3'.grey);
return string.replace(new RegExp('(.*?)(' + key + ')', 'gi'), '$1'.grey + '$2'.yellow);
}

0 comments on commit 0090580

Please sign in to comment.