class Hylite::CLI

Attributes

argv[RW]
stdin[RW]

Public Class Methods

new(stdin, argv) click to toggle source
# File lib/hylite/cli.rb, line 30
def initialize(stdin, argv)
  self.stdin = stdin
  self.argv = argv
end

Public Instance Methods

config() click to toggle source
# File lib/hylite/cli.rb, line 51
def config
  @config ||= parse(argv)
end
errors() click to toggle source
# File lib/hylite/cli.rb, line 47
def errors
  config.fetch :errors
end
help_screen() click to toggle source
# File lib/hylite/cli.rb, line 5
    def help_screen
      <<-HELP
      Usage: echo '1+1' | hylite

        Syntax Highlighting for Scripters.

        Finds an existing syntax highlighting tool (rouge, coderay, pygments),
        and uses that to highlight the code.

      Options:
        -h        This help screen
        -l LANG   Set the language (defaults to Ruby)

      Examples:
        Set the language to JavaScript
        $ echo 'function alphabet() { console.log("abc"); }' | hylite -l javascript

        Add syntax highlighting to `cat`
        $ cat lib/hylite.rb | hylite

        Highlight sassy style sheets
        $ cat app/assets/stylesheets/site.css.scss | hylite -l scss
      HELP
    end
result() click to toggle source
# File lib/hylite/cli.rb, line 35
def result
  return help_screen if config[:help]
  ensure_evaluated
  @result
end
success?() click to toggle source
# File lib/hylite/cli.rb, line 41
def success?
  return true if config[:help]
  ensure_evaluated
  errors.none?
end

Private Instance Methods

ensure_evaluated() click to toggle source
# File lib/hylite/cli.rb, line 59
def ensure_evaluated
  return if @evaluated
  @evaluated = true
  @success   = true
  @result    = Hylite.call(stdin, config)
end
parse(args) click to toggle source
# File lib/hylite/cli.rb, line 66
def parse(args)
  config = {errors: [], help: false, lang: nil}
  args   = args.dup
  while args.any?
    arg = args.shift
    case arg
    when '-h', '--help'
      config[:help] = true
    when '-l', /^--l.*/
      if lang = args.shift
        config[:lang] = lang
      else
        config[:errors] << "Expected a language after #{arg.inspect}"
      end
    else
      config[:errors] << "Unknown argument #{arg.inspect}"
    end
  end
  config
end