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 39 |
# 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 :execute, \"Execute the input using `eshost -x`\", short: :x\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
70 71 72 |
# File 'lib/mdhost.rb', line 70 def display_table(input) system("eshost", "-g", "jsc,jsshell,d8", "-t#{@eshost_mode}", 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)
60 61 62 63 64 65 66 67 68 |
# File 'lib/mdhost.rb', line 60 def escape_input(input) if input.include?("'") && input.include?('"') "\"#{input.gsub('"', '\"')}\"" elsif input.include?('"') "'#{input}'" else "\"#{input}\"" end end |
#format(format_string, input) ⇒ Object
123 124 125 |
# File 'lib/mdhost.rb', line 123 def format(format_string, input) format_string.sub(FORMAT_SPECIFIER, input) end |
#results_for(escaped_input) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mdhost.rb', line 74 def results_for(escaped_input) result = `eshost -g jsc,jsshell,d8 -#{@eshost_mode} #{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| engine_name = %w[JavaScriptCore SpiderMonkey V8].find do |engine| line.downcase.end_with?(engine.downcase) end if engine_name table[engine_name.to_sym] = result[i + 1] end end table end |
#run ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mdhost.rb', line 41 def run @eshost_mode = .execute ? "x" : "e" @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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mdhost.rb', line 127 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mdhost.rb', line 95 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 -t\#{@eshost_mode} \#{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" |