pday 0.2.0 → 0.3.0

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.
Files changed (2) hide show
  1. data/lib/pday.rb +57 -24
  2. metadata +3 -3
@@ -16,17 +16,39 @@ def isdate testdate
16
16
  end
17
17
  end
18
18
 
19
+ # REVISIT THIS TO MAKE THE HEADER TEXT CUSTOMIZABLE
19
20
  # Default to a Header 1 in Markdown:
20
21
  def prepopulate headertext
21
22
  return "# "+headertext
22
23
  end
23
24
 
25
+ def formatdate requesteddate, dateformat
26
+ if requesteddate=='yesterday' || requesteddate=='y'
27
+ formateddate=(Time.now-86400)
28
+ elsif requesteddate=='yy' # Day before yesterday
29
+ formateddate=(Time.now-172800)
30
+ elsif requesteddate=='tomorrow' || requesteddate=='t'
31
+ formateddate=(Time.now+86400)
32
+ elsif requesteddate=='yyy' # Three days ago
33
+ formateddate=(Time.now-259200)
34
+ else
35
+ if isdate(requesteddate)
36
+ formateddate=Date.parse(requesteddate)
37
+ else
38
+ return nil
39
+ end
40
+ end
41
+ return formateddate.strftime(dateformat)
42
+ end
24
43
 
25
44
  main do
45
+
46
+ #Set up default options
26
47
  default_options=Hash.new
27
48
  default_options['suffix-delimit'] = '-'
28
49
  default_options['format'] = 'md'
29
50
  default_options['editor'] = 'vim'
51
+ default_options['date-format'] = "%Y.%m.%d"
30
52
  # Load the .dayconfig file and merge in options there:
31
53
  CONFIG_FILE = File.join(ENV['HOME'],'.dayconfig')
32
54
  if File.exists? CONFIG_FILE
@@ -38,50 +60,61 @@ main do
38
60
  end
39
61
  default_options.merge!(options)
40
62
  options.merge!(default_options)
63
+
64
+
41
65
  # Check if the diary directory is found:
42
66
  unless options['path']
43
67
  puts "Could not find path to your diary. Did you configure the .dayconfig file?"
44
68
  exit
45
69
  end
70
+
46
71
  # Use today's date unless there is one supplied
47
72
  unless options[:date]
48
- entrydate=Time.now.strftime("%Y.%m.%d")
73
+ entrydate=Time.now.strftime(options['date-format'])
49
74
  else
50
- entrydate=options['date']
75
+ # Check date
76
+ finaldate=formatdate(options['date'],options['date-format'])
77
+ unless finaldate!=nil
78
+ puts "DATE must be a valid date. For example '2013.1.15' or '01/15/2013' or 'Jan 1, 2013' or 'yesterday'"
79
+ exit
80
+ else
81
+ entrydate=finaldate
82
+ end
51
83
  end
84
+
52
85
  # Check to see if there is a suffix, and if so, add the suffix delimiter:
53
86
  if options[:suffix]
54
87
  suffix=options['suffix-delimit']+options['suffix']
55
88
  else
56
89
  suffix=''
57
90
  end
58
- # Build the command:
59
- filepath=options['path']+entrydate+suffix+'.'+options['format']
60
- puts "Creating new entry for: "+entrydate
61
- `echo '#{prepopulate(entrydate)}' > '#{filepath}'`
62
- puts "Editing entry..."
63
- `#{options['editor']} '#{filepath}'`
64
- end
65
91
 
66
- version '0.2'
67
- description 'Simple plain text diary management script.'
68
-
69
- opts.on("-d DATE","--date","Supply an explicit date. Also accepts 'yesterday' or 'y'") do |date|
70
- if date=='yesterday' || date=='y'
71
- date=(Time.now-86400).strftime("%Y.%m.%d")
72
- end
73
92
 
74
- unless isdate(date)
75
- puts "DATE must be a valid date. For example \
76
- '2013.1.15' or '01/15/2013' or 'Jan 1, 2013' or \
77
- 'yesterday'"
93
+ # Build the command to create the file with the header and edit it:
94
+ filepath=options['path']+entrydate+suffix+'.'+options['format']
95
+ if File.file?(filepath)
96
+ unless options[:force]
97
+ puts "File already exists. Will open existing file."
98
+ else
99
+ puts "File already exists, replacing it."
100
+ `echo '#{prepopulate(entrydate)}' > "#{filepath}"`
101
+ end
102
+ else
103
+ puts "Creating new entry for: "+entrydate
104
+ `echo '#{prepopulate(entrydate)}' > "#{filepath}"` # NEED TO ESCAPE FILEPATH FOR SPACES
78
105
  end
79
- options[:date]=true
80
- options['date']=Date.parse(date).strftime("%Y.%m.%d")
106
+ puts "Editing entry: #{filepath}"
107
+ `#{options['editor']} "#{filepath}"`
81
108
  end
82
109
 
83
- on("-f FORMAT","--format","Set format suffix")
84
- on("-s SUFFIX","--suffix","Add suffix")
110
+ version '0.3'
111
+ description 'Simple plain text diary management script.'
112
+
113
+ on("-t FORMAT","--format","Set format suffix")
114
+ on("-f","--force","If a file already exists with that name, replace it instead of editing existing")
115
+ on("-s SUFFIX","--suffix","Add suffix to file name (before format suffix)")
85
116
  on("-e EDITOR","--editor","Set the editor, vim, emacs, nano, etc.")
117
+ on("-d DATE","--date","Supply an explicit date. Also accepts 'yesterday' or 'y'")
118
+
86
119
 
87
120
  go!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-13 00:00:00.000000000 Z
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Simple shortcut script for creating a new plain text diary entry with
15
15
  today's date.
@@ -42,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  version: '0'
43
43
  requirements: []
44
44
  rubyforge_project:
45
- rubygems_version: 1.8.25
45
+ rubygems_version: 1.8.15
46
46
  signing_key:
47
47
  specification_version: 3
48
48
  summary: Simple plain text diary management script.