Skip to content

Commit

Permalink
Merge pull request #22 from lavgl/fuzzy-search-multi-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ranyitz committed Mar 28, 2018
2 parents d2cd8b5 + 1e2b7a1 commit 8988ffd
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 9 deletions.
105 changes: 96 additions & 9 deletions src/actions/fuzzy-search.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
const chalk = require('chalk');
const figures = require('figures');
const logUpdate = require('log-update');
const isEmpty = require('lodash/isEmpty');

const Input = require('../fuzzy-search/input');
const sortBySimilarity = require('../fuzzy-search/sort-by-similarity');
const getAction = require('./get');

const HALF_WIDTH_SPACE = '\u2000';

const resetConsole = () => process.stdout.write('\x1Bc');

const getResultState = ({ chosen, result, i, currentResult }) => {
const isMarked = chosen.indexOf(result.value) !== -1;
const isCurrent = i === currentResult;

if (isCurrent && isMarked) {
return 'current&marked';
}

if (isCurrent) {
return 'current';
}

if (isMarked) {
return 'marked';
}

return 'plain';
};

const renderItem = ({ state, result }) => {
switch (state) {
case 'current': {
return `${chalk.red(figures.pointer)} ${chalk.bold(result.highlight)}`;
}
case 'marked': {
return `${HALF_WIDTH_SPACE}${chalk.magenta(figures.pointer)}${
result.highlight
}`;
}
case 'current&marked': {
return `${chalk.red(figures.pointer)}${chalk.magenta(
figures.pointer,
)}${chalk.bold(result.highlight)}`;
}
default: {
return ` ${result.highlight}`;
}
}
};

const toggleMarking = ({ chosen, value }) => {
return chosen.indexOf(value) !== -1
? chosen.filter(moduleName => moduleName !== value)
: chosen.concat(value);
};

module.exports = workspace => {
let results = [];
let chosen = [];
let currentResult = 0;
let currentInputValue = '';

Expand All @@ -17,12 +68,14 @@ module.exports = workspace => {
const renderResults = () => {
return results
.map((result, i) => {
if (i === currentResult) {
return `${chalk.red(figures.pointer)} ${chalk.bold(
result.highlight,
)}`;
}
return ` ${result.highlight}`;
const state = getResultState({
chosen,
result,
i,
currentResult,
});

return renderItem({ state, result });
})
.join('\n');
};
Expand Down Expand Up @@ -81,15 +134,49 @@ module.exports = workspace => {
renderUi();
});

input.on('tab', () => {
if (isEmpty(results)) return;

const { value } = results[currentResult];

chosen = toggleMarking({ chosen, value });

if (currentResult < results.length - 1) {
currentResult++;
}

renderUi();
});

input.on('shiftTab', () => {
if (isEmpty(results)) return;

const { value } = results[currentResult];

chosen = toggleMarking({ chosen, value });

if (currentResult > 0) {
currentResult--;
}

renderUi();
});

input.on('choose', () => {
if (results.length === 0) {
if (isEmpty(results) && isEmpty(chosen)) {
return;
}

input.end();
logUpdate('');
const chosen = results[currentResult].value;
console.log(getAction(workspace, chosen)); // eslint-disable-line no-console

if (isEmpty(chosen)) {
chosen.push(results[currentResult].value);
}

chosen.forEach(moduleName => {
console.log(getAction(workspace, moduleName)); // eslint-disable-line no-console
});
process.exit(0);
});

Expand Down
8 changes: 8 additions & 0 deletions src/fuzzy-search/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
enter,
del,
backspace,
tab,
shiftTab,
} = require('./raw-key-codes');

module.exports = class Input extends EventEmitter {
Expand Down Expand Up @@ -88,6 +90,12 @@ module.exports = class Input extends EventEmitter {
case enter:
this.emit('choose');
break;
case tab:
this.emit('tab');
break;
case shiftTab:
this.emit('shiftTab');
break;
default:
this.insertChar(key);
changed = true;
Expand Down
2 changes: 2 additions & 0 deletions src/fuzzy-search/raw-key-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ module.exports.down = '\u001b\u005b\u0042';
module.exports.enter = '\u000d';
module.exports.del = '\u001b\u005b\u0033\u007e';
module.exports.backspace = '\u007f';
module.exports.tab = '\u0009';
module.exports.shiftTab = '\u001b[Z';

0 comments on commit 8988ffd

Please sign in to comment.