Class: IStats::Printer

Inherits:
Object
  • Object
show all
Extended by:
Color
Defined in:
lib/iStats/printer.rb

Constant Summary collapse

LABEL_WIDTH =
24
VALUE_WIDTH =
8
SCALE_WIDTH =
4

Constants included from Color

Color::CODES

Class Method Summary collapse

Methods included from Color

colorize, included

Class Method Details

.disable_graphsObject



15
16
17
# File 'lib/iStats/printer.rb', line 15

def disable_graphs
  @display_graphs = false
end

.disable_labelsObject



19
20
21
# File 'lib/iStats/printer.rb', line 19

def disable_labels
  @display_labels = false
end

.disable_scaleObject



23
24
25
# File 'lib/iStats/printer.rb', line 23

def disable_scale
  @display_scale = false
end

.format_label(label) ⇒ Object



105
106
107
108
109
# File 'lib/iStats/printer.rb', line 105

def format_label(label)
  if @display_labels == true
    "#{label}\t"
  end
end

.format_scale(scale) ⇒ Object



111
112
113
114
115
# File 'lib/iStats/printer.rb', line 111

def format_scale(scale)
  if @display_scale == true
    "#{scale}"
  end
end

.format_temperature(temperature) ⇒ Object

Pretty print temperature values. Returns the formatted temperature string.



135
136
137
138
# File 'lib/iStats/printer.rb', line 135

def format_temperature(temperature)
    value, scale = Printer.format_temperature(temperature)
    "#{value}#{scale}"
end

.gen_sparkline(value, thresholds) ⇒ Object

Create colored sparkline value - The stat value thresholds - must be an array of size 4 containing the threshold values

for the sparkline colors

If the values in the thresholds array are descending, treat 100& as good (green) instead of bad (red)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/iStats/printer.rb', line 43

def gen_sparkline(value, thresholds)
  # Graphs can be disabled globally
  return '' unless @display_graphs

  return if thresholds.count < 4

  value = value.to_f

  list = [0, 30, 55, 80, 100, 130]
  sparkline = Sparkr.sparkline(list) do |tick, count, index|
    if thresholds[3] > thresholds[0]
      #
      # Normal sparkline where 100% is bad
      #
      if index.between?(0, 5) and value > thresholds[3]
        flash_red(tick)
      elsif index.between?(0, 1)
        green(tick)
      elsif index.between?(2, 3) and value > thresholds[0]
        light_yellow(tick)
      elsif index == 4 and value > thresholds[1]
        yellow(tick)
      elsif index == 5 and value > thresholds[2]
        red(tick)
      else
        tick
      end
    else
      #
      # Reversed sparkline where 100% is good
      #
      if value < thresholds[3]
        if index == 1
          red(tick)
        else
          tick
        end
      elsif value < thresholds[2]
        if index.between?(0, 2)
          yellow(tick)
        else
          tick
        end
      elsif value < thresholds[1]
        if index.between?(0, 3)
          light_yellow(tick)
        else
          tick
        end
      elsif value < thresholds[0]
        if index.between?(0, 4)
          green(tick)
        else
          tick
        end
      else
        green(tick)
      end
    end
  end
end

.get_temperature_scaleObject



27
28
29
# File 'lib/iStats/printer.rb', line 27

def get_temperature_scale
  @temperature_scale
end

.parse_temperature(temperature) ⇒ Object

Converts the value to the class temperature_scale with accompanying scale string.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/iStats/printer.rb', line 120

def parse_temperature(temperature)
  if @temperature_scale == 'celcius'
    value = temperature
    symbol = "C"
  else
    value = Utils.to_fahrenheit(temperature)
    symbol = "F"
  end

  return value.round(2), "#{Symbols.degree}#{symbol}"
end

Prints a standard item line with label, value, scale, and sparkline as determined by the runtime command-line arguments supplied by the user.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/iStats/printer.rb', line 144

def print_item_line(label, value, scale="", thresholds=[], suffix="")

  if @display_scale
    full_value_width = VALUE_WIDTH + SCALE_WIDTH
    full_value = value.to_s + scale.to_s
  else
    full_value_width = VALUE_WIDTH
    full_value = value
  end

  if @display_labels
    format = "%-"+LABEL_WIDTH.to_s + "s"
    printf("%-" + LABEL_WIDTH.to_s + "s", label + ":")
  end

  printf("%-" + full_value_width.to_s + "s", full_value)

  if @display_graphs
    print "#{Printer.gen_sparkline(value, thresholds)}"
  end

  if @display_labels && suffix != ""
    print "  #{suffix}"
  end

  printf "\n"
end

.set_temperature_scale(scale) ⇒ Object



31
32
33
# File 'lib/iStats/printer.rb', line 31

def set_temperature_scale(scale)
  @temperature_scale = scale
end