Skip to content

Commit 335f83b

Browse files
committed
Recent pages improvement
1 parent eebb491 commit 335f83b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

Images/recent_page.png

8.51 KB
Loading

Main.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class OneNote : IPlugin, IContextMenu
1313
private readonly string unavailableIconPath = "Images/unavailable.png";
1414
private readonly string syncIconPath = "Images/refresh.png";
1515
private readonly string recentIconPath = "Images/recent.png";
16+
private readonly string recentPageIconPath = "Images/recent_page.png";
1617

1718
private readonly string structureKeyword = "nb:\\";
1819
private readonly string recentKeyword = "rcntpgs:";
@@ -102,13 +103,17 @@ public List<Result> Query(Query query)
102103
{
103104
int count = recentPagesCount;
104105
if (query.FirstSearch.Length > recentKeyword.Length && int.TryParse(query.FirstSearch[recentKeyword.Length..], out int userChosenCount))
105-
{
106106
count = userChosenCount;
107-
}
108-
107+
109108
return OneNoteProvider.PageItems.OrderByDescending(pg => pg.LastModified)
110-
.Select(pg => CreatePageResult(pg, null))
111109
.Take(count)
110+
.Select(pg =>
111+
{
112+
Result result = CreatePageResult(pg, null);
113+
result.SubTitle = $"{GetLastEdited(DateTime.Now - pg.LastModified)}\t{result.SubTitle}";
114+
result.IcoPath = recentPageIconPath;
115+
return result;
116+
})
112117
.ToList();
113118
}
114119

@@ -350,5 +355,29 @@ private Result CreateNotebookResult(IOneNoteExtNotebook notebook, List<int> high
350355
},
351356
};
352357
}
358+
private static string GetLastEdited(TimeSpan diff)
359+
{
360+
string lastEdited = "Last editied ";
361+
if (PluralCheck(diff.TotalDays, "day", ref lastEdited)
362+
|| PluralCheck(diff.TotalHours, "hour", ref lastEdited)
363+
|| PluralCheck(diff.TotalMinutes, "min", ref lastEdited)
364+
|| PluralCheck(diff.TotalSeconds, "sec", ref lastEdited))
365+
return lastEdited;
366+
else
367+
return lastEdited += "Now.";
368+
bool PluralCheck(double totalTime, string timeType, ref string lastEdited)
369+
{
370+
var roundedTime = (int)Math.Round(totalTime);
371+
if(roundedTime > 0)
372+
{
373+
string plural = roundedTime == 1 ? "" : "s";
374+
lastEdited += $"{roundedTime} {timeType}{plural} ago.";
375+
return true;
376+
}
377+
else
378+
return false;
379+
380+
}
381+
}
353382
}
354383
}

0 commit comments

Comments
 (0)