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
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/work/md/commands/last.rb', line 8
def execute(argv = [])
work_dir = Work::Md::Config.work_dir
if argv == []
current_day = Date.today.prev_day
found_file = false
(1..160).each do
file_name = "#{current_day.strftime('%Y/%m/%d')}.md"
if ::File.exist?("#{work_dir}/#{file_name}")
Work::Md::File.open_in_editor([file_name])
found_file = true
break
end
current_day = current_day.prev_day
end
unless found_file
Work::Md::Cli.help(
::TTY::Box.frame(
"message: No file found in last 5 months",
**Work::Md::Cli.error_frame_style
)
)
end
return
end
to_open = []
is_numeric = ->(str) {
str == "#{str.to_f}" || str == "#{str.to_i}"
}
if is_numeric.(argv.first)
last_n = argv.first.to_i
else
Work::Md::Cli.help(
::TTY::Box.frame(
"message: 'last' command accept only numeric arguments, you give: #{argv.inspect}",
'',
'Usage example:',
'',
'work-md l 7 # open the last 7 days',
'work-md l # open the last day',
**Work::Md::Cli.error_frame_style
)
)
return
end
last_date = Date.today.prev_day
(1..last_n).map do
last_file_name = "#{last_date.strftime('%Y/%m/%d')}.md"
if ::File.exist?("#{work_dir}/#{last_file_name}")
to_open.push("#{work_dir}/#{last_file_name}")
else
nil
end
last_date = last_date.prev_day
end
Work::Md::File.open_in_editor(to_open)
end
|