mdhost 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/mdhost.rb +87 -17
  4. data/mdhost.gemspec +1 -0
  5. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b987e2d55ede53f92d2f8a49261cb1cc2555e0a6bcc4f47408b0e14145729054
4
- data.tar.gz: 30fcf7b31833fe0a8b1c3ee33758597825c8fb1e227b1a58a54c35b054c52520
3
+ metadata.gz: 5777cc6b150eb2b2d4fd99a8aa09b92d213b1c256bbebe84cb2e518f70c9a557
4
+ data.tar.gz: 749f2a4656b9ab982676021c0a023172353244eda49923b5b51de42c197556f5
5
5
  SHA512:
6
- metadata.gz: 70bf37bbc9d5f04f6679fd4bdd930c8dba800f4d6e0bba90f878bbdbe0fe914a6e2cb72ca3a824713f68010858bcad967d8b9bcce9b43f5432f687f2a0e57d6c
7
- data.tar.gz: e1fafa621015efc0658c0744bcaf4489c77b03e3f5d043090dc475dd1012683ca0c0e22c642412cf585bf8d236917b7cc3c6c30f79591d0a1dd59a0e07e30685
6
+ metadata.gz: 02f255490e30db071012c1a29ab94d10cb252396d5b5d64f064ffed44666263b80e0625fbe2c709399f10f450832a1a51a63e192205e5bba99190a5e522d4651
7
+ data.tar.gz: b7bbb2a5feaae8386e8d85d423177d21fe7ee4700f63e747f3cab772811a8063decdf4e8adf4e1a577ab83bc354de3cc93a2e2b2a1245e4cd6d92d11e68b76cb
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1
1
+ 0.2
data/lib/mdhost.rb CHANGED
@@ -1,34 +1,66 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "clipboard"
4
+ require "optimist"
4
5
 
5
- VERSION = File.read(File.expand_path("../VERSION", __dir__)).strip.freeze
6
+ FORMAT_SPECIFIER = '#{}'
6
7
 
7
8
  module MDHost
8
9
  class CLI
9
10
  def initialize
10
- if ARGV.empty? || %w[--version -v].include?(ARGV.first)
11
- puts "mdhost version #{VERSION}"
12
- exit 0
11
+ @options = Optimist.options do
12
+ version "mdhost #{File.read(File.expand_path('../VERSION', __dir__)).strip}"
13
+
14
+ banner <<~BANNER
15
+ #{version}
16
+
17
+ Usage:
18
+ mdhost INPUT
19
+ mdhost --format FORMAT_STRING INPUTS...
20
+
21
+ Given a format string, inputs will be formatted to the location of
22
+ the sequence #{FORMAT_SPECIFIER}
23
+
24
+ Options:
25
+ BANNER
26
+
27
+ opt :format, "Format string to compose multiple inputs into", type: :string
28
+
29
+ educate_on_error
13
30
  end
14
31
 
15
- @input = ARGV.first
32
+ if ARGV.empty?
33
+ Optimist::educate
34
+ end
16
35
  end
17
36
 
18
37
  def run
19
- # Display table
20
- system("eshost", "-h", "JavaScriptCore,SpiderMonkey,V8", "-te", @input)
21
-
22
- # Pretty escape the input (we'll be using it in the markdown so we want
23
- # it to look clean)
24
- escaped_input = if @input.include?("'") && @input.include?('"')
25
- "\"#{@input.gsub('"', '\"')}\""
26
- elsif @input.include?('"')
27
- "'#{@input}'"
38
+ if (@format_string = @options.format)
39
+ Optimist::educate unless @format_string.include?(FORMAT_SPECIFIER)
40
+ run_format
28
41
  else
29
- "\"#{@input}\""
42
+ @input = ARGV.first
43
+ run_single_input
30
44
  end
45
+ end
31
46
 
47
+ # Pretty escape the input (we might be using it in the markdown so
48
+ # we want it to look clean)
49
+ def escape_input(input)
50
+ if input.include?("'") && input.include?('"')
51
+ "\"#{input.gsub('"', '\"')}\""
52
+ elsif input.include?('"')
53
+ "'#{input}'"
54
+ else
55
+ "\"#{input}\""
56
+ end
57
+ end
58
+
59
+ def display_table(input)
60
+ system("eshost", "-h", "JavaScriptCore,SpiderMonkey,V8", "-te", input)
61
+ end
62
+
63
+ def results_for(escaped_input)
32
64
  result = `eshost -e #{escaped_input}`.split(/\n+/)
33
65
 
34
66
  # We can't just #each_slice by 2, because sometimes an engine acts up and
@@ -42,9 +74,23 @@ module MDHost
42
74
  end
43
75
  end
44
76
 
77
+ table
78
+ end
79
+
80
+ def run_single_input
81
+ display_table @input
82
+
83
+ escaped_input = escape_input @input
84
+
85
+ result = `eshost -e #{escaped_input}`.split(/\n+/)
86
+
87
+ table = results_for escaped_input
88
+
45
89
  # We don't *need* to pretty format the table so precisely, but why not?
46
- engine_length = table.keys.max_by(&:length).length
47
- result_length = table.values.max_by(&:length).length
90
+ # The smallest this can be is 6 because of the length of the "Engine"
91
+ # and "Result" headers.
92
+ engine_length = [table.keys.max_by(&:length).length, 6].max
93
+ result_length = [table.values.max_by(&:length).length, 6].max
48
94
 
49
95
  markdown_table = table.map do |e, r|
50
96
  "|#{e}#{' ' * (engine_length - e.length)}|#{r}#{' ' * (result_length - r.length)}|"
@@ -61,5 +107,29 @@ module MDHost
61
107
 
62
108
  Clipboard.copy(output)
63
109
  end
110
+
111
+ def run_format
112
+ inputs = ARGV.map { |s| @format_string.sub(FORMAT_SPECIFIER, s) }
113
+ inputs.each do |input|
114
+ puts input
115
+ display_table input
116
+ end
117
+
118
+ output = +<<~TABLE
119
+ |Input|JavaScriptCore|SpiderMonkey|V8
120
+ |-----|--------------|------------|--
121
+ TABLE
122
+
123
+ inputs.each do |input|
124
+ escaped_input = escape_input input
125
+ results = results_for escaped_input
126
+
127
+ output << <<~ROW
128
+ |`#{input}`|#{results[:JavaScriptCore]}|#{results[:SpiderMonkey]}|#{results[:V8]}
129
+ ROW
130
+ end
131
+
132
+ Clipboard.copy(output)
133
+ end
64
134
  end
65
135
  end
data/mdhost.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.files = `git ls-files -z`.split "\x0"
21
21
 
22
22
  gem.add_dependency "clipboard", "~> 1.1"
23
+ gem.add_dependency "optimist", "~> 3.1"
23
24
 
24
25
  gem.add_development_dependency "rubocop", "~> 1.54"
25
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdhost
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinny Diehl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-12 00:00:00.000000000 Z
11
+ date: 2023-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clipboard
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: optimist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rubocop
29
43
  requirement: !ruby/object:Gem::Requirement