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,
lib/til/services/note_displayer.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Class Method Details

.config_settings_file_and_notes_repoObject



14
15
16
17
18
# File 'lib/til.rb', line 14

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



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

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

.list_all_notesObject



143
144
145
146
147
# File 'lib/til.rb', line 143

def self.list_all_notes
  notes = Directory.root.notes.sort_by_modified_time
  puts "Listing all #{notes.length} notes: "
  NoteDisplayer.new(notes).list
end

.list_notes_in(subject) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/til.rb', line 120

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
  else
    NoteDisplayer.new(notes).list
  end
end

.list_subjects(list_style = :bullet_points) ⇒ Object



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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/til.rb', line 24

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



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

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.".red
    end
  end
end


149
150
151
# File 'lib/til.rb', line 149

def self.print note
  NoteDisplayer.new(note).print
end


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

def self.print_all_notes
  notes = Directory.root.notes
  NoteDisplayer.new(notes).print
end


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

def self.print_all_notes_in(subject)
  notes = Directory.for(subject).notes
  NoteDisplayer.new(notes).print
end


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

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


20
21
22
# File 'lib/til.rb', line 20

def self.print_working_directory
  puts Settings.load.directory
end

.remove_file(search_term) ⇒ Object



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

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



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

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

.run_git_command(*args) ⇒ Object



93
94
95
# File 'lib/til.rb', line 93

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



97
98
99
# File 'lib/til.rb', line 97

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

.search_results_for(search_terms) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/til.rb', line 47

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