Skip to content

Commit

Permalink
Support fuzzy search (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
jopemachine and sindresorhus committed Feb 12, 2022
1 parent 96cbfa6 commit 91efb6d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const cli = meow(`
Run without arguments to use the interactive mode.
In interactive mode, 🚦n% indicates high CPU usage and 🐏n% indicates high memory usage.
Supports fuzzy search in the interactive mode.
The process name is case insensitive.
`, {
Expand Down
29 changes: 11 additions & 18 deletions interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import cliTruncate from 'cli-truncate';
import {allPortsWithPid} from 'pid-port';
import fkill from 'fkill';
import processExists from 'process-exists';
import FuzzySearch from 'fuzzy-search';

const isWindows = process.platform === 'win32';
const commandLineMargins = 4;
Expand Down Expand Up @@ -43,16 +44,6 @@ const processExited = async (pid, timeout) => {
return !exists;
};

const nameFilter = (input, process_) => {
const isPort = input[0] === ':';

if (isPort) {
return process_.ports.find(port => port.startsWith(input.slice(1)));
}

return process_.name.toLowerCase().includes(input.toLowerCase());
};

const preferNotMatching = matches => (a, b) => {
const aMatches = matches(a);
return matches(b) === aMatches ? 0 : (aMatches ? 1 : -1);
Expand Down Expand Up @@ -99,22 +90,24 @@ const preferHeurisicallyInterestingProcesses = (a, b) => {
};

const filterProcesses = (input, processes, flags) => {
const filters = {
name: process_ => input ? nameFilter(input, process_) : true,
verbose: process_ => input ? (isWindows ? process_.name : process_.cmd).toLowerCase().includes(input.toLowerCase()) : true,
};

const memoryThreshold = flags.verbose ? 0 : 1;
const cpuThreshold = flags.verbose ? 0 : 3;

return processes
const filteredProcesses = new FuzzySearch(
processes,
[flags.verbose && !isWindows ? 'cmd' : 'name'],
{
caseSensitive: false,
},
)
.search(input);

return filteredProcesses
.filter(process_ => !(
process_.name.endsWith('-helper')
|| process_.name.endsWith('Helper')
|| process_.name.endsWith('HelperApp')
))
// eslint-disable-next-line unicorn/no-array-callback-reference
.filter(flags.verbose ? filters.verbose : filters.name)
.sort(preferHeurisicallyInterestingProcesses)
.map(process_ => {
const renderPercentage = percents => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"cli-truncate": "^3.1.0",
"esc-exit": "^3.0.0",
"fkill": "^8.0.0",
"fuzzy-search": "^3.2.1",
"inquirer": "^8.2.0",
"inquirer-autocomplete-prompt": "^1.4.0",
"meow": "^10.1.1",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ $ fkill --help
Run without arguments to use the interactive interface.
In interactive mode, 🚦n% indicates high CPU usage and 🐏n% indicates high memory usage.
Supports fuzzy search in the interactive mode.
The process name is case insensitive.
```
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process';
import childProcess from 'node:child_process';
import test from 'ava';
import execa from 'execa';
Expand All @@ -23,6 +24,15 @@ test('pid', async t => {
await noopProcessKilled(t, pid);
});

// TODO: Remove the if-statement when https://github.com/nodejs/node/issues/35503 is fixed.
if (process.platform === 'darwin') {
test('fuzzy search', async t => {
const pid = await noopProcess({title: '!noo00oop@'});
await execa('./cli.js', ['o00oop@']);
await noopProcessKilled(t, pid);
});
}

test('kill from port', async t => {
const port = await getPort();
const {pid} = childProcess.spawn('node', ['fixture.js', port]);
Expand Down

0 comments on commit 91efb6d

Please sign in to comment.