Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<string> _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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal static List<AvailableResult> 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[]
{
Expand All @@ -59,14 +60,20 @@ internal static List<AvailableResult> 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));

Expand Down Expand Up @@ -251,13 +258,6 @@ internal static List<AvailableResult> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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,
Expand Down Expand Up @@ -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<string> CustomFormats => _customFormats.Value.Split(TEXTBOXNEWLINE).ToList();

internal static string SettingsJsonPath()
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static List<ListItem> 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
Expand Down Expand Up @@ -91,23 +91,6 @@ public static List<ListItem> 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();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove last left over too: Resources.Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal_Description

Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,6 @@
<data name="Microsoft_plugin_timedate_SettingDateWithWeekday_Description" xml:space="preserve">
<value>This setting applies to the 'Date' and 'Now' result.</value>
</data>
<data name="Microsoft_plugin_timedate_SettingHideNumberMessageOnGlobalQuery" xml:space="preserve">
<value>Hide 'Invalid number input' error message on global queries</value>
</data>
<data name="Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal" xml:space="preserve">
<value>Show only 'Time', 'Date' and 'Now' result for system time on global queries</value>
</data>
<data name="Microsoft_plugin_timedate_SettingOnlyDateTimeNowGlobal_Description" xml:space="preserve">
<value>Regardless of this setting, for global queries the first word of the query has to be a complete match.</value>
</data>
<data name="Microsoft_plugin_timedate_SettingTimeWithSeconds" xml:space="preserve">
<value>Show time with seconds</value>
</data>
Expand Down Expand Up @@ -433,4 +424,16 @@
<data name="Microsoft_plugin_timedate_DaysInMonth" xml:space="preserve">
<value>Days in month</value>
</data>
<data name="Microsoft_plugin_timedate_fallback_display_title" xml:space="preserve">
<value>Open Time Data Command</value>
</data>
<data name="Microsoft_plugin_timedate_SearchTagWeek" xml:space="preserve">
<value>Current Week; Calendar week; Week of the year; Week</value>
</data>
<data name="Microsoft_plugin_timedate_SettingEnableFallbackItems" xml:space="preserve">
<value>Enable fallback items for TimeDate (week, year, now, time, date)</value>
</data>
<data name="Microsoft_plugin_timedate_SettingEnableFallbackItems_Description" xml:space="preserve">
<value>Show time and date results when typing keywords like "week", "year", "now", "time", or "date"</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -45,4 +46,6 @@ private string GetTranslatedPluginDescription()
}

public override ICommandItem[] TopLevelCommands() => [_command];

public override IFallbackCommandItem[] FallbackCommands() => [_fallbackTimeDateItem];
}
Loading