Skip to content

Commit

Permalink
ptop: Enable searching by PID
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Feb 16, 2023
1 parent 30e404a commit 8f52d01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions px/px_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ def match(self, string, require_exact_user=True):
if string in self.cmdline.lower():
return True

if str(self.pid).startswith(string):
return True

return False

def get_command_line_array(self):
Expand Down
9 changes: 8 additions & 1 deletion px/px_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,15 @@ def get_screen_lines(

# Put exact search matches first. Useful for "px cat" or other short
# search strings with tons of hits.
search_pid = -1
try:
search_pid = int(search)
except ValueError:
pass
toplist = sorted(
toplist, key=lambda p: search in (p.command, p.username), reverse=True
toplist,
key=lambda p: search in (p.command, p.username) or p.pid == search_pid,
reverse=True,
)

# Hand out different amount of lines to the different sections
Expand Down
8 changes: 8 additions & 0 deletions tests/px_process_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ def test_match():
assert p.match("air")
assert p.match("play")

# Match PID by prefix but not substring. Exact matches are used for
# searching in ptop. Prefix matching is used to not throw the right answer
# away while the user is typing their search in ptop. Substring matching has
# no value.
assert p.match("47536")
assert p.match("4753")
assert not p.match("7536")


def test_seconds_to_str():
assert px_process.seconds_to_str(0.54321) == "0.54s"
Expand Down

0 comments on commit 8f52d01

Please sign in to comment.