Documentation
¶
Overview ¶
Package utils provides utility functions for the Charta graph system. This file specifically handles Braille character mapping for condensed graph mode.
Package utils provides utility functions for the Charta graph system.
This package contains helper functions used across multiple graph types:
- Mathematical operations: Sum, Average, Mod
- Scale calculations: GetMaxScale
- Type conversions: ToSliceOfAny
- Terminal detection: IsTrueColor
- Braille character mapping: GetBraille (in braille.go)
These utilities are designed to be lightweight and focused on specific tasks needed by the graph rendering system.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Average ¶
Average calculates the arithmetic mean of a slice of float64 values. This is a utility function used by graph types that need to compute averages.
Parameters:
- tab: Slice of float64 values to average
Returns:
- avg: The arithmetic mean of the input values
func GetBraille ¶ added in v0.6.0
GetBraille returns the Braille character corresponding to the given array of values. This function is used in condensed graph mode to represent multiple data points within a single character position.
Parameters:
- l: Array of 4 float64 values representing normalized positions (-0.5 to 1)
Returns:
- string: The corresponding Braille Unicode character
Example:
- GetBraille([4]float64{0.5, 0.5, 0.5, 0.5}) returns "⡇"
- GetBraille([4]float64{1, 1, 1, 1}) returns "⣿"
func GetMaxScale ¶
GetMaxScale calculates an appropriate scale maximum for a given value. It determines a "nice" round number that is greater than the absolute value, using multiples of 2, 5, or 10 for better readability on graphs. The function preserves the sign of the input value.
Parameters:
- v: The value to calculate a scale for
Returns:
- int: A rounded scale value (2, 5, 10, 20, 50, 100, etc.) with appropriate sign
Example:
- GetMaxScale(75.0) returns 100
- GetMaxScale(-75.0) returns -100
- GetMaxScale(35.0) returns 50
func IsTrueColor ¶ added in v0.9.0
func IsTrueColor() bool
IsTrueColor checks if the terminal supports 24-bit true color. This is determined by checking the COLORTERM environment variable for "truecolor" or "24bit" values.
True color support enables:
- Smooth color gradients in heatmaps
- RGB color specifications
- Condensed mode with Braille characters
Returns:
- bool: true if the terminal supports 24-bit color, false otherwise
func Mod ¶ added in v0.4.0
Mod calculates the modulo (remainder) of integer division. This function provides a custom modulo implementation that may handle edge cases differently than the built-in % operator.
Parameters:
- a: The dividend (number to be divided)
- b: The divisor (number to divide by)
Returns:
- int: The remainder of a divided by b
Example:
- Mod(10, 3) returns 1
- Mod(15, 5) returns 0
- Mod(7, 4) returns 3
func Sum ¶ added in v0.3.0
Sum calculates the sum of a slice of float64 values efficiently. This optimized version uses direct pointer access and avoids bounds checking where possible for better performance with large slices.
Parameters:
- tab: Slice of float64 values to sum
Returns:
- sum: The arithmetic sum of the input values
func ToSliceOfAny ¶ added in v0.3.0
ToSliceOfAny converts a typed slice to a slice of any (interface{}). This is a generic utility function useful for working with functions that require []any parameters while maintaining type safety in the calling code.
Parameters:
- s: Slice of any type T to convert
Returns:
- []any: A new slice containing the same elements as interface{} values
Example:
- ToSliceOfAny([]int{1, 2, 3}) returns []any{1, 2, 3}
- ToSliceOfAny([]string{"a", "b"}) returns []any{"a", "b"}
Types ¶
This section is empty.