byteme 0.0.1 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -0
- data/LICENSE +7 -0
- data/README.md +65 -0
- data/bin/byteme +3 -94
- data/lib/byteme.rb +4 -0
- data/lib/byteme/convert.rb +42 -0
- data/lib/byteme/parse.rb +29 -0
- data/lib/byteme/usage.rb +25 -0
- data/lib/byteme/version.rb +3 -0
- metadata +13 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6ab4b8aff36a0b16d4d6f5cd49211e529d89c98
|
4
|
+
data.tar.gz: fd675a23ddc96a31df110a657336d05e318b5496
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79491b2a2319b077f0c72a4df781c6521d6f332c0b4ede9848c3c516e970667558d8663a8c561084fa5b1acbb12953ba7109eaf113640500c933540ee67d3065
|
7
|
+
data.tar.gz: c8f2a12fa5c03aff44916bd53aaf102a0867a994546cf41affee7d993fac70045eb5da8e5204176a4a2db282db4369a2adfbc4381d7b76a6782e857387d99316
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright <YEAR> <COPYRIGHT HOLDER>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# ByteMe
|
2
|
+
A simple CLI for converting between bytes, kilobytes, megabytes, etc.
|
3
|
+
Can you just Google it? Yes. Is leaving terminal for newbs? Maybe.
|
4
|
+
|
5
|
+
![image](gingerbread.jpg)
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
`gem install byteme`
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
`byteme <input value> <input format>`
|
15
|
+
|
16
|
+
Note: if no input format is specified, ByteMe assumes that the input format is Bytes.
|
17
|
+
|
18
|
+
Optional `-r / --round` flag to print output as rounded floats instead of in scientific notation.
|
19
|
+
|
20
|
+
Input/Output format can be:
|
21
|
+
|
22
|
+
| Command Argument | Corresponds To |
|
23
|
+
| ---------------- | -------------- |
|
24
|
+
| B | Bytes |
|
25
|
+
| KB | Kilobytes |
|
26
|
+
| MB | Megabytes |
|
27
|
+
| GB | Gigabytes |
|
28
|
+
| TB | Terabytes |
|
29
|
+
| PB | Petabytes |
|
30
|
+
| b | bits |
|
31
|
+
| kb | kilobits |
|
32
|
+
| mb | megabits |
|
33
|
+
| gb | gigabits |
|
34
|
+
| tb | terabits |
|
35
|
+
| pb | petabits |
|
36
|
+
|
37
|
+
## Examples:
|
38
|
+
`$ byteme 1000 `
|
39
|
+
|
40
|
+
`$ byteme 1000 GB`
|
41
|
+
|
42
|
+
`$ byteme 1000 kb -r`
|
43
|
+
|
44
|
+
## Example output:
|
45
|
+
|
46
|
+
``` ./bin/byteme 1000
|
47
|
+
+-----------+----------+
|
48
|
+
| Type | Value |
|
49
|
+
+-----------+----------+
|
50
|
+
| Bytes | 1000.0 |
|
51
|
+
| Kilobytes | 1.0 |
|
52
|
+
| Megabytes | 0.001 |
|
53
|
+
| Gigabytes | 1.0e-06 |
|
54
|
+
| Terabytes | 1.0e-09 |
|
55
|
+
| Petabytes | 1.0e-12 |
|
56
|
+
| bits | 8000.0 |
|
57
|
+
| kilobits | 0.125 |
|
58
|
+
| megabits | 0.000125 |
|
59
|
+
| gigabits | 1.25e-07 |
|
60
|
+
| terabits | 1.25e-10 |
|
61
|
+
| petabits | 1.25e-13 |
|
62
|
+
+-----------+----------+
|
63
|
+
```
|
64
|
+
|
65
|
+
PRs are welcome!
|
data/bin/byteme
CHANGED
@@ -1,96 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require 'terminal-table'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
3
|
+
$:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
4
|
+
require 'byteme'
|
5
|
+
Parse.parse
|
data/lib/byteme.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module Convert
|
2
|
+
def self.convert(input, input_format)
|
3
|
+
formulas = {
|
4
|
+
B: [1, 'Bytes'],
|
5
|
+
KB: [10**3, 'Kilobytes'],
|
6
|
+
MB: [10**6, 'Megabytes'],
|
7
|
+
GB: [10**9, 'Gigabytes'],
|
8
|
+
TB: [10**12, 'Terabytes'],
|
9
|
+
PB: [10**15, 'Petabytes'],
|
10
|
+
b: [0.125, 'bits'],
|
11
|
+
kb: [8*10**3, 'kilobits'],
|
12
|
+
mb: [8*10**6, 'megabits'],
|
13
|
+
gb: [8*10**9, 'gigabits'],
|
14
|
+
tb: [8*10**12, 'terabits'],
|
15
|
+
pb: [8*10**15, 'petabits']
|
16
|
+
}
|
17
|
+
|
18
|
+
# Sanitize input
|
19
|
+
if !formulas.keys.include?(input_format.to_sym)
|
20
|
+
puts "Error: input format (#{input_format}) is incorrect."
|
21
|
+
puts @usage
|
22
|
+
return
|
23
|
+
end
|
24
|
+
|
25
|
+
rows = []
|
26
|
+
formulas.keys.each do |key|
|
27
|
+
numerator = formulas[input_format.to_sym][0]
|
28
|
+
raw_value = input.to_f * numerator / formulas[key.to_sym][0]
|
29
|
+
if Parse.get
|
30
|
+
output = ( "%.5f" % raw_value ).sub(/\.?0*$/, '')
|
31
|
+
else
|
32
|
+
output = raw_value
|
33
|
+
end
|
34
|
+
output_format = formulas[key.to_sym][1]
|
35
|
+
rows.push([output_format, output])
|
36
|
+
end
|
37
|
+
|
38
|
+
table = Terminal::Table.new :headings => ['Type', 'Value'], :rows => rows
|
39
|
+
# table.style = {:all_separators => true}
|
40
|
+
puts table
|
41
|
+
end
|
42
|
+
end
|
data/lib/byteme/parse.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Parse
|
2
|
+
def self.parse
|
3
|
+
if ARGV.length < 1 || ARGV.length > 4
|
4
|
+
puts Usage::USAGE
|
5
|
+
return
|
6
|
+
end
|
7
|
+
|
8
|
+
if ARGV[0]
|
9
|
+
if ARGV[0] == 'help'
|
10
|
+
puts Usage::USAGE
|
11
|
+
return
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
@@round = false
|
16
|
+
if ARGV.include?('-r') | ARGV.include?('--round')
|
17
|
+
@@round = true
|
18
|
+
ARGV.pop
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get
|
22
|
+
@@round
|
23
|
+
end
|
24
|
+
|
25
|
+
input = ARGV[0]
|
26
|
+
input_format = ARGV[1] || 'B'
|
27
|
+
Convert.convert(input, input_format)
|
28
|
+
end
|
29
|
+
end
|
data/lib/byteme/usage.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Usage
|
2
|
+
USAGE = <<-HEREDOC
|
3
|
+
Usage: <input value> <input format>
|
4
|
+
Optional -r / --round flag to print output as rounded floats instead of in scientific notation.
|
5
|
+
Input/Output format can be:
|
6
|
+
|
7
|
+
B: Bytes
|
8
|
+
KB: Kilobytes
|
9
|
+
MB: Megabytes
|
10
|
+
GB: Gigabytes
|
11
|
+
TB: Terabytes
|
12
|
+
PB: Petabytes
|
13
|
+
b: bits
|
14
|
+
kb: kilobits
|
15
|
+
mb: megabits
|
16
|
+
gb: gigabits
|
17
|
+
tb: terabits
|
18
|
+
pb: petabits
|
19
|
+
|
20
|
+
Examples:
|
21
|
+
$ byteme 1000
|
22
|
+
$ byteme 1000 GB
|
23
|
+
$ byteme 1000 kb -r
|
24
|
+
HEREDOC
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: byteme
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Perlman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: terminal-table
|
@@ -24,15 +24,23 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.8.0
|
27
|
-
description:
|
27
|
+
description: A simple CLI that converts between bytes, kilobytes, megabytes, etc.
|
28
28
|
email: thumbthrough@gmail.com
|
29
29
|
executables:
|
30
30
|
- byteme
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
34
37
|
- bin/byteme
|
35
|
-
|
38
|
+
- lib/byteme.rb
|
39
|
+
- lib/byteme/convert.rb
|
40
|
+
- lib/byteme/parse.rb
|
41
|
+
- lib/byteme/usage.rb
|
42
|
+
- lib/byteme/version.rb
|
43
|
+
homepage: https://github.com/droctothorpe/byteme
|
36
44
|
licenses:
|
37
45
|
- MIT
|
38
46
|
metadata: {}
|
@@ -55,5 +63,5 @@ rubyforge_project:
|
|
55
63
|
rubygems_version: 2.6.12
|
56
64
|
signing_key:
|
57
65
|
specification_version: 4
|
58
|
-
summary:
|
66
|
+
summary: A simple CLI that converts between bytes, kilobytes, megabytes, etc.
|
59
67
|
test_files: []
|