Skip to content

Commit

Permalink
feat(ux): 🚸 autocomplete command on whitespace (#52)
Browse files Browse the repository at this point in the history
examples:
`tgl a ` -> `tgl start `
`tgl e ` -> `tgl edit `
`tgl o ` -> `tgl stop `
  • Loading branch information
JamesNZL committed Jun 10, 2023
1 parent a121db9 commit dcf4161
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public class Main : IAsyncPlugin, ISettingProvider

internal TogglTrack? _togglTrack;

private (
string LastCommand,
// ! this is needed as tuples must have minimum 2 elements
bool? _null
) _state = (
LastCommand: string.Empty,
null
);

public static string ExtractFromQuery(Query query, int index)
{
return (index == 1)
Expand Down Expand Up @@ -81,7 +90,20 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
return await this._togglTrack.GetDefaultHotKeys(prefetch: true);
}

return (query.FirstSearch.ToLower()) switch
string command = query.FirstSearch;
if (!Settings.Commands.Contains(command) && command == this._state.LastCommand)
{
command = (await this._togglTrack.GetDefaultHotKeys())
.GroupBy(result => this._context!.API.FuzzySearch(query.FirstSearch, result.Title).Score)
.MaxBy(group => group.Key)
?.MaxBy(result => result.Score)
?.Title
?? query.FirstSearch;
this._context!.API.ChangeQuery($"{query.ActionKeyword} {command} ");
}
this._state.LastCommand = command;

return (command.ToLower()) switch
{
Settings.StartCommand => await this._togglTrack.RequestStartEntry(token, query),
Settings.StopCommand => await this._togglTrack.RequestStopEntry(token, query),
Expand Down
10 changes: 10 additions & 0 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public class Settings
internal const string ReportsCommand = "reports";
internal const string BrowserCommand = "browser";
internal const string RefreshCommand = "refresh";
internal static readonly string[] Commands = new string[] {
StartCommand,
StopCommand,
ContinueCommand,
EditCommand,
DeleteCommand,
ReportsCommand,
BrowserCommand,
RefreshCommand,
};

internal const string EditProjectFlag = "-p";
internal const string TimeSpanFlag = "-t";
Expand Down
16 changes: 8 additions & 8 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Start a new time entry",
IcoPath = "start.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.StartCommand} ",
Score = 100,
Score = 15000,
Action = c =>
{
this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.StartCommand} ");
Expand All @@ -494,7 +494,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Continue previous time entry",
IcoPath = "continue.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.ContinueCommand} ",
Score = 80,
Score = 12500,
Action = c =>
{
this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.ContinueCommand} ");
Expand All @@ -507,7 +507,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Edit previous time entry",
IcoPath = "edit.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.EditCommand} ",
Score = 60,
Score = 6000,
Action = c =>
{
this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.EditCommand} ");
Expand All @@ -520,7 +520,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Delete previous time entry",
IcoPath = "delete.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.DeleteCommand} ",
Score = 20,
Score = 4000,
Action = c =>
{
this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.DeleteCommand} ");
Expand All @@ -533,7 +533,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "View tracked time reports",
IcoPath = "reports.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.ReportsCommand} ",
Score = 5,
Score = 2000,
Action = c =>
{
this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.ReportsCommand} ");
Expand All @@ -546,7 +546,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Open Toggl Track in browser",
IcoPath = "browser.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.BrowserCommand} ",
Score = -50,
Score = 100,
Action = c =>
{
this._context.API.OpenUrl(new Uri(@"https://track.toggl.com/timer"));
Expand All @@ -559,7 +559,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Refresh plugin cache",
IcoPath = "refresh.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.RefreshCommand} ",
Score = -100,
Score = 5,
Action = c =>
{
this.RefreshCache(refreshMe: true);
Expand All @@ -579,7 +579,7 @@ internal async ValueTask<List<Result>> GetDefaultHotKeys(bool prefetch = false)
SubTitle = "Stop current time entry",
IcoPath = "stop.png",
AutoCompleteText = $"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.StopCommand} ",
Score = 130,
Score = 15050,
Action = c =>
{
this._context.API.ChangeQuery($"{this._context.CurrentPluginMetadata.ActionKeyword} {Settings.StopCommand} ");
Expand Down

0 comments on commit dcf4161

Please sign in to comment.