Module: Til

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

Defined Under Namespace

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

Constant Summary collapse

HIGHLIGHT_COLORS =
{
  0 => :yellow,
  1 => :light_blue,
  2 => :green,
  3 => :blue,
  4 => :red,
  5 => :magenta,
  6 => :light_magenta,
}
VERSION =
"0.0.6"

Class Method Summary collapse

Class Method Details

.config_settings_file_and_notes_repoObject



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

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

.edit_file(search_term, options) ⇒ Object



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

def self.edit_file(search_term, options)
  if options[:last] 
    system(Settings.load.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(Settings.load.editor, matches.filter.path)
  end
end

.enumerate(notes) ⇒ Object



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

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

.list_all_notesObject



151
152
153
154
155
# File 'lib/til.rb', line 151

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



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

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



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

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



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

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



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

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


157
158
159
160
# File 'lib/til.rb', line 157

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


120
121
122
# File 'lib/til.rb', line 120

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


124
125
126
# File 'lib/til.rb', line 124

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


116
117
118
# File 'lib/til.rb', line 116

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


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

def self.print_working_directory
  puts Settings.load.directory
end

.remove_file(search_term) ⇒ Object



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

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



111
112
113
# File 'lib/til.rb', line 111

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

.run_git_command(*args) ⇒ Object



102
103
104
# File 'lib/til.rb', line 102

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



107
108
109
# File 'lib/til.rb', line 107

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

.search_results_for(search_terms) ⇒ Object



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

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