Skip to content

Commit

Permalink
feat(reports): ✨ add -E to show time entry stop times (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed Jul 2, 2023
1 parent 267e2a0 commit 349f152
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Settings
internal const string ClearDescriptionFlag = "-C";
internal const string TimeSpanFlag = "-t";
internal const string TimeSpanEndFlag = "-T";
internal const string ShowStopFlag = "-E";

internal const string NoProjectName = "No Project";
internal const string NoClientName = "No Client";
Expand Down
64 changes: 62 additions & 2 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,12 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
}
}

subGroupQuery = new TransformedQuery(query)
.After(ArgumentIndices.SubGroupingName)
.RemoveAll(Settings.ShowStopFlag)
.ToString(TransformedQuery.Escaping.Unescaped);
bool hasShowStopFlag = query.SearchTerms.Contains(Settings.ShowStopFlag);

var filteredTimeEntries = report.SelectMany(timeEntryGroup =>
{
return (string.IsNullOrEmpty(subGroupQuery))
Expand All @@ -2374,11 +2380,17 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
subResults = subResults.Concat(filteredTimeEntries.Select(timeEntry =>
{
DateTimeOffset startDate = timeEntry.StartDate.ToLocalTime();
DateTimeOffset? stopDate = timeEntry.StopDate?.ToLocalTime();
string stopString = (stopDate is not null)
? $"{stopDate?.ToString("t")} {stopDate?.ToString("d")}"
: "now";
return new Result
{
Title = timeEntry.GetDescription(),
SubTitle = $"{timeEntry.DetailedElapsed} ({timeEntry.HumanisedStart} at {startDate.ToString("t")} {startDate.ToString("ddd")} {startDate.ToString("m")})",
SubTitle = (hasShowStopFlag)
? $"{timeEntry.DetailedElapsed} ({startDate.ToString("t")} {startDate.ToString("d")} to {stopString})"
: $"{timeEntry.DetailedElapsed} ({timeEntry.HumanisedStart} at {startDate.ToString("t")} {startDate.ToString("ddd")} {startDate.ToString("m")})",
IcoPath = this._colourIconProvider.GetColourIcon(project?.Colour, "reports.png"),
AutoCompleteText = $"{query.ActionKeyword} {Settings.ReportsCommand} {spanArgument} {groupingArgument} {project?.KebabName ?? "no-project"} {timeEntry.GetDescription(escapePotentialFlags: true)}",
Score = timeEntry.GetScoreByStart(),
Expand All @@ -2390,6 +2402,24 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
},
};
}));

if (this._settings.ShowUsageTips && !hasShowStopFlag)
{
subResults = subResults.Append(new Result
{
// ! see #79... also Flow-Launcher/Flow.Launcher#2201 and Flow-Launcher/Flow.Launcher#2202
Title = $"{Settings.UsageTipTitle}{new string('\u200B', subGroupQuery.Length)}",
SubTitle = $"Use {Settings.ShowStopFlag} to display time entry stop times",
IcoPath = "tip.png",
AutoCompleteText = $"{query.ActionKeyword} {query.Search} {Settings.ShowStopFlag} ",
Score = 1,
Action = c =>
{
this._context.API.ChangeQuery($"{query.ActionKeyword} {query.Search} {Settings.ShowStopFlag} ");
return false;
}
});
}
}
else
{
Expand Down Expand Up @@ -2581,6 +2611,12 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
}
}

groupQuery = new TransformedQuery(query)
.After(ArgumentIndices.GroupingName)
.RemoveAll(Settings.ShowStopFlag)
.ToString(TransformedQuery.Escaping.Unescaped);
bool hasShowStopFlag = query.SearchTerms.Contains(Settings.ShowStopFlag);

var filteredTimeEntries = report.SelectMany(timeEntryGroup =>
{
return (string.IsNullOrEmpty(groupQuery))
Expand All @@ -2593,11 +2629,17 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
subResults = subResults.Concat(filteredTimeEntries.Select(timeEntry =>
{
DateTimeOffset startDate = timeEntry.StartDate.ToLocalTime();
DateTimeOffset? stopDate = timeEntry.StopDate?.ToLocalTime();
string stopString = (stopDate is not null)
? $"{stopDate?.ToString("t")} {stopDate?.ToString("d")}"
: "now";
return new Result
{
Title = timeEntry.GetDescription(),
SubTitle = $"{timeEntry.DetailedElapsed} ({timeEntry.HumanisedStart} at {startDate.ToString("t")} {startDate.ToString("ddd")} {startDate.ToString("m")})",
SubTitle = (hasShowStopFlag)
? $"{timeEntry.DetailedElapsed} ({startDate.ToString("t")} {startDate.ToString("d")} to {stopString})"
: $"{timeEntry.DetailedElapsed} ({timeEntry.HumanisedStart} at {startDate.ToString("t")} {startDate.ToString("ddd")} {startDate.ToString("m")})",
IcoPath = this._colourIconProvider.GetColourIcon(timeEntry.Project?.Colour, "reports.png"),
AutoCompleteText = $"{query.ActionKeyword} {Settings.ReportsCommand} {spanArgument} {groupingArgument} {timeEntry.GetDescription(escapePotentialFlags: true)}",
Score = timeEntry.GetScoreByStart(),
Expand All @@ -2609,6 +2651,24 @@ internal async ValueTask<List<Result>> RequestViewReports(CancellationToken toke
},
};
}));

if (this._settings.ShowUsageTips && !hasShowStopFlag)
{
subResults = subResults.Append(new Result
{
// ! see #79... also Flow-Launcher/Flow.Launcher#2201 and Flow-Launcher/Flow.Launcher#2202
Title = $"{Settings.UsageTipTitle}{new string('\u200B', groupQuery.Length)}",
SubTitle = $"Use {Settings.ShowStopFlag} to display time entry stop times",
IcoPath = "tip.png",
AutoCompleteText = $"{query.ActionKeyword} {query.Search} {Settings.ShowStopFlag} ",
Score = 1,
Action = c =>
{
this._context.API.ChangeQuery($"{query.ActionKeyword} {query.Search} {Settings.ShowStopFlag} ");
return false;
}
});
}
}
else
{
Expand Down
19 changes: 13 additions & 6 deletions src/TransformedSearch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text.RegularExpressions;
using System.Linq;

namespace Flow.Launcher.Plugin.TogglTrack
{
Expand Down Expand Up @@ -29,9 +30,9 @@ private TransformedQuery(string[] searchTerms)
this.SearchTerms = searchTerms;
}

public int IndexOf(string value)
public int IndexOf(string term)
{
return Array.IndexOf(this.SearchTerms, value);
return Array.IndexOf(this.SearchTerms, term);
}

private TransformedQuery Slice(int? start = null, int? end = null)
Expand All @@ -45,19 +46,19 @@ public TransformedQuery To(int index)
return this.Slice(end: index);
}

public TransformedQuery To(string value)
public TransformedQuery To(string term)
{
return this.Slice(end: this.IndexOf(value));
return this.Slice(end: this.IndexOf(term));
}

public TransformedQuery After(int index)
{
return this.Slice(start: index);
}

public TransformedQuery After(string value)
public TransformedQuery After(string term)
{
return this.Slice(start: this.IndexOf(value) + 1);
return this.Slice(start: this.IndexOf(term) + 1);
}

public TransformedQuery Between(int after, int to)
Expand All @@ -78,6 +79,12 @@ public TransformedQuery Between(int start, string to)
);
}

public TransformedQuery RemoveAll(string term)
{
this.SearchTerms = this.SearchTerms.Where(searchTerm => searchTerm != term).ToArray();
return this;
}

private string UnescapeSearch()
{
return Regex.Replace(this.ToString(), @"(\\(?!\\))", string.Empty);
Expand Down

0 comments on commit 349f152

Please sign in to comment.