Class: MDHost::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/mdhost.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI



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.options do
    version "mdhost #{File.read(File.expand_path('../VERSION', __dir__)).strip}"

    banner "      \#{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    educate_on_error\n  end\n\n  if ARGV.empty?\n    Optimist.educate\n  end\nend\n"

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

#runObject



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_formatObject



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 = +"    |Input|JavaScriptCore|SpiderMonkey|V8\n    |-----|--------------|------------|--\n  TABLE\n\n  ARGV.each do |input|\n    formatted_input = format(@format_string, input)\n\n    puts formatted_input\n    display_table formatted_input\n\n    escaped_input = escape_input formatted_input\n    results = results_for escaped_input\n\n    display_input = @options.table_format ? format(@options.table_format, input)\n                                          : formatted_input\n\n    output << <<~ROW\n      |`\#{display_input}`|\#{results[:JavaScriptCore]}|\#{results[:SpiderMonkey]}|\#{results[:V8]}\n    ROW\n  end\n\n  Clipboard.copy(output)\nend\n"

#run_single_inputObject



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 = "    ```\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"