Class: Done::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/done_log.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date:, date_color: nil) ⇒ Log

Returns a new instance of Log.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/done_log.rb', line 14

def initialize date:, date_color: nil
  @date = date
  @dir = Done::Log.default_log_dir
  year = date.year.to_s
  month = date.month.to_s.rjust(2, "0")
  @log_file = File.join(dir, year, month, date_string)

  config = Done::Log.config
  @git = GitRepo.new(config[:git_repo], dir, log_file)
  @date_color = date_color || config[:date_color]
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



12
13
14
# File 'lib/done_log.rb', line 12

def date
  @date
end

#dirObject (readonly)

Returns the value of attribute dir.



12
13
14
# File 'lib/done_log.rb', line 12

def dir
  @dir
end

#gitObject (readonly)

Returns the value of attribute git.



12
13
14
# File 'lib/done_log.rb', line 12

def git
  @git
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



12
13
14
# File 'lib/done_log.rb', line 12

def log_file
  @log_file
end

Class Method Details

.config(config_file = default_config_file) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/done_log.rb', line 115

def config config_file = default_config_file
  if File.exist? config_file
    begin
      JSON.parse(File.read(config_file), symbolize_names: true)
    rescue JSON::ParserError => e
      warn "Unable to parse config file: #{e.inspect}"
      {}
    end
  else
    FileUtils.mkdir_p(default_dir)

    config = default_config
    puts "What Git URL (e.g. [email protected]:user_name/example_log.git) should we use?\nLeave blank to skip."
    print "? "
    url = gets.strip

    unless url.empty?
      config[:git_repo] = url
    end

    File.open(config_file, "w") do |f|
      f.puts(JSON.pretty_generate(config))
    end

    config
  end
end

.default_configObject



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

def default_config
  {
    git_repo: nil,
    date_color: :cyan
  }
end

.default_config_fileObject



150
151
152
# File 'lib/done_log.rb', line 150

def default_config_file
  File.join(default_dir, "config")
end

.default_dirObject



103
104
105
# File 'lib/done_log.rb', line 103

def default_dir
  File.join(Dir.home, ".done").freeze
end

.default_log_dirObject



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

def default_log_dir
  File.join(Dir.home, ".done", "log").freeze
end

.edit_logs(time_period) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/done_log.rb', line 154

def edit_logs time_period
  if time_period.is_a? Date
    Done::Log.new(date: time_period).log
  else
    time_period.each do |d|
      Done::Log.new(date: d).log
    end
  end
end

.logsObject



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

def logs
  Dir[self.default_dir].sort
end

.show_logs(time_period, date_color = nil) ⇒ Object



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

def show_logs time_period, date_color = nil
  if time_period.is_a? Date
    Done::Log.new(date: time_period, date_color: date_color).show
  else
    pull = true
    time_period.each do |d|
      Done::Log.new(date: d, date_color: date_color).show(pull)
      pull = false
    end
  end
end

Instance Method Details

#create_logObject



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

def create_log
  unless File.exist? log_file
    File.open(log_file, "w") do |f|
      f.puts <<~LOG
      #{date_string}

      LOG
    end

    git.add
  end
end

#date_stringObject



98
99
100
# File 'lib/done_log.rb', line 98

def date_string
  date.iso8601.freeze
end

#ensure_directoryObject



60
61
62
63
64
# File 'lib/done_log.rb', line 60

def ensure_directory
  FileUtils.mkdir_p(dir)
  git.init
  FileUtils.mkdir_p(File.dirname(log_file))
end

#logObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/done_log.rb', line 26

def log
  ensure_directory
  git.pull
  create_log
  vim_edit
  strip_log
  git.add
  git.commit
  git.push
end

#show(pull = true) ⇒ Object



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

def show pull = true
  ensure_directory
  git.pull if pull

  if File.exist? log_file
    log = File.read(log_file)
    if $stdout.tty?
      puts log.sub(/^(\d{4}-\d{2}-\d{2})$/) { |date| Done::ANSIColors.colorize(date, @date_color) }
    else
      puts log
    end

    puts
  else
    puts <<~LOG
  #{Done::ANSIColors.colorize(date_string, @date_color)}

  [No log]

    LOG
  end
end

#strip_logObject



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

def strip_log
  log = File.read(log_file).strip

  if log.empty?
    File.delete(log_file)
  else
    File.open(log_file, "w") do |f|
      f.puts log
    end
  end
end

#vim_editObject



91
92
93
94
95
96
# File 'lib/done_log.rb', line 91

def vim_edit
  system 'vim',
    '+ normal Go', # Move to end of file, start new line
    '+startinsert', # Switch to insert mode
    log_file
end