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 |
# File 'lib/mdhost.rb', line 10 def initialize @options = Optimist. do version "mdhost #{File.read(File.('../VERSION', __dir__)).strip}" <<~BANNER #{version} Usage: mdhost INPUT mdhost --format FORMAT_STRING INPUTS... Given a format string, inputs will be formatted to the location of the sequence #{FORMAT_SPECIFIER} Options: BANNER opt :format, "Format string to compose multiple inputs into", type: :string opt :table_format, 'Format string to use for the table "Input" column', type: :string educate_on_error end if ARGV.empty? Optimist.educate end end |
Instance Method Details
#display_table(input) ⇒ Object
66 67 68 |
# File 'lib/mdhost.rb', line 66 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)
56 57 58 59 60 61 62 63 64 |
# File 'lib/mdhost.rb', line 56 def escape_input(input) if input.include?("'") && input.include?('"') "\"#{input.gsub('"', '\"')}\"" elsif input.include?('"') "'#{input}'" else "\"#{input}\"" end end |
#format(format_string, input) ⇒ Object
115 116 117 |
# File 'lib/mdhost.rb', line 115 def format(format_string, input) format_string.sub(FORMAT_SPECIFIER, input) end |
#results_for(escaped_input) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mdhost.rb', line 70 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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mdhost.rb', line 38 def run @format_string = @options.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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mdhost.rb', line 119 def run_format output = +<<~TABLE |Input|JavaScriptCore|SpiderMonkey|V8 |-----|--------------|------------|-- TABLE 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 = @options.table_format ? format(@options.table_format, input) : formatted_input output << <<~ROW |`#{display_input}`|#{results[:JavaScriptCore]}|#{results[:SpiderMonkey]}|#{results[:V8]} ROW end Clipboard.copy(output) end |
#run_single_input ⇒ Object
87 88 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 |
# File 'lib/mdhost.rb', line 87 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 = <<~EOS ``` > eshost -te #{escaped_input} ``` |Engine#{' ' * (engine_length - 6)}|Result#{' ' * (result_length - 6)}| |#{'-' * engine_length}|#{'-' * result_length}| #{markdown_table} EOS Clipboard.copy(output) end |