Module: Til

Defined in:
lib/til.rb,
lib/til/cli.rb,
lib/til/note.rb,
lib/til/version.rb,
lib/til/settings.rb,
lib/til/directory.rb,
lib/til/note_list.rb,
lib/til/initializer.rb,
lib/til/note_editor.rb,
lib/til/note_finder.rb,
lib/til/note_writer.rb,
lib/til/note_deleter.rb

Defined Under Namespace

Classes: Cli, Directory, Initializer, Note, NoteDeleter, NoteEditor, NoteFinder, NoteList, NoteWriter, Settings, TemporaryNote

Constant Summary collapse

EDITOR =
ENV["VISUAL"] || ENV["EDITOR"] || "vim"
HIGHLIGHT_COLORS =
{
  0 => :yellow,
  1 => :light_blue,
  2 => :green,
  3 => :blue,
  4 => :red,
  5 => :magenta,
  6 => :light_magenta,
}
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.edit_file(search_term, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/til.rb', line 69

def self.edit_file(search_term, options)
  if options[:last] 
    system(EDITOR, Directory.root.notes.most_recent.path)
    return
  end
  matches = Til.search_results_for(search_term)
  if matches.length < 1
    puts "no matches"
  else
    system(EDITOR, matches.filter.path)
  end
end

.enumerate(notes) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/til.rb', line 164

def self.enumerate notes
  subjects_seen = Array.new
  longest_subject_length = notes.sort_by{|note| note.subject.length}.last.subject.length
  notes.each do |note|
    subjects_seen.push(note.subject) if !subjects_seen.include?(note.subject)
    color_index = subjects_seen.index(note.subject) % HIGHLIGHT_COLORS.length
    color = HIGHLIGHT_COLORS[color_index]
    spacing = " " * (longest_subject_length - note.subject.length)
    puts note.subject.colorize(color) + ": " + spacing + note.title.bold + " (" + note.pretty_printed_mtime + ")"
  end
end

.init_settings_file_and_notes_repoObject



24
25
26
27
28
# File 'lib/til.rb', line 24

def self.init_settings_file_and_notes_repo
  Initializer.new.create_settings_file unless Settings.settings_file_configured?
  Initializer.new.create_notes_directory
  puts "Your settings file is at `#{Settings.settings_file_path}`. Your notes are at `#{Settings.load.directory}`."
end

.list_all_notesObject



153
154
155
156
157
# File 'lib/til.rb', line 153

def self.list_all_notes
  notes = Directory.root.notes.sort_by_modified_time
  puts "Listing all #{notes.length} notes: "
  Til.enumerate notes
end

.list_notes_in(subject) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/til.rb', line 130

def self.list_notes_in(subject)
  notes = Directory.for(subject).notes
  if notes.empty?
    puts "You don't seem to have any notes on that subject!"
    puts "You DO have notes on the following subjects:"
    Til.list_subjects :flat
  else
    Til.enumerate notes
  end
end

.list_subjects(list_style = :bullet_points) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/til.rb', line 141

def self.list_subjects(list_style=:bullet_points)
  subjects = Array.new
  Dir.glob(Settings.load.directory + "/*").each do |dirname|
    subjects.push dirname.gsub(Settings.load.directory + "/", "")
  end
  if list_style==:flat
    puts subjects.join(", ")
  else
    subjects.each {|subject| puts "- #{subject}"}
  end
end

.new_note(subject, title) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/til.rb', line 34

def self.new_note(subject, title)
  if_unmodified = Proc.new do
    puts "You didn't write anything, so a note wasn't created.".red
    return
  end

  if_modified = Proc.new do |note_content|
    subject_path = Settings.load.directory + "/#{subject.downcase}"
    if !File.directory? subject_path
      FileUtils.mkdir(subject_path)
      puts "Created a new directory for #{subject.downcase}"
    end
    file_path = subject_path + "/" + title.downcase.gsub(" ", "-") + ".md"

    write_file = File.new(file_path, "w")
    write_file.write note_content
    write_file.close
    puts "You created a new note in ".green + subject.green.bold + ". `til last` to read it, or `til edit --last` to edit it.".green
  end

  NoteWriter.new(title).call(if_modified, if_unmodified)
end

.open_github_page_for_repoObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/til.rb', line 91

def self.open_github_page_for_repo
  if !Settings.load.github_repo.nil?
    system("open https://www.github.com/#{Settings.github_repo}")
  else
    begin
      repo = %x|git --git-dir=#{Settings.load.directory}/.git --work-tree=#{Settings.load.directory} remote -v|.scan(/:(.*).git/).flatten.first
      system("open https://www.github.com/#{repo}")
    rescue
      puts "You don't appear to have a GitHub remote repository for your notes."
    end
  end
end


159
160
161
162
# File 'lib/til.rb', line 159

def self.print note
  puts note.subject.underline + ": " + note.title.bold + " (" + note.pretty_printed_mtime + ")"
  puts note.content
end


122
123
124
# File 'lib/til.rb', line 122

def self.print_all_notes
  Directory.root.notes.each {|note| Til.print note}
end


126
127
128
# File 'lib/til.rb', line 126

def self.print_all_notes_in(subject)
  Directory.for(subject).notes.each {|note| Til.print note}
end


118
119
120
# File 'lib/til.rb', line 118

def self.print_most_recent_note quantity
  Directory.root.notes.sort_by_modified_time.take(quantity).each {|note| Til.print note}
end


30
31
32
# File 'lib/til.rb', line 30

def self.print_working_directory
  puts Settings.load.directory
end

.remove_file(search_term) ⇒ Object



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

def self.remove_file(search_term)
  matches = Til.search_results_for(search_term)
  if matches.length < 1
    puts "no matches"
  else
    NoteDeleter.new(matches.filter).delete
  end
end

.run_ag_command(*args) ⇒ Object



113
114
115
# File 'lib/til.rb', line 113

def self.run_ag_command(*args)
  system("ag #{args.join(" ")} #{Settings.load.directory}")
end

.run_git_command(*args) ⇒ Object



104
105
106
# File 'lib/til.rb', line 104

def self.run_git_command(*args)
  system("git --git-dir=#{Settings.load.directory}/.git --work-tree=#{Settings.load.directory} #{args.join(" ")}")
end

.run_grep_command(*args) ⇒ Object



109
110
111
# File 'lib/til.rb', line 109

def self.run_grep_command(*args)
  system("grep -r #{args.join(" ")} #{Settings.load.directory}")
end

.search_results_for(search_terms) ⇒ Object



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

def self.search_results_for(search_terms)
  matches = NoteList.new
  Directory.root.notes.each do |note|
    if note.title.downcase.include?(search_terms.downcase)
      matches.push note
    end
  end
  matches
end