From 89bda6d7b5dd1f274e184cb721457c5a90ccf5a4 Mon Sep 17 00:00:00 2001 From: Brett Terpstra Date: Thu, 17 Aug 2023 09:48:20 -0500 Subject: [PATCH] Add shortdate format options - NEW: shortdate format config options (run `doing config update` and see the new `shortdate_format` section in your config file) --- Gemfile.lock | 2 +- README.md | 2 +- lib/doing.rb | 4 ++-- lib/doing/configuration.rb | 17 +++++++++++++++-- lib/doing/template_string.rb | 3 ++- lib/doing/time.rb | 8 ++++---- lib/doing/version.rb | 2 +- 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 21a177ac..5e3dc9cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/README.md b/README.md index 74642b7a..f3e3152f 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ _If you're one of the rare people like me who find this useful, feel free to -The current version of `doing` is 2.1.82. +The current version of `doing` is 2.1.83. Find all of the documentation in the [doing wiki][wiki]. diff --git a/lib/doing.rb b/lib/doing.rb index 6c9b17f8..7ec6d248 100644 --- a/lib/doing.rb +++ b/lib/doing.rb @@ -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) diff --git a/lib/doing/configuration.rb b/lib/doing/configuration.rb index 66f87997..fbb32262 100644 --- a/lib/doing/configuration.rb +++ b/lib/doing/configuration.rb @@ -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', @@ -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 =~ /^[.*]?$/ @@ -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 diff --git a/lib/doing/template_string.rb b/lib/doing/template_string.rb index ba1817d0..713c451c 100644 --- a/lib/doing/template_string.rb +++ b/lib/doing/template_string.rb @@ -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'] diff --git a/lib/doing/time.rb b/lib/doing/time.rb index ef0ceaca..5179487b 100644 --- a/lib/doing/time.rb +++ b/lib/doing/time.rb @@ -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 diff --git a/lib/doing/version.rb b/lib/doing/version.rb index a324b5f1..e87dacb4 100644 --- a/lib/doing/version.rb +++ b/lib/doing/version.rb @@ -1,3 +1,3 @@ module Doing - VERSION = '2.1.83' + VERSION = '2.1.84' end