Class: Noter::Viewer

Inherits:
Object
  • Object
show all
Includes:
Pager
Defined in:
lib/noter/viewer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Viewer

Returns a new instance of Viewer.



11
12
13
14
15
16
# File 'lib/noter/viewer.rb', line 11

def initialize(options = {})
  @do_paging = options[:paging].nil? ? true : options[:paging]
  @do_colors = options[:colorize].nil? ? true : options[:colorize]
  @grep_string = options[:grep_string]
  @tail_count = options[:tail_count].nil? ? false : options[:tail_count].to_i
end

Instance Attribute Details

#do_pagingObject (readonly)

Returns the value of attribute do_paging.



9
10
11
# File 'lib/noter/viewer.rb', line 9

def do_paging
  @do_paging
end

Instance Method Details

#existing_filesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/noter/viewer.rb', line 18

def existing_files
  return @existing_files if @existing_files

  if @grep_string
    @existing_files = `grep -l #{@grep_string} #{NoteFile.dir}/*`.split("\n")
  else
    @existing_files = Dir.glob("#{NoteFile.dir}/*").sort
  end
  if @tail_count
    @existing_files = @existing_files.last(@tail_count)
  end
  @existing_files
end

#filename_from_index(index) ⇒ Object



43
44
45
# File 'lib/noter/viewer.rb', line 43

def filename_from_index(index)
  existing_files[index.to_i]
end

#show_file(filename) ⇒ Object



51
52
53
54
55
# File 'lib/noter/viewer.rb', line 51

def show_file(filename)
  contents = File.read(filename)
  page if @do_paging
  puts contents
end

#show_file_from_index(index) ⇒ Object



47
48
49
# File 'lib/noter/viewer.rb', line 47

def show_file_from_index(index)
  show_file(filename_from_index(index))
end

#show_filesObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/noter/viewer.rb', line 57

def show_files
  existing_files.each do |filename|
    if @do_colors
      puts "\n\n#{filename}".colorize(:red)
    else
      puts "\n\n#{filename}"
    end
    show_file(filename)
  end
end

#show_first_lines(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/noter/viewer.rb', line 32

def show_first_lines(options = {})
  existing_files.sort.reverse.each do |filename|
    file = NoteFile.new(filename)
    filename_string = ""
    if options[:with_filename]
      filename_string = "#{filename}: "
    end
    puts "#{file.formatted_time}: #{filename_string}#{file.first_line}"
  end
end