Class: Work::Md::Commands::Daily

Inherits:
Object
  • Object
show all
Defined in:
lib/work/md/commands/daily.rb

Class Method Summary collapse

Class Method Details

.execute(_argv = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/work/md/commands/daily.rb', line 8

def execute(_argv = [])
  last_date = Date.today.prev_day
  work_dir = Work::Md::Config.work_dir
  last_file_name = nil

  (1..160).each do
    last_file_name = "#{last_date.strftime('%Y/%m/%d')}.md"
    break if ::File.exist?("#{work_dir}/#{last_file_name}")

    last_date = last_date.prev_day
  end

  today_file_name = Work::Md::DateFile.create_if_not_exist(DateTime.now)

  parser_today = Work::Md::Parser::Engine.new
  parser_today.add_file("#{work_dir}/#{today_file_name}")
  parser_today.freeze

  parser_last_day = Work::Md::Parser::Engine.new
  parser_last_day.add_file("#{work_dir}/#{last_file_name}")
  parser_last_day.freeze

  daily_file_path = work_dir + '/daily.md'
  ::File.delete(daily_file_path) if ::File.exist? daily_file_path

  t = Work::Md::Config.translations

  ajust_line = lambda do |line|
    get_first_line = ->(multiline_string) do
      lines = multiline_string.split("\n")
      first_line = lines.first
      return first_line
    end

    get_first_line.call(line.sub('x]', '-').sub(']', '-'))
  end

  ::File.open(daily_file_path, 'w+') do |f|
    f.puts("📅 Daily\n\n")
    f.puts("#{t[:did]}:\n")
    parser_last_day.tasks.each do |task|
      f.puts("#{ajust_line.call(task)}\n") if task != ' ]'
    end
    parser_last_day.meetings.each do |meeting|
      f.puts("#{ajust_line.call(meeting)}\n") if meeting != ' ]'
    end
    f.puts("\n")
    f.puts("#{t[:todo]}:\n")
    parser_today.tasks.each do |task|
      f.puts("#{ajust_line.call(task)}\n") if task != ' ]'
    end
    parser_today.meetings.each do |meeting|
      f.puts("#{ajust_line.call(meeting)}\n") if meeting != ' ]'
    end
  end

  Work::Md::File.open_in_editor([daily_file_path])
end