Skip to content
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
11 changes: 10 additions & 1 deletion Src/args.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ArgManager {
var showpid = true
var showpath = true
var toFile: String?
let availableArgs = ["--nocolor", "--timeline", "--timestamps", "-o", "--standard", "--version", "--sources", "--nonetwork", "--nopid", "--nopath"]
var filterParent: String?
let availableArgs = ["--nocolor", "--timeline", "--timestamps", "-o", "--standard", "--version", "--sources", "--nonetwork", "--nopid", "--nopath", "--filter"]

init(suppliedArgs: [String]) {
setArgs(suppliedArgs)
Expand Down Expand Up @@ -57,6 +58,13 @@ class ArgManager {
} else {
toFile = "tree_output.txt"
}
} else if arg == "--filter" {
if args.count > x+1 && !availableArgs.contains(args[x+1]) {
filterParent = args[x+1]
} else {
print("--filter requires a PID or process name argument")
exit(1)
}
} else {
print("Unidentified argument " + arg)
exit(1)
Expand All @@ -75,6 +83,7 @@ class ArgManager {
print("--nopath Print process name only instead of full paths")
print("--version Print the TrueTree version number")
print("-o <filename> Output to file")
print("--filter <pid|name> Show only the subtree rooted at the matching process")
exit(0)
}
}
20 changes: 18 additions & 2 deletions Src/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ guard rootNode != nil else {
exit(1)
}

// Helper: resolve the subtree root when --filter is supplied
func resolveFilterRoot(from root: Node) -> Node {
guard let filter = argManager.filterParent else { return root }
if let pid = Int(filter), let found = root.search(pid: pid) {
return found
}
if let found = root.search(name: filter) {
return found
}
print("No process matching '\(filter)' was found in the tree.")
exit(1)
}


// If standard tree mode is active
if argManager.standardMode {
Expand All @@ -42,7 +55,9 @@ if argManager.standardMode {
// Assign this process as a child
parentNode?.add(child: proc.node)
}
rootNode?.printTree()

let printRoot = resolveFilterRoot(from: rootNode!)
printRoot.printTree()
exit(0)
}

Expand Down Expand Up @@ -93,5 +108,6 @@ for proc in pc.processes {
}

// print the launchd pid and all of it's children
rootNode?.printTree()
let printRoot = resolveFilterRoot(from: rootNode!)
printRoot.printTree()

24 changes: 24 additions & 0 deletions Src/node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ final class Node {
}
return nil
}

func search(pid: Int) -> Node? {
if self.pid == pid {
return self
}
for child in children {
if let found = child.search(pid: pid) {
return found
}
}
return nil
}

func search(name: String) -> Node? {
if fileName.lowercased() == name.lowercased() || path.lowercased().contains(name.lowercased()) {
return self
}
for child in children {
if let found = child.search(name: name) {
return found
}
}
return nil
}
}


Expand Down