Class: PSTree
- Inherits:
-
Object
- Object
- PSTree
- Includes:
- Enumerable
- Defined in:
- lib/pstree.rb,
lib/pstree/version.rb
Overview
PSTree is a process status tree implementation that creates and displays hierarchical process information.
This class provides functionality to build and visualize process trees starting from a specified root process ID. It encapsulates process data including parent process ID, process ID, user, and command line, and formats this information into a readable tree structure with proper indentation and branch characters.
Defined Under Namespace
Classes: ProcStruct
Constant Summary collapse
- VERSION =
PSTree version
'0.5.0'- VERSION_ARRAY =
:nodoc:
VERSION.split('.').map(&:to_i)
- VERSION_MAJOR =
:nodoc:
VERSION_ARRAY[0]
- VERSION_MINOR =
:nodoc:
VERSION_ARRAY[1]
- VERSION_BUILD =
:nodoc:
VERSION_ARRAY[2]
Instance Method Summary collapse
-
#children_not_zero(last) ⇒ String
Returns the appropriate tree drawing character sequence for a process with children, based on the current charset setting and whether the process is the last child in a sequence.
-
#children_zero(last) ⇒ String
Returns the appropriate tree drawing character sequence for a process with zero children, based on the current charset setting.
-
#each {|ps| ... } ⇒ PSTree
The each method iterates over all processes in the process tree by executing the given block for each process.
-
#initialize(root_pid = nil, charset: 'UTF-8') ⇒ PSTree
constructor
Initializes a new PSTree instance with the specified root process ID and character set.
-
#to_s ⇒ String
The to_s method generates a string representation of the process tree starting from the root process ID.
Constructor Details
#initialize(root_pid = nil, charset: 'UTF-8') ⇒ PSTree
Initializes a new PSTree instance with the specified root process ID and character set.
to start the tree from formatting
79 80 81 82 |
# File 'lib/pstree.rb', line 79 def initialize(root_pid = nil, charset: 'UTF-8') @charset = charset.to_s.upcase @root_pid = root_pid.to_i end |
Instance Method Details
#children_not_zero(last) ⇒ String
Returns the appropriate tree drawing character sequence for a process with children, based on the current charset setting and whether the process is the last child in a sequence.
110 111 112 113 114 115 116 |
# File 'lib/pstree.rb', line 110 def children_not_zero(last) if @charset == 'UTF-8' last ? '├─ ' : '│ ' else last ? '+- ' : '| ' end end |
#children_zero(last) ⇒ String
Returns the appropriate tree drawing character sequence for a process with zero children, based on the current charset setting.
93 94 95 96 97 98 99 |
# File 'lib/pstree.rb', line 93 def children_zero(last) if @charset == 'UTF-8' last ? '└─ ' : ' ' else last ? '`- ' : ' ' end end |
#each {|ps| ... } ⇒ PSTree
The each method iterates over all processes in the process tree by executing the given block for each process.
148 149 150 151 152 |
# File 'lib/pstree.rb', line 148 def each(&block) build recurse @root_pid, &block self end |
#to_s ⇒ String
The to_s method generates a string representation of the process tree starting from the root process ID. It builds the tree structure and formats each process entry with appropriate tree-drawing characters based on the process hierarchy and charset configuration. The resulting string includes process IDs, commands, and users in a hierarchical display format.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/pstree.rb', line 126 def to_s build result = '' recurse @root_pid, -> children, last { if children.zero? result << children_zero(last) else result << children_not_zero(last) end } do |ps| result << ps.to_s << "\n" end result end |