Class: Notes

Inherits:
Object
  • Object
show all
Defined in:
lib/notes-cli.rb,
lib/notes-cli/version.rb

Constant Summary collapse

VERSION =
"1.0.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/notes-cli.rb', line 2

def options
  @options
end

Class Method Details

.build_options(argv) ⇒ Object

Append any command line arguments to a default set of arguments arg_list is a directory and argument groups parsed from ARGV. For example:

“app/”, [‘-f’, ‘refactor’, ‘broken’], [‘–exclude’, ‘tmp’, ‘log’

]



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/notes-cli.rb', line 38

def build_options(argv)
  arg_list = Notes.parse_argv(argv)
  options  = {
    :flags   => %w(TODO FIXME OPTIMIZE),
    :exclude => []
  }

  options[:dir] = arg_list.shift

  arg_list.reject(&:empty?).each do |set|
    flag, *args = set
    args.map! { |arg| arg.delete("/") } #{ }"log/" => "log"

    case flag
    when '-f', '--flags'   then options[:flags].concat(args)
    when '-e', '--exclude' then options[:exclude].concat(args)
    else puts "Unknown argument: #{flag}"
    end
  end

  @options = options
end

.filesObject

List of files to scan for notes as specified in the options



62
63
64
65
66
67
# File 'lib/notes-cli.rb', line 62

def files
  pattern = File.join(@options[:dir], "**/*")
  Dir[pattern].reject do |f|
    File.directory?(f) || @options[:exclude].any? { |dir| File.dirname(f).include?(dir) }
  end
end

.find_allObject

Read and parse all files as specified in the options



70
71
72
# File 'lib/notes-cli.rb', line 70

def find_all
  Notes.files.each { |f| Notes.parse_file(f) }
end

.parse_argv(args) ⇒ Object

Parse ARGV into a directory and list of argument groups For example, given [‘app/’, -f’, ‘refactor’, ‘broken’, ‘–exclude’, ‘tmp’, ‘log’]:

> [ ‘app/’, [‘-f’, ‘refactor’, ‘broken’], [‘–exclude’, ‘tmp’, ‘log’] ]



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/notes-cli.rb', line 9

def parse_argv(args)
  result = []
  buf    = []
  dir    = args.first

  if args.empty? || dir.start_with?("-")
    # No dir was passed, use current dir
    result << Dir.pwd
  else
    # Dir was passed in
    dir = Dir.pwd if dir == '.'
    result << dir
    args = args.drop(1)
  end

  args.each do |arg|
    if arg.start_with?('-')
      result << buf unless buf.empty?
      buf = []
    end
    buf << arg
  end

  result << buf
end

.parse_file(filename) ⇒ Object

Scan a file for annotations and output numbered lines for each



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/notes-cli.rb', line 75

def parse_file(filename)
  name    = filename.gsub(Dir.pwd, '')
  counter = 1
  tasks   = []

  begin
    File.read(filename).each_line do |line|
      if @options[:flags].any? { |flag| line =~ /#{flag}/i }
        tasks << {
          :line_num => counter,
          :line     => line.strip
        }
      end
      counter += 1
    end
  rescue
    # Error occurred reading the file (ex. invalid byte sequence in UTF-8)
    # Move on quietly
  end

  if !tasks.empty?
    name.slice!(0) if name.start_with?("/")
    puts "#{name}:"

    tasks.each do |task|
      flag_regex = Regexp.new(@options[:flags].join('|'), true)
      color = 33 # yellow
      line  = task[:line].gsub(flag_regex) do |flag|
        "\e[#{color};1m#{flag}\033[0m"
      end
      puts "  ln #{task[:line_num]}: #{line}"
    end

    puts ""
  end
end