Skip to content

Commit

Permalink
Add shortdate format options
Browse files Browse the repository at this point in the history
- NEW: shortdate format config options (run `doing config update` and see the new `shortdate_format` section in your config file)
  • Loading branch information
ttscoff committed Aug 17, 2023
1 parent 0914c38 commit 89bda6d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
doing (2.1.83)
doing (2.1.84)
chronic (~> 0.10, >= 0.10.2)
deep_merge (~> 1.2, >= 1.2.1)
gli (~> 2.20, >= 2.20.1)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _If you're one of the rare people like me who find this useful, feel free to

<!--README-->

The current version of `doing` is <!--VER-->2.1.82<!--END VER-->.
The current version of `doing` is <!--VER-->2.1.83<!--END VER-->.

Find all of the documentation in the [doing wiki][wiki].

Expand Down
4 changes: 2 additions & 2 deletions lib/doing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def original_options
## @param default A default value to return if the
## provided path returns nil result
##
def setting(keypath, default = nil)
def setting(keypath, default = nil, exact: false)
cfg = config.settings
case keypath
when Array
cfg.dig(*keypath) || default
when String
unless keypath =~ /^[.*]?$/
real_path = config.resolve_key_path(keypath, create: false)
real_path = config.resolve_key_path(keypath, create: false, distance: 0, exact: exact)
return default unless real_path&.count&.positive?

cfg = cfg.dig(*real_path)
Expand Down
17 changes: 15 additions & 2 deletions lib/doing/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def force_answer
'timer_format' => 'text',
'interval_format' => 'text',

'order' => 'asc',

'shortdate_format' => {
'today' => '%_I:%M%P',
'this_week' => '%a %_I:%M%P',
'this_month' => '%m/%d %_I:%M%P',
'older' => '%m/%d/%y %_I:%M%P'
},

'templates' => {
'default' => {
'date_format' => '%Y-%m-%d %H:%M',
Expand Down Expand Up @@ -200,7 +209,7 @@ def fetch(*path, default)
## matched, first match wins)
## @return [Array] ordered array of resolved keys
##
def resolve_key_path(keypath, create: false, distance: 2)
def resolve_key_path(keypath, create: false, distance: 2, exact: false)
cfg = @settings
real_path = []
unless keypath =~ /^[.*]?$/
Expand All @@ -211,7 +220,11 @@ def resolve_key_path(keypath, create: false, distance: 2)
new_cfg = nil

if cfg.is_a?(Hash)
matches = cfg.select { |key, val| key =~ path.to_rx(distance: distance) }
matches = if exact
cfg.select { |key, _| key == path }
else
cfg.select { |key, _| key =~ path.to_rx(distance: distance) }
end
if matches.count.positive?
shortest = matches.keys.group_by(&:length).min.last[0]
real_path << shortest
Expand Down
3 changes: 2 additions & 1 deletion lib/doing/template_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def fill(placeholder, value, wrap_width: 0, color: '', tags_color: '', reset: ''
pad = m['width'].to_i
mark = m['mchar'] || ''
if placeholder == 'shortdate' && m['width'].nil?
pad = 13
fmt_string = Doing.setting('shortdate_format.older', '%m/%d/%y %_I:%M%P', exact: true)
pad = Date.today.strftime(fmt_string).length
end
indent = nil
if m['ichar']
Expand Down
8 changes: 4 additions & 4 deletions lib/doing/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class ::Time
#
def relative_date
if self > Date.today.to_time
strftime('%_I:%M%P')
strftime(Doing.setting('shortdate_format.today', '%_I:%M%P', exact: true))
elsif self > (Date.today - 6).to_time
strftime('%a %_I:%M%P')
strftime(Doing.setting('shortdate_format.this_week', '%a %_I:%M%P', exact: true))
elsif year == Date.today.year || (year + 1 == Date.today.year && month > Date.today.month)
strftime('%m/%d %_I:%M%P')
strftime(Doing.setting('shortdate_format.this_month', '%m/%d %_I:%M%P', exact: true))
else
strftime('%m/%d/%y %_I:%M%P')
strftime(Doing.setting('shortdate_format.older', '%m/%d/%y %_I:%M%P', exact: true))
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/doing/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Doing
VERSION = '2.1.83'
VERSION = '2.1.84'
end

0 comments on commit 89bda6d

Please sign in to comment.