Class: WorkMd::Commands::Parse

Inherits:
Object
  • Object
show all
Defined in:
lib/work_md/commands/parse.rb

Constant Summary collapse

PARSED_FILE_PATH =
WorkMd::Config.work_dir + '/parsed.md'

Class Method Summary collapse

Class Method Details

.execute(argv = []) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/work_md/commands/parse.rb', line 9

def execute(argv = [])
  begin
    args = Hash[argv.join(' ').scan(/-?([^=\s]+)(?:=(\S+))?/)]
    parser = WorkMd::Parser::Engine.new
    t = WorkMd::Config.translations

    year = args['y'] || Time.new.year
    month = args['m'] || Time.new.month

    month = "0#{month.to_i}" if month.to_i < 10

    add_file_to_parser = ->(day) do
      day = "0#{day.to_i}" if day.to_i < 10

      file_name = WorkMd::Config.work_dir + "/#{year}/#{month}/#{day}.md"

      parser.add_file(file_name)
    end

    if args['d'].include?('..')
      range = args['d'].split('..')

      (range[0].to_i..range[1].to_i).each { |day| add_file_to_parser.(day) }
    else
      args['d'].split(',').each { |day| add_file_to_parser.(day) }
    end

    parser.freeze

    ::File.delete(PARSED_FILE_PATH) if ::File.exist? PARSED_FILE_PATH

    ::File.open(PARSED_FILE_PATH, 'w+') do |f|
      f.puts("# #{WorkMd::Config.title}\n\n")
      f.puts("### #{t[:tasks]} (#{parser.tasks.size}):\n\n")
      parser.tasks.each do |task|
        f.puts("- [#{task}\n\n") if task != ' ]'
      end
      f.puts("---\n\n")
      f.puts("### #{t[:meetings]} (#{parser.meetings.size}):\n\n")
      parser.meetings.each do |meeting|
        f.puts("- [#{meeting}\n\n") if meeting != ' ]'
      end
      f.puts("---\n\n")
      f.puts("### #{t[:interruptions]} (#{parser.interruptions.size}):\n\n")
      parser.interruptions.each do |interruption|
        f.puts("- #{interruption}\n")
      end
      f.puts("---\n\n")
      f.puts("### #{t[:difficulties]} (#{parser.difficulties.size}):\n\n")
      parser.difficulties.each do |difficulty|
        f.puts("- #{difficulty}\n")
      end
      f.puts("---\n\n")
      f.puts("### #{t[:observations]} (#{parser.observations.size}):\n\n")
      parser.observations.each do |observation|
        f.puts("- #{observation}\n")
      end
      f.puts("---\n\n")
      f.puts("### #{t[:pomodoros]} (#{parser.average_pomodoros} #{t[:per_day]}):\n\n")
      f.puts("**#{t[:total]}: #{parser.pomodoros_sum}**")
      f.puts("\n\n")
      parser.pomodoros_bars.each do |pomodoro_bar|
        f.puts(pomodoro_bar)
        f.puts("\n\n")
      end
      f.puts("---\n\n")
      f.puts("### #{t[:days_bars]}:\n\n")
      f.puts("**#{t[:pomodoros]}: ⬛ | #{t[:meetings]}: 📅 | #{t[:interruptions]}: ⚠️ | #{t[:difficulties]}: 😓 | #{t[:observations]}: 📝 | #{t[:tasks]}: ✔️**")

      f.puts("\n\n")
      parser.days_bars.each do |day_bar|
        f.puts(day_bar)
        f.puts("\n\n")
      end

      f.puts("\n\n")
    end

    editor = WorkMd::Config.editor

    unless editor.nil?
      ::TTY::Editor.open(PARSED_FILE_PATH, command: editor)
    else
      ::TTY::Editor.open(PARSED_FILE_PATH)
    end
  rescue => e
    WorkMd::Cli.help(
      ::TTY::Box.frame(
        "message: #{e.message}",
        "",
        "Usage examples:",
        "",
        "work_md parse -d=1 -m=5 -y=2000 | get day 1 from month 5 and year 2000",
        "work_md parse -d=1,2,3          | get day 1, 2 and 3 from the current month and year",
        "work_md parse -d=1,2 -m=4       | get day 1 and 2 from month 4 and current year",
        "work_md parse -d=1..10 -m=4     | get day 1 to 10 from month 4 and current year",
        **WorkMd::Cli.error_frame_style
      )
    )
  end
end