Class: OdroidLCD::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv, mocks = {}) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/odroid_lcd/cli.rb', line 7

def initialize(argv, mocks = {})
  @options = parse_arguments(argv)
  @argv = argv

  # If we're testing via the command line, set the mock rather than LCD.
  @lcd = if @options[:test]
    @hw_mock = OdroidLCD::HWMock.new
    mock_hw = mocks.merge({odroidlcd_hw: @hw_mock})
    OdroidLCD::LCD.new(mocks: mock_hw)
  else
    OdroidLCD::LCD.new(mocks: mocks)
  end

  # set the default text alignment
  @align = @options[:align] || "left"

  freeze()
end

Instance Method Details

#parse_arguments(argv) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/odroid_lcd/cli.rb', line 47

def parse_arguments(argv)
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: odroid-lcd [options] [first line] [second line]"

    opts.on("-f", "--file=FILE", String, "Read content from a file") do |x|
      if x == nil
        puts opts
        exit 2
      end
      options[:file] = x
    end

    opts.on("--test", "Dry run test (does not update the screen)") do |x|
      options[:test] = x
    end

    opts.on("--align=ALIGN", String, "Align text: left, right, center") do |x|
      if x == nil
        puts opts
        exit 2
      end
      options[:align] = x
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end

  end.parse!
  options
end

#read_file(filename) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/odroid_lcd/cli.rb', line 81

def read_file(filename)
  file = if (filename == '-')
    $stdin
  else
    File.open(filename, "r")
  end

  file.readlines.map {|line| line.chomp }
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/odroid_lcd/cli.rb', line 27

def run
  lines = if @options.has_key?(:file)
    read_file(@options[:file])
  else
    @argv[0,2].map {|line| line.chomp }
  end

  @lcd.set_lines(lines: lines, align: @align)

  if @options[:test]
    show_mocked_lcd()
  end
end

#show_mocked_lcdObject



41
42
43
44
# File 'lib/odroid_lcd/cli.rb', line 41

def show_mocked_lcd
  puts "[#{@hw_mock.get(row: 0).join}]"
  puts "[#{@hw_mock.get(row: 1).join}]"
end