diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs new file mode 100644 index 000000000000..267ab43989f0 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs @@ -0,0 +1,104 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Globalization; +using System.Linq; +using Microsoft.CmdPal.Ext.TimeDate.Helpers; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace Microsoft.CmdPal.Ext.TimeDate; + +internal sealed partial class FallbackTimeDateItem : FallbackCommandItem +{ + private readonly HashSet _validOptions; + private SettingsManager _settingsManager; + + public FallbackTimeDateItem(SettingsManager settings) + : base(new NoOpCommand(), Resources.Microsoft_plugin_timedate_fallback_display_title) + { + Title = string.Empty; + Subtitle = string.Empty; + _settingsManager = settings; + _validOptions = new(StringComparer.OrdinalIgnoreCase) + { + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagDate", CultureInfo.CurrentCulture), + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagDateNow", CultureInfo.CurrentCulture), + + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagTime", CultureInfo.CurrentCulture), + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagTimeNow", CultureInfo.CurrentCulture), + + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagFormat", CultureInfo.CurrentCulture), + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagFormatNow", CultureInfo.CurrentCulture), + + Resources.ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagWeek", CultureInfo.CurrentCulture), + }; + } + + public override void UpdateQuery(string query) + { + if (!_settingsManager.EnableFallbackItems || string.IsNullOrWhiteSpace(query) || !IsValidQuery(query)) + { + Title = string.Empty; + Subtitle = string.Empty; + Command = new NoOpCommand(); + return; + } + + var availableResults = AvailableResultsList.GetList(false, _settingsManager); + ListItem result = null; + var maxScore = 0; + + foreach (var f in availableResults) + { + var score = f.Score(query, f.Label, f.AlternativeSearchTag); + if (score > maxScore) + { + maxScore = score; + result = f.ToListItem(); + } + } + + if (result != null) + { + Title = result.Title; + Subtitle = result.Subtitle; + Icon = result.Icon; + } + else + { + Title = string.Empty; + Subtitle = string.Empty; + Command = new NoOpCommand(); + } + } + + private bool IsValidQuery(string query) + { + if (_validOptions.Contains(query)) + { + return true; + } + + foreach (var option in _validOptions) + { + if (option == null) + { + continue; + } + + var parts = option.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + + if (parts.Any(part => string.Equals(part, query, StringComparison.OrdinalIgnoreCase))) + { + return true; + } + } + + return false; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/AvailableResultsList.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/AvailableResultsList.cs index bc6a3b972c61..60ccaf38b57f 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/AvailableResultsList.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/AvailableResultsList.cs @@ -33,6 +33,7 @@ internal static List GetList(bool isKeywordSearch, SettingsMana var dateTimeNowUtc = dateTimeNow.ToUniversalTime(); var firstWeekRule = firstWeekOfYear ?? TimeAndDateHelper.GetCalendarWeekRule(settings.FirstWeekOfYear); var firstDayOfTheWeek = firstDayOfWeek ?? TimeAndDateHelper.GetFirstDayOfWeek(settings.FirstDayOfWeek); + var weekOfYear = calendar.GetWeekOfYear(dateTimeNow, firstWeekRule, firstDayOfTheWeek); results.AddRange(new[] { @@ -59,14 +60,20 @@ internal static List GetList(bool isKeywordSearch, SettingsMana AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"), IconType = ResultIconType.DateTime, }, + new AvailableResult() + { + Value = weekOfYear.ToString(CultureInfo.CurrentCulture), + Label = Resources.Microsoft_plugin_timedate_WeekOfYear, + AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"), + IconType = ResultIconType.Date, + }, }); - if (isKeywordSearch || !settings.OnlyDateTimeNowGlobal) + if (isKeywordSearch) { // We use long instead of int for unix time stamp because int is too small after 03:14:07 UTC 2038-01-19 var unixTimestamp = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeSeconds(); var unixTimestampMilliseconds = ((DateTimeOffset)dateTimeNowUtc).ToUnixTimeMilliseconds(); - var weekOfYear = calendar.GetWeekOfYear(dateTimeNow, firstWeekRule, firstDayOfTheWeek); var era = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow)); var eraShort = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow)); @@ -251,13 +258,6 @@ internal static List GetList(bool isKeywordSearch, SettingsMana IconType = ResultIconType.Date, }, new AvailableResult() - { - Value = weekOfYear.ToString(CultureInfo.CurrentCulture), - Label = Resources.Microsoft_plugin_timedate_WeekOfYear, - AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"), - IconType = ResultIconType.Date, - }, - new AvailableResult() { Value = DateTimeFormatInfo.CurrentInfo.GetMonthName(dateTimeNow.Month), Label = Resources.Microsoft_plugin_timedate_Month, diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/SettingsManager.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/SettingsManager.cs index 5b30a4816f83..7b351fe3b87c 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/SettingsManager.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/SettingsManager.cs @@ -75,11 +75,11 @@ public class SettingsManager : JsonSettingsManager Resources.Microsoft_plugin_timedate_SettingFirstDayOfWeek, _firstDayOfWeekChoices); - private readonly ToggleSetting _onlyDateTimeNowGlobal = new( - Namespaced(nameof(OnlyDateTimeNowGlobal)), - Resources.Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal, - Resources.Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal_Description, - true); // TODO -- double check default value + private readonly ToggleSetting _enableFallbackItems = new( + Namespaced(nameof(EnableFallbackItems)), + Resources.Microsoft_plugin_timedate_SettingEnableFallbackItems, + Resources.Microsoft_plugin_timedate_SettingEnableFallbackItems_Description, + true); private readonly ToggleSetting _timeWithSeconds = new( Namespaced(nameof(TimeWithSecond)), @@ -93,12 +93,6 @@ public class SettingsManager : JsonSettingsManager Resources.Microsoft_plugin_timedate_SettingDateWithWeekday_Description, false); // TODO -- double check default value - private readonly ToggleSetting _hideNumberMessageOnGlobalQuery = new( - Namespaced(nameof(HideNumberMessageOnGlobalQuery)), - Resources.Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery, - Resources.Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery, - true); // TODO -- double check default value - private readonly TextSetting _customFormats = new( Namespaced(nameof(CustomFormats)), Resources.Microsoft_plugin_timedate_Setting_CustomFormats, @@ -145,14 +139,12 @@ public int FirstDayOfWeek } } - public bool OnlyDateTimeNowGlobal => _onlyDateTimeNowGlobal.Value; + public bool EnableFallbackItems => _enableFallbackItems.Value; public bool TimeWithSecond => _timeWithSeconds.Value; public bool DateWithWeekday => _dateWithWeekday.Value; - public bool HideNumberMessageOnGlobalQuery => _hideNumberMessageOnGlobalQuery.Value; - public List CustomFormats => _customFormats.Value.Split(TEXTBOXNEWLINE).ToList(); internal static string SettingsJsonPath() @@ -168,10 +160,7 @@ public SettingsManager() { FilePath = SettingsJsonPath(); - /* The following two settings make no sense with current CmdPal behavior. - Settings.Add(_onlyDateTimeNowGlobal); - Settings.Add(_hideNumberMessageOnGlobalQuery); */ - + Settings.Add(_enableFallbackItems); Settings.Add(_timeWithSeconds); Settings.Add(_dateWithWeekday); Settings.Add(_firstWeekOfYear); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/TimeDateCalculator.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/TimeDateCalculator.cs index eb4eed18cd33..38f417ad5bfb 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/TimeDateCalculator.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Helpers/TimeDateCalculator.cs @@ -40,7 +40,7 @@ public static List ExecuteSearch(SettingsManager settings, string quer var lastInputParsingErrorMsg = string.Empty; // Switch search type - if (isEmptySearchInput || (!isKeywordSearch && settings.OnlyDateTimeNowGlobal)) + if (isEmptySearchInput || (!isKeywordSearch)) { // Return all results for system time/date on empty keyword search // or only time, date and now results for system time on global queries if the corresponding setting is enabled @@ -91,23 +91,6 @@ public static List ExecuteSearch(SettingsManager settings, string quer } } - /*htcfreek:Code obsolete with current CmdPal behavior. - // If search term is only a number that can't be parsed return an error message - if (!isEmptySearchInput && results.Count == 0 && Regex.IsMatch(query, @"\w+\d+.*$") && !query.Any(char.IsWhiteSpace) && (TimeAndDateHelper.IsSpecialInputParsing(query) || !Regex.IsMatch(query, @"\d+[\.:/]\d+"))) - { - // Without plugin key word show only if message is not hidden by setting - if (!settings.HideNumberMessageOnGlobalQuery) - { - var er = ResultHelper.CreateInvalidInputErrorResult(); - if (!string.IsNullOrEmpty(lastInputParsingErrorMsg)) - { - er.Details = new Details() { Body = lastInputParsingErrorMsg }; - } - - results.Add(er); - } - } */ - if (results.Count == 0) { var er = ResultHelper.CreateInvalidInputErrorResult(); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.Designer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.Designer.cs index d62d76eefdd8..a21759d10aa6 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.Designer.cs @@ -213,6 +213,15 @@ public static string Microsoft_plugin_timedate_Excel1904 { } } + /// + /// Looks up a localized string similar to Open Time Data Command. + /// + public static string Microsoft_plugin_timedate_fallback_display_title { + get { + return ResourceManager.GetString("Microsoft_plugin_timedate_fallback_display_title", resourceCulture); + } + } + /// /// Looks up a localized string similar to Date and time in filename-compatible format. /// @@ -609,6 +618,15 @@ public static string Microsoft_plugin_timedate_SearchTagTimeNow { } } + /// + /// Looks up a localized string similar to Current Week; Calendar week; Week of the year; Week. + /// + public static string Microsoft_plugin_timedate_SearchTagWeek { + get { + return ResourceManager.GetString("Microsoft_plugin_timedate_SearchTagWeek", resourceCulture); + } + } + /// /// Looks up a localized string similar to Second. /// @@ -663,6 +681,24 @@ public static string Microsoft_plugin_timedate_SettingDateWithWeekday_Descriptio } } + /// + /// Looks up a localized string similar to Enable fallback items for TimeDate (week, year, now, time, date). + /// + public static string Microsoft_plugin_timedate_SettingEnableFallbackItems { + get { + return ResourceManager.GetString("Microsoft_plugin_timedate_SettingEnableFallbackItems", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Show time and date results when typing keywords like "week", "year", "now", "time", or "date". + /// + public static string Microsoft_plugin_timedate_SettingEnableFallbackItems_Description { + get { + return ResourceManager.GetString("Microsoft_plugin_timedate_SettingEnableFallbackItems_Description", resourceCulture); + } + } + /// /// Looks up a localized string similar to First day of the week. /// @@ -780,33 +816,6 @@ public static string Microsoft_plugin_timedate_SettingFirstWeekRule_FirstFullWee } } - /// - /// Looks up a localized string similar to Hide 'Invalid number input' error message on global queries. - /// - public static string Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery { - get { - return ResourceManager.GetString("Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Show only 'Time', 'Date' and 'Now' result for system time on global queries. - /// - public static string Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal { - get { - return ResourceManager.GetString("Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Regardless of this setting, for global queries the first word of the query has to be a complete match.. - /// - public static string Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal_Description { - get { - return ResourceManager.GetString("Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal_Description", resourceCulture); - } - } - /// /// Looks up a localized string similar to Show time with seconds. /// diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.resx b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.resx index f1a36e2a906a..35862592ca7f 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.resx +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/Properties/Resources.resx @@ -265,15 +265,6 @@ This setting applies to the 'Date' and 'Now' result. - - Hide 'Invalid number input' error message on global queries - - - Show only 'Time', 'Date' and 'Now' result for system time on global queries - - - Regardless of this setting, for global queries the first word of the query has to be a complete match. - Show time with seconds @@ -433,4 +424,16 @@ Days in month + + Open Time Data Command + + + Current Week; Calendar week; Week of the year; Week + + + Enable fallback items for TimeDate (week, year, now, time, date) + + + Show time and date results when typing keywords like "week", "year", "now", "time", or "date" + \ No newline at end of file diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs index 05597c455360..d29356fa7770 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/TimeDateCommandsProvider.cs @@ -18,6 +18,7 @@ public partial class TimeDateCommandsProvider : CommandProvider private static readonly SettingsManager _settingsManager = new(); private static readonly CompositeFormat MicrosoftPluginTimedatePluginDescription = System.Text.CompositeFormat.Parse(Resources.Microsoft_plugin_timedate_plugin_description); private static readonly TimeDateExtensionPage _timeDateExtensionPage = new(_settingsManager); + private readonly FallbackTimeDateItem _fallbackTimeDateItem = new(_settingsManager); public TimeDateCommandsProvider() { @@ -45,4 +46,6 @@ private string GetTranslatedPluginDescription() } public override ICommandItem[] TopLevelCommands() => [_command]; + + public override IFallbackCommandItem[] FallbackCommands() => [_fallbackTimeDateItem]; }