Sha256: a7ee35620a6493b8d9d47501aa124dd1e2242b85b404055dbc0bee19cb56d3fa

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "clipboard"

VERSION = File.read(File.expand_path("../VERSION", __dir__)).strip.freeze

module MDHost
  class CLI
    def initialize
      if ARGV.empty? || %w[--version -v].include?(ARGV.first)
        puts "mdhost version #{VERSION}"
        exit 0
      end

      @input = ARGV.first
    end

    def run
      # Display table
      system("eshost", "-h", "JavaScriptCore,SpiderMonkey,V8", "-te", @input)

      # Pretty escape the input (we'll be using it in the markdown so we want
      # it to look clean)
      escaped_input = if @input.include?("'") && @input.include?('"')
        "\"#{@input.gsub('"', '\"')}\""
      elsif @input.include?('"')
        "'#{@input}'"
      else
        "\"#{@input}\""
      end

      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

      # We don't *need* to pretty format the table so precisely, but why not?
      engine_length = table.keys.max_by(&:length).length
      result_length = table.values.max_by(&:length).length

      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mdhost-0.1 lib/mdhost.rb