Class: MDHost::CLI
- Inherits:
-
Object
- Object
- MDHost::CLI
- Defined in:
- lib/mdhost.rb
Instance Method Summary collapse
- #display_table(input) ⇒ Object
-
#escape_input(input) ⇒ Object
Pretty escape the input (we might be using it in the markdown so we want it to look clean).
- #format(format_string, input) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #results_for(escaped_input) ⇒ Object
- #run ⇒ Object
- #run_format ⇒ Object
- #run_single_input ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mdhost.rb', line 10 def initialize = Optimist. do version "mdhost #{File.read(File.expand_path('../VERSION', __dir__)).strip}" " \#{version}\n\n Usage:\n mdhost INPUT\n mdhost --format FORMAT_STRING INPUTS...\n\n Given a format string, inputs will be formatted to the location of\n the sequence \#{FORMAT_SPECIFIER}\n\n Options:\n BANNER\n\n opt :format, \"Format string to compose multiple inputs into\", type: :string\n opt :table_format, 'Format string to use for the table \"Input\" column', type: :string\n\n opt :browser_names, \"Use browser names in output table\"\n\n educate_on_error\n end\n\n if ARGV.empty?\n Optimist.educate\n end\nend\n" |
Instance Method Details
#display_table(input) ⇒ Object
68 69 70 |
# File 'lib/mdhost.rb', line 68 def display_table(input) system("eshost", "-h", "JavaScriptCore,SpiderMonkey,V8", "-te", input) end |
#escape_input(input) ⇒ Object
Pretty escape the input (we might be using it in the markdown so we want it to look clean)
58 59 60 61 62 63 64 65 66 |
# File 'lib/mdhost.rb', line 58 def escape_input(input) if input.include?("'") && input.include?('"') "\"#{input.gsub('"', '\"')}\"" elsif input.include?('"') "'#{input}'" else "\"#{input}\"" end end |
#format(format_string, input) ⇒ Object
117 118 119 |
# File 'lib/mdhost.rb', line 117 def format(format_string, input) format_string.sub(FORMAT_SPECIFIER, input) end |
#results_for(escaped_input) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mdhost.rb', line 72 def results_for(escaped_input) result = `eshost -e #{escaped_input}`.split(/\n+/) # We can't just #each_slice by 2, because sometimes an engine acts up and # produces no output, which would mess up the grouping. So, we need to # look specifically for the engines that we want and then take the next # line as the result. table = {} result.each_with_index do |line, i| if %w[JavaScriptCore SpiderMonkey V8].any? { |e| line.end_with? e } table[line.match(/\w+/).to_s.to_sym] = result[i + 1] end end table end |
#run ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mdhost.rb', line 40 def run @format_string = .format if !@format_string && ARGV.length > 1 @format_string = FORMAT_SPECIFIER end if @format_string Optimist.educate unless @format_string.include?(FORMAT_SPECIFIER) run_format else @input = ARGV.first run_single_input end end |
#run_format ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mdhost.rb', line 121 def run_format output = +"|Input|" output += .browser_names ? "Safari|Firefox|Chrome" : "JavaScriptCore|SpiderMonkey|V8" output += "\n|---|---|---|---\n" ARGV.each do |input| formatted_input = format(@format_string, input) puts formatted_input display_table formatted_input escaped_input = escape_input formatted_input results = results_for escaped_input display_input = .table_format ? format(.table_format, input) : formatted_input output << " |`\#{display_input}`|\#{results[:JavaScriptCore]}|\#{results[:SpiderMonkey]}|\#{results[:V8]}\n ROW\n end\n\n Clipboard.copy(output)\nend\n" |
#run_single_input ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mdhost.rb', line 89 def run_single_input display_table @input escaped_input = escape_input @input table = results_for escaped_input # We don't *need* to pretty format the table so precisely, but why not? # The smallest this can be is 6 because of the length of the "Engine" # and "Result" headers. engine_length = [table.keys.max_by(&:length).length, 6].max result_length = [table.values.max_by(&:length).length, 6].max markdown_table = table.map do |e, r| "|#{e}#{' ' * (engine_length - e.length)}|#{r}#{' ' * (result_length - r.length)}|" end.join("\n") output = " ```\n > eshost -te \#{escaped_input}\n ```\n |Engine\#{' ' * (engine_length - 6)}|Result\#{' ' * (result_length - 6)}|\n |\#{'-' * engine_length}|\#{'-' * result_length}|\n \#{markdown_table}\n EOS\n\n Clipboard.copy(output)\nend\n" |