Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds process search #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/gtop.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function init() {
new monitor.Mem(memLine, memDonut, swapDonut);
new monitor.Net(netSpark);
new monitor.Disk(diskDonut);
new monitor.Proc(procTable); // no Windows support
new monitor.Proc(procTable, screen); // no Windows support
}

process.on('uncaughtException', function(err) {
Expand Down
42 changes: 39 additions & 3 deletions lib/monitor/proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ var pars = {
m: 'mem',
};

function Proc(table) {
const blessed = require("blessed")
let processSearchTerm = ""

function Proc(table, screen) {
this.screen = screen
this.table = table;

this.pSort = pars.c;
Expand All @@ -20,7 +24,18 @@ function Proc(table) {

var updater = function() {
si.processes(data => {
that.updateData(data);

// If processSearchTerm looks like a regular expression use that to filter
// the process list. Otherwise, just do a simple string match.
if (processSearchTerm.length > 0) {
if (processSearchTerm[0] === "/" && processSearchTerm[processSearchTerm.length - 1] === "/") {
const regex = new RegExp(processSearchTerm.slice(1, -1))
data.list = data.list.filter(p => regex.test(p.name.toLowerCase()) || regex.test(p.command.toLowerCase()))
} else {
data.list = data.list.filter(p => p.name.toLowerCase().includes(processSearchTerm) || p.command.toLowerCase().includes(processSearchTerm))
}
}
that.updateData(data)
});
};
updater();
Expand All @@ -35,6 +50,23 @@ function Proc(table) {
that.reIndex = true;
updater();
});

this.screen.key(['s'], (ch, key) => {
try {
const dialogMessage =
`Enter Search Parameter
(Use regular expressions by wrapping in //)`
let searchPrompt = blessed.prompt({top: "top", left: "center", width: "shrink", height: "shrink", border: { type: "line" }})
screen.append(searchPrompt)
searchPrompt.readInput(dialogMessage, processSearchTerm, (err, obj) => {
processSearchTerm = err ? "" : obj.toString().toLowerCase()
})
updater()
} catch (e) {
console.log(e.message)
consol.error(e)
}
})
}

Proc.prototype.updateData = function(data) {
Expand All @@ -53,7 +85,11 @@ Proc.prototype.updateData = function(data) {
];
});

var headers = ['PID', 'Command', '%CPU', '%MEM'];
let searchedCommand = `Command`
if (processSearchTerm.length > 0) {
searchedCommand = `Command (${processSearchTerm})`
}
var headers = ['PID', searchedCommand, '%CPU', '%MEM'];

headers[
{
Expand Down
35 changes: 33 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"lint-check": "prettier --check --single-quote --trailing-comma es5 'lib/**/*.js'",
"prepublishOnly": "marked-man --version $npm_package_version --manual 'Gtop Help' --section 1 ./CLI.md > gtop.1"
},
"man": ["./gtop.1"],
"man": [
"./gtop.1"
],
"repository": {
"type": "git",
"url": "git+https://github.com/aksakalli/gtop.git"
Expand Down Expand Up @@ -42,6 +44,7 @@
"node": ">=4.0.0"
},
"devDependencies": {
"@types/blessed": "^0.1.19",
"marked-man": "^0.7.0",
"prettier": "1.19.1"
}
Expand Down