byteme 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/byteme +96 -0
  3. metadata +59 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab9e848c7658d84ebf411158e1129cce64a49fab
4
+ data.tar.gz: ad7f7791f6df1ef3fe2bd807413507be24cd356f
5
+ SHA512:
6
+ metadata.gz: 11c858df738ceb19f4eff32793a80b89621e4ac955049e07edc0d30d96abf9b4e5645a28be0a9c9ab948945314fd81e4205bbcf6313095862add8167e22f26dc
7
+ data.tar.gz: 69996d78fa237892d9eb6b1a2069a44152a495f89bffb726014ac516a4407a0a59e95fff3817db7a0e61440b8c2c493f7d7e29f3bbb0868c2442354f08db953c
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env ruby
2
+ require 'terminal-table'
3
+
4
+ @usage = <<-HEREDOC
5
+ Usage: <input value> <input format>
6
+ Optional -r / --round flag to print output as rounded floats instead of in scientific notation.
7
+ Input/Output format can be:
8
+
9
+ B: Bytes
10
+ KB: Kilobytes
11
+ MB: Megabytes
12
+ GB: Gigabytes
13
+ TB: Terabytes
14
+ PB: Petabytes
15
+ b: bits
16
+ kb: kilobits
17
+ mb: megabits
18
+ gb: gigabits
19
+ tb: terabits
20
+ pb: petabits
21
+
22
+ Examples:
23
+ $ byteme 1000
24
+ $ byteme 1000 GB
25
+ $ byteme 1000 kb -r
26
+ HEREDOC
27
+
28
+ def convert(input, input_format)
29
+ formulas = {
30
+ B: [1, 'Bytes'],
31
+ KB: [10**3, 'Kilobytes'],
32
+ MB: [10**6, 'Megabytes'],
33
+ GB: [10**9, 'Gigabytes'],
34
+ TB: [10**12, 'Terabytes'],
35
+ PB: [10**15, 'Petabytes'],
36
+ b: [0.125, 'bits'],
37
+ kb: [8*10**3, 'kilobits'],
38
+ mb: [8*10**6, 'megabits'],
39
+ gb: [8*10**9, 'gigabits'],
40
+ tb: [8*10**12, 'terabits'],
41
+ pb: [8*10**15, 'petabits']
42
+ }
43
+
44
+ # Sanatization
45
+ if !formulas.keys.include?(input_format.to_sym)
46
+ puts "Error: input format (#{input_format}) is incorrect."
47
+ puts @usage
48
+ return
49
+ end
50
+
51
+ rows = []
52
+ formulas.keys.each do |key|
53
+ numerator = formulas[input_format.to_sym][0]
54
+ raw_value = input.to_f * numerator / formulas[key.to_sym][0]
55
+ if @round
56
+ output = ( "%.5f" % raw_value ).sub(/\.?0*$/, '')
57
+ else
58
+ output = raw_value
59
+ end
60
+ output_format = formulas[key.to_sym][1]
61
+ rows.push([output_format, output])
62
+ end
63
+
64
+ table = Terminal::Table.new :headings => ['Type', 'Value'], :rows => rows
65
+ # table.style = {:all_separators => true}
66
+ puts table
67
+ end
68
+
69
+ # Command Parsing
70
+ if ARGV.length < 1
71
+ puts @usage
72
+ exit
73
+ end
74
+
75
+ @round = false
76
+ if ARGV.include?('-r') | ARGV.include?('--r')
77
+ @round = true
78
+ end
79
+
80
+ input = ARGV[0]
81
+
82
+ if !ARGV[1]
83
+ input_format = 'B'
84
+ elsif ARGV[1][0] == '-'
85
+ input_format = 'B'
86
+ else
87
+ input_format = ARGV[1]
88
+ end
89
+
90
+ convert(input, input_format)
91
+
92
+ =begin
93
+ References:
94
+ https://www.eagle-web-designs.com/cool_stuff/ByteConversion.html
95
+ https://bundler.io/v1.17/guides/creating_gem.html
96
+ =end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: byteme
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Perlman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: terminal-table
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
27
+ description: Convert between bytes, kilobytes, megabytes, etc.
28
+ email: thumbthrough@gmail.com
29
+ executables:
30
+ - byteme
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/byteme
35
+ homepage: ''
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.6.12
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Convert between bytes, kilobytes, megabytes, etc.
59
+ test_files: []