work-md 0.4.8 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d958b459035344108c414ccf382b6bce83acba70585accb0c4584b634d62808
4
- data.tar.gz: db98f8a2b2a2d5dacd81cfa3951b92407455e0dffacd15e2d6e08ca6f237a98c
3
+ metadata.gz: 3333dbe54859ef57acade3b997c337b6994b64e681369e782e39a80fb478632d
4
+ data.tar.gz: e7de4d34bf2c5eb846fa160bf0cff93250216dbc45daa6b26035ac3a3784ede6
5
5
  SHA512:
6
- metadata.gz: 48b5aad2d02b382d86ccf273518f87ccfd3fd7248feac6de4eca364461019533dd3497066c99139dac7c34ade603459ab806f35d646ad8cfadb664bb8e4fcfc7
7
- data.tar.gz: 5d8e4ab76582f9e462a2d6286d1f5aef1a26b8e118a0f8c711ce36e1917f5f9c500c24c3357b5ef1985060d75cc0a3a777c14ef7ceba055e0791ceb41fee119c
6
+ metadata.gz: 3ea36e20ab31b4237157d55327978cba6c64c3b4f02df9987fcb56993b1af1826fa7ed5dbe2371ee83a0e06d6e2987126010e34351694f6fdb2538c68bafc356
7
+ data.tar.gz: 9bb98607f7e1ed8d426164b5b28a8f0406160405706adb3f2bbfcb90af6cb4cfe860b35672d147742f47fa9c89c665f4582f6a43fe4e229374ea901122f7cca3
@@ -8,9 +8,9 @@ module Work
8
8
  def execute(argv = [])
9
9
  prompt = TTY::Prompt.new
10
10
 
11
- file_names = Work::Md::DateFile.list_file_names_by_argv_query(argv)
11
+ file_paths = Work::Md::DateFile.list_file_paths_by_argv_query(argv)
12
12
 
13
- if file_names == []
13
+ if file_paths == []
14
14
  puts ::TTY::Box.frame(
15
15
  "message: File(s) not found!",
16
16
  **Work::Md::Cli.error_frame_style
@@ -19,7 +19,7 @@ module Work
19
19
  return
20
20
  end
21
21
 
22
- file_names.each do |file_name|
22
+ file_paths.each do |file_name|
23
23
  if prompt.yes?("Do you really want to delete \"#{file_name}\"!?")
24
24
  ::File.delete(file_name)
25
25
  end
@@ -27,7 +27,7 @@ module Work
27
27
  rescue StandardError
28
28
  Work::Md::Cli.help(
29
29
  ::TTY::Box.frame(
30
- "message: Some error occurred interpreting your command!",
30
+ "message: Some error occurred interpreting your command or the date sent is invalid!",
31
31
  '',
32
32
  'Usage examples:',
33
33
  '',
@@ -6,9 +6,13 @@ module Work
6
6
  class Open
7
7
  class << self
8
8
  def execute(argv = [])
9
- file_names = Work::Md::DateFile.list_file_names_by_argv_query(argv)
9
+ file_paths =
10
+ Work::Md::DateFile.list_file_paths_by_argv_query(
11
+ argv,
12
+ create_inexistent: true
13
+ )
10
14
 
11
- if file_names == []
15
+ if file_paths == []
12
16
  puts ::TTY::Box.frame(
13
17
  "message: File(s) not found!",
14
18
  **Work::Md::Cli.error_frame_style
@@ -17,11 +21,11 @@ module Work
17
21
  return
18
22
  end
19
23
 
20
- Work::Md::File.open_in_editor(file_names)
21
- rescue StandardError
24
+ Work::Md::File.open_in_editor(file_paths)
25
+ rescue
22
26
  Work::Md::Cli.help(
23
27
  ::TTY::Box.frame(
24
- "message: Some error occurred interpreting your command!",
28
+ "message: Some error occurred interpreting your command or the date sent is invalid!",
25
29
  '',
26
30
  'Usage examples:',
27
31
  '',
@@ -9,8 +9,8 @@ module Work
9
9
  parser = Work::Md::Parser::Engine.new
10
10
 
11
11
  Work::Md::DateFile
12
- .list_file_names_by_argv_query(argv)
13
- .each { |file_name| parser.add_file(file_name) }
12
+ .list_file_paths_by_argv_query(argv)
13
+ .each { |file_path| parser.add_file(file_path) }
14
14
 
15
15
  Work::Md::File.create_and_open_parsed(parser)
16
16
  rescue Work::Md::Parser::Error => e
@@ -3,60 +3,63 @@
3
3
  module Work
4
4
  module Md
5
5
  class DateFile
6
- def self.open_or_create(some_date, dir: nil)
6
+ def self.open_or_create(some_date)
7
7
  Work::Md::File.open_in_editor(
8
- [create_if_not_exist(some_date, dir: dir)], dir: dir
8
+ [create_if_not_exist(some_date)]
9
9
  )
10
10
  end
11
11
 
12
- def self.list_file_names_by_argv_query(argv)
13
- file_names = []
12
+ def self.list_file_paths_by_argv_query(argv, create_inexistent: false)
13
+ file_paths = []
14
14
 
15
- argv_keys_to_files = -> (argv_keys, acc_file_names) {
15
+ argv_keys_to_files = -> (argv_keys, acc_file_paths) {
16
16
  year = argv_keys['y'] || Time.new.year
17
17
  month = argv_keys['m'] || Time.new.month
18
18
 
19
19
  month = "0#{month.to_i}" if month.to_i < 10
20
20
 
21
- add_file_to_open = lambda do |day|
21
+ add_file_to_acc = lambda do |day|
22
22
  day = "0#{day.to_i}" if day.to_i < 10
23
23
 
24
- file_name = Work::Md::Config.work_dir + "/#{year}/#{month}/#{day}.md"
24
+ file_path = Work::Md::Config.work_dir + "/#{year}/#{month}/#{day}.md"
25
25
 
26
- acc_file_names.push(file_name) if ::File.exist? file_name
26
+ if create_inexistent
27
+ create_if_not_exist(DateTime.new(year.to_i, month.to_i, day.to_i))
28
+
29
+ acc_file_paths.push(file_path)
30
+ elsif ::File.exist? file_path
31
+ acc_file_paths.push(file_path)
32
+ end
27
33
  end
28
34
 
29
35
  if argv_keys['d'].include?('..')
30
36
  range = argv_keys['d'].split('..')
31
37
 
32
- (range[0].to_i..range[1].to_i).each { |day| add_file_to_open.call(day) }
38
+ (range[0].to_i..range[1].to_i).each { |day| add_file_to_acc.call(day) }
33
39
  else
34
- argv_keys['d'].split(',').each { |day| add_file_to_open.call(day) }
40
+ argv_keys['d'].split(',').each { |day| add_file_to_acc.call(day) }
35
41
  end
36
42
 
37
- acc_file_names
43
+ acc_file_paths
38
44
  }
39
45
 
40
46
  argv.join('#').split('#and#').map { |v| v.split("#") }.each do |args|
41
- argv_keys_to_files.(Work::Md::Cli.fetch_argv_keys(args), file_names)
47
+ argv_keys_to_files.(Work::Md::Cli.fetch_argv_keys(args), file_paths)
42
48
  end
43
49
 
44
- file_names
50
+ file_paths
45
51
  end
46
52
 
47
- def self.create_if_not_exist(some_date, dir: nil)
53
+ def self.create_if_not_exist(some_date)
48
54
  t = Work::Md::Config.translations
49
- work_dir = dir || Work::Md::Config.work_dir
50
-
51
- file_name = "#{some_date.strftime('%Y/%m/%d')}.md"
52
-
53
- return file_name if ::File.exist?("#{work_dir}/#{file_name}")
55
+ file = date_to_file_locations(some_date)
56
+ return file[:name] if ::File.exist?(file[:path])
54
57
 
55
58
  ::FileUtils
56
- .mkdir_p("#{work_dir}/#{some_date.strftime('%Y/%m')}")
59
+ .mkdir_p(file[:dir])
57
60
 
58
61
  ::File.open(
59
- "#{work_dir}/#{file_name}",
62
+ file[:path],
60
63
  'w+'
61
64
  ) do |f|
62
65
  f.puts("# #{some_date.strftime('%d/%m/%Y')} - #{Work::Md::Config.title} \n\n")
@@ -76,7 +79,17 @@ module Work
76
79
  f.puts("0\n\n")
77
80
  end
78
81
 
79
- file_name
82
+ file[:name]
83
+ end
84
+
85
+ def self.date_to_file_locations(some_date)
86
+ work_dir = Work::Md::Config.work_dir
87
+
88
+ {
89
+ name: "#{some_date.strftime('%Y/%m/%d')}.md",
90
+ dir: "#{work_dir}/#{some_date.strftime('%Y/%m')}",
91
+ path: "#{work_dir}/#{some_date.strftime('%Y/%m/%d')}.md"
92
+ }
80
93
  end
81
94
  end
82
95
  end
data/lib/work/md/file.rb CHANGED
@@ -3,19 +3,6 @@
3
3
  module Work
4
4
  module Md
5
5
  class File
6
- def self.open_in_editor(file_names = [], dir: nil)
7
- editor = Work::Md::Config.editor
8
- work_dir = dir || Work::Md::Config.work_dir
9
-
10
- ::FileUtils.cd(work_dir) do
11
- ENV['EDITOR'] = editor unless editor.nil?
12
-
13
- return ::TTY::Editor.open(file_names[0]) if file_names[1].nil?
14
-
15
- ::TTY::Editor.open(file_names[0], file_names[1])
16
- end
17
- end
18
-
19
6
  def self.create_and_open_parsed(parser)
20
7
  parser.freeze
21
8
 
@@ -76,6 +63,19 @@ module Work
76
63
 
77
64
  Work::Md::File.open_in_editor([parsed_file_path])
78
65
  end
66
+
67
+ def self.open_in_editor(file_names = [])
68
+ editor = Work::Md::Config.editor
69
+ work_dir = Work::Md::Config.work_dir
70
+
71
+ ::FileUtils.cd(work_dir) do
72
+ ENV['EDITOR'] = editor unless editor.nil?
73
+
74
+ return ::TTY::Editor.open(file_names[0]) if file_names[1].nil?
75
+
76
+ ::TTY::Editor.open(*file_names)
77
+ end
78
+ end
79
79
  end
80
80
  end
81
81
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Work
4
4
  module Md
5
- VERSION = '0.4.8'
5
+ VERSION = '0.4.9'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: work-md
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Fernandez Teixeira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-29 00:00:00.000000000 Z
11
+ date: 2022-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-box