linux_stat 0.6.3 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,46 @@ rescue LoadError
7
7
  abort "The Gem needs to be installed before this test can be run!"
8
8
  end
9
9
 
10
+ # Gradient colour to strings
11
+ class String
12
+ def colourize(colour = 1, flip: false)
13
+ colours, line_length = [], -1
14
+ temp = ''
15
+
16
+ each_line do |c|
17
+ n, i = c.length, -1
18
+
19
+ if line_length != n
20
+ step, line_length = 255.0./(n), n
21
+ colours.clear
22
+
23
+ while (i += 1) < n
24
+ l = i.*(step)
25
+ colours.<<(
26
+ case colour
27
+ when 0 then [ l.*(2).to_i.clamp(0, 255), l.to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255) ]
28
+ when 1 then [ 255, 255.-(l).to_i.clamp(0, 255), l.to_i.clamp(0, 255) ]
29
+ when 2 then [ l.to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255), l.to_i.clamp(0, 255) ]
30
+ when 3 then [ l.*(2).to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255), 100.+(l / 2).to_i.clamp(0, 255) ]
31
+ when 4 then [ 30, 255.-(l / 2).to_i.clamp(0, 255), 110.+(l / 2).to_i.clamp(0, 255) ]
32
+ when 5 then [ 255.-(l * 2).to_i.clamp(0, 255), l.to_i.clamp(0, 255), 200 ]
33
+ when 6 then [ 50.+(255 - l).to_i.clamp(0, 255), 255.-(l / 2).to_i.clamp(0, 255), (l * 2).to_i.clamp(0, 255) ]
34
+ else [ l.*(2).to_i.clamp(0, 255), 255.-(l).to_i.clamp(0, 255), 100.+(l / 2).to_i.clamp(0, 255) ]
35
+ end
36
+ )
37
+ end
38
+
39
+ colours.reverse! if flip
40
+ end
41
+
42
+ i = -1
43
+ temp.concat "\e[38;2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}" while (i += 1) < n
44
+ end
45
+
46
+ temp << "\e[0m".freeze
47
+ end
48
+ end
49
+
10
50
  # Check which conflicting argument (e.g., `-md -html` together) is passed last
11
51
  # Always use the last argument
12
52
  conflicting, hash = [
@@ -54,7 +94,6 @@ execute.sort.each do |c|
54
94
  next if e.class != Module && e.class != Class
55
95
 
56
96
  meths = e.methods(false).sort
57
- next if meths.any? { |a| e.method(a).arity > 0 }
58
97
 
59
98
  if meths.length > 0
60
99
  if MARKDOWN
@@ -67,20 +106,68 @@ execute.sort.each do |c|
67
106
  end
68
107
 
69
108
  meths.each do |meth|
109
+ arg = nil
110
+ params = e.method(meth).parameters
111
+
112
+ param = ''
113
+
114
+ params.each do |p|
115
+ case p[0]
116
+ when :opt
117
+ param << "#{p[1]}, "
118
+ when :key
119
+ param << "#{p[1]}:, "
120
+ when :req
121
+ param << "#{p[1] || 'arg'}, "
122
+ end
123
+ end
124
+
125
+ param.delete_suffix!(", ")
126
+
127
+ if e.method(meth).arity > 0
128
+ if c == :PrettifyBytes
129
+ arg = rand(10 ** 15)
130
+ elsif c == :FS
131
+ arg = '/'
132
+ else
133
+ next
134
+ end
135
+ end
136
+
137
+ disp_meth = "#{meth}"
138
+ disp_meth.concat(arg ? "(#{param} = #{arg.inspect})" : "(#{param})")
139
+
70
140
  time = Time.now
71
- v = e.send(meth)
141
+ v = arg ? e.send(meth, arg) : e.send(meth)
72
142
  time2 = Time.now
73
143
  time = time2.-(time).*(1_000_000).round(3)
74
144
 
75
145
  v = v.inspect
76
146
  dis = v.length > 253 ? v[0..250].strip + '...'.freeze : v
77
147
 
148
+ source = e.singleton_method(meth).source_location.to_a
149
+ src, src_meth = '', ''
150
+
151
+ unless source.empty?
152
+ src << " File: #{File.split(source[0])[-1]} | Line: #{source[1]}\n"
153
+ src_meth << " Definition: #{IO.foreach(source[0]).first(source[1])[-1].strip}\n"
154
+
155
+ if MARKDOWN || HTML
156
+ src.prepend('#'.freeze)
157
+ src_meth.prepend('#'.freeze)
158
+ else
159
+ src.prepend(?\u2B23.freeze)
160
+ src_meth.prepend(?\u2B23.freeze)
161
+ end
162
+ end
163
+
164
+
78
165
  if MARKDOWN
79
- puts "#{e}.#{meth}\n=> #{dis}"
166
+ puts "#{src}#{src_meth}#{e}.#{disp_meth}\n=> #{dis}"
80
167
  elsif HTML
81
- puts "#{e}.#{meth}\n=> #{dis}"
168
+ puts "#{src}#{src_meth}#{e}.#{disp_meth}\n=> #{dis}"
82
169
  else
83
- puts "\e[1;38;2;80;80;255m#{e}.#{meth}\e[0m\n=> #{dis}"
170
+ puts "\e[1m#{src.colourize}\e[1m#{src_meth.colourize(4)}\e[0m\e[1;38;2;80;80;255m#{e}.#{disp_meth}\e[0m\n=> #{dis}"
84
171
  end
85
172
 
86
173
  puts( "(" +
@@ -1,6 +1,10 @@
1
1
  require 'mkmf'
2
2
 
3
- unless (have_header('sys/statvfs.h') && have_header('ruby.h'))
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/statvfs.h') && have_header('ruby.h')
4
8
  abort('Missing header')
5
9
  end
6
10
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  #pragma GCC optimize ("O3")
5
5
  #pragma clang optimize on
6
- #pragma once
7
6
 
8
7
  static VALUE statfs(VALUE obj, VALUE dir) {
9
8
  struct statvfs buf ;
@@ -1,6 +1,10 @@
1
1
  require 'mkmf'
2
2
 
3
- unless (have_header('sys/unistd.h') && have_header('ruby.h'))
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/unistd.h') && have_header('ruby.h')
4
8
  abort('Missing header')
5
9
  end
6
10
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  #pragma GCC optimize ("O3")
5
5
  #pragma clang optimize on
6
- #pragma once
7
6
 
8
7
  static VALUE getTick(VALUE obj) {
9
8
  return INT2FIX(sysconf(_SC_CLK_TCK)) ;
@@ -1,6 +1,10 @@
1
1
  require 'mkmf'
2
2
 
3
- unless (have_header('sys/utsname.h') && have_header('ruby.h'))
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/utsname.h') && have_header('ruby.h')
4
8
  abort('Missing header')
5
9
  end
6
10
 
@@ -3,7 +3,6 @@
3
3
 
4
4
  #pragma GCC optimize ("O3")
5
5
  #pragma clang optimize on
6
- #pragma once
7
6
 
8
7
  static struct utsname buf ;
9
8
  static short status ;
@@ -3,12 +3,15 @@ module LinuxStat
3
3
  PATH = "/sys/class/power_supply/BAT0"
4
4
 
5
5
  class << self
6
+ ##
6
7
  # Returns true or false based on the presence of the battery.
7
8
  def present?
8
9
  @@present ||= Dir.exist?(PATH)
9
10
  end
10
11
 
12
+ ##
11
13
  # Returns the details of the battery.
14
+ #
12
15
  # If the battery is not present it will return an empty Hash.
13
16
  def stat
14
17
  st = status.downcase
@@ -26,57 +29,73 @@ module LinuxStat
26
29
  }
27
30
  end
28
31
 
32
+ ##
29
33
  # Returns the model of the battery.
34
+ #
30
35
  # If the battery is not present or the information isn't available it will return an empty String.
31
36
  def model
32
37
  return ''.freeze unless model_readable?
33
38
  IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
34
39
  end
35
40
 
41
+ ##
36
42
  # Returns the manufacturer of the battery.
43
+ #
37
44
  # If the battery is not present or the information is not available, it will return an empty String.
38
45
  def manufacturer
39
46
  return ''.freeze unless manufacturer_readable?
40
47
  IO.read(File.join(PATH, 'manufacturer')).tap(&:strip!)
41
48
  end
42
49
 
50
+ ##
43
51
  # Returns the technology of the battery.
52
+ #
44
53
  # If the battery is not present or the information is not available, it will return an empty String.
45
54
  def technology
46
55
  return ''.freeze unless tech_readable?
47
56
  IO.read(File.join(PATH, 'technology')).tap(&:strip!)
48
57
  end
49
58
 
59
+ ##
50
60
  # Returns the status of the battery.
51
- # If the battery is not present or the information is not available, it will return an empty String.
52
61
  # The status generally includes either of the full, charging, discharging and unknown states in most cases.
62
+ #
63
+ # If the battery is not present or the information is not available, it will return an empty frozen String.
53
64
  def status
54
65
  return ''.freeze unless status_readable?
55
66
  IO.read(File.join(PATH, 'status')).tap(&:strip!)
56
67
  end
57
68
 
69
+ ##
58
70
  # Returns true if the battery is charging, false if the battery is not charging.
71
+ #
59
72
  # If the battery is not present or the information is not available, it will return nil.
60
73
  def charging?
61
74
  return nil if status.empty?
62
75
  %w(full charging unknown).each(&:freeze).include?(status.downcase)
63
76
  end
64
77
 
78
+ ##
65
79
  # Returns true if the battery is discharging, false if the battery is not discharging.
80
+ #
66
81
  # If the battery is not present or the information is not available, it will return nil.
67
82
  def discharging?
68
83
  return nil if status.empty?
69
84
  status.downcase == 'discharging'
70
85
  end
71
86
 
87
+ ##
72
88
  # Returns true if the battery status if full, false if the battery status is not full.
89
+ #
73
90
  # If the battery is not present or the information is not available, it will return nil.
74
91
  def full?
75
92
  return nil if status.empty?
76
93
  status.downcase == 'full'
77
94
  end
78
95
 
96
+ ##
79
97
  # Returns the charge of the battery.
98
+ #
80
99
  # If the battery is not present or the information is not available, it will return nil.
81
100
  def charge
82
101
  return nil unless charge_now_readable?
@@ -1,7 +1,9 @@
1
1
  module LinuxStat
2
2
  module BIOS
3
3
  class << self
4
+ ##
4
5
  # Returns the model of the BIOS.
6
+ #
5
7
  # If the information is not available it will return a frozen empty string.
6
8
  #
7
9
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -16,7 +18,9 @@ module LinuxStat
16
18
  end
17
19
  end
18
20
 
21
+ ##
19
22
  # Returns the vendor of the BIOS.
23
+ #
20
24
  # If the information is not available it will return a frozen empty string.
21
25
  #
22
26
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -29,7 +33,9 @@ module LinuxStat
29
33
  end
30
34
  end
31
35
 
36
+ ##
32
37
  # Returns the version of the BIOS.
38
+ #
33
39
  # If the information is not available it will return a frozen empty string.
34
40
  #
35
41
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -41,7 +47,9 @@ module LinuxStat
41
47
  end
42
48
  end
43
49
 
50
+ ##
44
51
  # Returns the date of the BIOS.
52
+ #
45
53
  # If the information is not available it will return a frozen empty string.
46
54
  #
47
55
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -1,16 +1,21 @@
1
1
  module LinuxStat
2
2
  module CPU
3
3
  class << self
4
- # stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
4
+ ##
5
+ # = stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
6
+ #
5
7
  # Where sleep is the delay to gather the data.
8
+ #
6
9
  # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
10
+ #
7
11
  # This method returns the cpu usage of all threads.
8
12
  #
9
13
  # The first one is aggregated CPU usage reported by the Linux kernel.
14
+ #
10
15
  # And the consecutive ones are the real core usages.
11
16
  #
12
17
  # On a system with 4 threads, the output will be like::
13
- # {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
18
+ # {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
14
19
  #
15
20
  # If the information is not available, it will return an empty Hash
16
21
  def stat(sleep = ticks_to_ms)
@@ -43,9 +48,13 @@ module LinuxStat
43
48
  end
44
49
  end
45
50
 
46
- # total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
51
+ ##
52
+ # = total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
53
+ #
47
54
  # Where sleep is the delay to gather the data.
55
+ #
48
56
  # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
57
+ #
49
58
  # This method returns the cpu usage of all threads.
50
59
  #
51
60
  # It's like running LinuxStat::CPU.stat[0] but it's much more efficient and calculates just the aggregated usage which is available at the top of the /proc/stat file.
@@ -66,14 +75,18 @@ module LinuxStat
66
75
  totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
67
76
  end
68
77
 
78
+ ##
69
79
  # Returns the total number of CPU threads.
80
+ #
70
81
  # If the information isn't available, it will return 0.
71
82
  def count
72
83
  # CPU count can change during the program runtime
73
84
  cpuinfo.count { |x| x.start_with?('processor') }
74
85
  end
75
86
 
87
+ ##
76
88
  # Returns the model of processor.
89
+ #
77
90
  # If the information isn't available, it will return en empty string.
78
91
  #
79
92
  # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
@@ -81,7 +94,9 @@ module LinuxStat
81
94
  @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
82
95
  end
83
96
 
97
+ ##
84
98
  # Returns an array with current core frequencies corresponding to the usages.
99
+ #
85
100
  # If the information isn't available, it will return an empty array.
86
101
  def cur_freq
87
102
  @@cpu_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_cur_freq"]
@@ -94,7 +109,9 @@ module LinuxStat
94
109
  end
95
110
  end
96
111
 
112
+ ##
97
113
  # Returns an array with max core frequencies corresponding to the usages.
114
+ #
98
115
  # If the information isn't available, it will return an empty array.
99
116
  def max_freq
100
117
  @@max_freqs ||= Dir["/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq"]
@@ -1,13 +1,16 @@
1
1
  module LinuxStat
2
2
  module Filesystem
3
3
  class << self
4
- # stat(fs = '/')
4
+ ##
5
+ # = stat(fs = '/')
6
+ #
5
7
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
6
8
  #
7
- # It returns a Hash with the following info:
8
- # 1. total size of the device (in bytes)
9
- # 2. free space (in kilobytes)
10
- # 3. used space (in kilobytes)
9
+ # * It returns a Hash with the following info:
10
+ #
11
+ # 1. total size of the device (in bytes)
12
+ # 2. free space (in kilobytes)
13
+ # 3. used space (in kilobytes)
11
14
  #
12
15
  # In a hash format:
13
16
  # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
@@ -25,8 +28,11 @@ module LinuxStat
25
28
  }
26
29
  end
27
30
 
28
- # total(fs = '/')
31
+ ##
32
+ # = total(fs = '/')
33
+ #
29
34
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
35
+ #
30
36
  # It returns the total size of a given disk in bytes.
31
37
  #
32
38
  # If the stat can't be acquired, this method will return nil.
@@ -37,10 +43,15 @@ module LinuxStat
37
43
  s[:block_size] * s[:blocks]
38
44
  end
39
45
 
40
- # free(fs = '/')
46
+ ##
47
+ # = free(fs = '/')
48
+ #
41
49
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
50
+ #
42
51
  # It returns the total free space in a disk in bytes.
52
+ #
43
53
  # It is to be noted that free is not same as available.
54
+ #
44
55
  # Free returns the size of free blocks.
45
56
  #
46
57
  # If the stat can't be acquired, this method will return an empty Hash.
@@ -51,8 +62,11 @@ module LinuxStat
51
62
  s[:block_size] * s[:block_free]
52
63
  end
53
64
 
54
- # used(fs = '/')
65
+ ##
66
+ # = used(fs = '/')
67
+ #
55
68
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
69
+ #
56
70
  # It returns the used space of a given disk in bytes.
57
71
  #
58
72
  # If the stat can't be acquired, this method will return nil.
@@ -63,10 +77,15 @@ module LinuxStat
63
77
  s[:blocks].-(s[:block_free]) * s[:block_size]
64
78
  end
65
79
 
66
- # available(fs = '/')
80
+ ##
81
+ # = available(fs = '/')
82
+ #
67
83
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
84
+ #
68
85
  # It returns the total free space in a disk in bytes.
86
+ #
69
87
  # It is to be noted that free is not same as available.
88
+ #
70
89
  # Available returns the size of free blocks for unpriviledged users.
71
90
  #
72
91
  # If the stat can't be acquired, this method will return an empty Hash.
@@ -77,7 +96,9 @@ module LinuxStat
77
96
  s[:block_size] * s[:block_avail_unpriv]
78
97
  end
79
98
 
80
- # stat_raw(fs = '/')
99
+ ##
100
+ # = stat_raw(fs = '/')
101
+ #
81
102
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
82
103
  #
83
104
  # It returns a Hash with the following data (for example):