Class: YAMLCommand::Command::ViewCommand

Inherits:
YAMLCommand::Command show all
Defined in:
lib/yaml_command/view.rb

Overview

Display YAML file with color highlighting.

This command requires the ANSI gem be installed.

Instance Attribute Summary

Attributes inherited from YAMLCommand::Command

#file

Instance Method Summary collapse

Methods inherited from YAMLCommand::Command

command_name, #debug=, #debug?, #h!, #help!, #inspect=, #inspect?, #json=, #json?, #mute=, #mute?, #yaml=, #yaml?

Instance Method Details

#call(path = nil) ⇒ Object

Invoke view command.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yaml_command/view.rb', line 20

def call(path=nil)
  require 'ansi'

  colon = ":".ansi(:magenta)

  if path
    text = File.read(path)
  else
    if file
      text = File.read(file)
    else
      text = $stdin.read
    end
  end

  # Make sure it is valid YAML.
  YAML.load(text)

  text.each_line do |line|
    case line
    when /^---/
      print line
    when /^(\s*\-)(.*?)$/
      puts "%s%s" % [$1.ansi(:magenta), $2]
    when /^(\s*\w+)\:(.*?)$/
      puts "%s%s%s" % [$1.ansi(:cyan), colon, $2]
    else
      print line
    end
  end
end