Class: Hackmac::Graph

Inherits:
Object
  • Object
show all
Includes:
Formatters, Term::ANSIColor
Defined in:
lib/hackmac/graph.rb,
lib/hackmac/graph/display.rb

Overview

A class that provides graphical display functionality for terminal-based data visualization

The Graph class enables the creation of dynamic, real-time visualizations of data values within a terminal environment. It manages the rendering of graphical representations such as line charts or graphs, updating them continuously based on provided data sources. The class handles terminal control operations, including cursor positioning, color management, and screen clearing to ensure smooth visual updates. It also supports configuration of display parameters like title, formatting strategies for values, update intervals, and color schemes for different data series.

Examples:

graph = Hackmac::Graph.new(
  title: 'CPU Usage',
  value: ->(i) { rand(100) },
  format_value: :as_percent,
  sleep: 1,
  color: 33
)
graph.start
# Starts the graphical display loop
graph = Hackmac::Graph.new(
  title: 'Memory Usage',
  value: ->(i) { `vm_stat`.match(/Pages free: (\d+)/)[1].to_i },
  format_value: :as_bytes,
  sleep: 2
)
graph.start
# Starts a memory usage graph with custom data source and formatting

Defined Under Namespace

Modules: Formatters Classes: Display

Instance Method Summary collapse

Methods included from Formatters

#as_bytes, #as_celsius, #as_default, #as_hertz, #as_percent, #derive_color_from_string

Constructor Details

#initialize(title:, value: -> i { 0 }, format_value: nil, sleep: nil, color: nil) ⇒ Graph

The initialize method sets up a Graph instance by configuring its display parameters and internal state

This method configures the graph visualization with title, value provider, formatting options, update interval, and color settings. It initializes internal data structures for storing historical values and manages synchronization through a mutex for thread-safe operations.

Parameters:

  • title (String)

    the title to display at the bottom of the graph

  • value (Proc) (defaults to: -> i { 0 })

    a proc that takes an index and returns a numeric value for plotting

  • format_value (Proc, Symbol, nil) (defaults to: nil)

    formatting strategy for displaying values

  • sleep (Numeric) (defaults to: nil)

    time in seconds between updates

  • color (Integer, Proc, nil) (defaults to: nil)

    color index or proc to determine color dynamically

Raises:

  • (ArgumentError)

    if the sleep parameter is negative



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hackmac/graph.rb', line 166

def initialize(
  title:,
  value: -> i { 0 },
  format_value: nil,
  sleep: nil,
  color: nil
)
  sleep >= 0 or raise ArgumentError, 'sleep has to be >= 0'
  @title        = title
  @value        = value
  @format_value = format_value
  @sleep        = sleep
  @continue     = false
  @data         = []
  @color        = color
  @mutex        = Mutex.new
end

Instance Method Details

#startObject

The start method initiates the graphical display process by setting up signal handlers, performing an initial terminal reset, and entering the main update loop

This method serves as the entry point for starting the graph visualization functionality. It configures the necessary signal handlers for graceful shutdown and terminal resizing, performs an initial full reset of the display state, and then begins the continuous loop that updates and renders graphical data.



193
194
195
196
197
# File 'lib/hackmac/graph.rb', line 193

def start
  install_handlers
  full_reset
  start_loop
end

#stopObject

The stop method terminates the graphical display process by performing a full reset and setting the continue flag to false

This method serves as the shutdown mechanism for the graph visualization functionality. It ensures that all display resources are properly cleaned up and the terminal state is restored to its original condition before stopping the continuous update loop.



206
207
208
209
# File 'lib/hackmac/graph.rb', line 206

def stop
  full_reset
  @continue = false
end