Class: PSTree

Inherits:
Object
  • Object
show all
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.

Examples:

Creating and displaying a process tree

tree = PSTree.new($$)
puts tree.to_s

Iterating over processes in the tree

tree = PSTree.new($$)
tree.each do |process|
  puts process.pid
end

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

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

Parameters:

  • root_pid (String, Integer, nil) (defaults to: nil)

    the process ID of the root process

  • charset (String) (defaults to: 'UTF-8')

    the character encoding to use for display



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.

Parameters:

  • last (TrueClass, FalseClass)

    whether this is the last child in a sequence

Returns:

  • (String)

    the character sequence to use for drawing the tree branch line, either UTF-8 or ASCII characters depending on charset configuration



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.

Parameters:

  • last (TrueClass, FalseClass)

    whether this is the last child in a sequence

Returns:

  • (String)

    the character sequence to use for drawing the tree branch line, either UTF-8 or ASCII characters depending on charset configuration



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.

Yields:

  • (ps)

Returns:

  • (PSTree)

    returns itself to allow for method chaining



148
149
150
151
152
# File 'lib/pstree.rb', line 148

def each(&block)
  build
  recurse @root_pid, &block
  self
end

#to_sString

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.

Returns:

  • (String)

    a formatted string representation of the process tree with proper indentation and tree-drawing characters



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