Skip to content

Commit 1e2a4e0

Browse files
committed
Add and use Humanizer package
1 parent dc590ad commit 1e2a4e0

File tree

5 files changed

+16
-81
lines changed

5 files changed

+16
-81
lines changed

Flow.Launcher.Plugin.OneNote/Flow.Launcher.Plugin.OneNote.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
<ItemGroup>
2828
<PackageReference Include="Flow.Launcher.Plugin" Version="4.1.0" />
29+
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
2930
<PackageReference Include="ModernWpfUI" Version="0.9.6" />
3031
<PackageReference Include="Odotocodot.OneNote.Linq" Version="1.0.0" />
3132
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />

Flow.Launcher.Plugin.OneNote/Icons/IconProvider.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -178,43 +178,6 @@ public void ClearCachedIcons()
178178
OnPropertyChanged(nameof(CachedIconCount));
179179
}
180180

181-
182-
public string GetCachedIconsMemorySize()
183-
{
184-
var i = GeneratedImagesDirectoryInfo.EnumerateFiles()
185-
.Select(file => file.Length)
186-
.Aggregate(0L, (a, b) => a + b);
187-
188-
// Returns the human-readable file size for an arbitrary, 64-bit file size
189-
// The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
190-
// Get absolute value
191-
long absolute_i = Math.Abs(i);
192-
// Determine the suffix and readable value
193-
string suffix;
194-
double readable;
195-
switch (absolute_i)
196-
{
197-
case >= 0x40000000: // Gigabyte
198-
suffix = "GB";
199-
readable = i >> 20;
200-
break;
201-
case >= 0x100000: // Megabyte
202-
suffix = "MB";
203-
readable = i >> 10;
204-
break;
205-
case >= 0x400:
206-
suffix = "KB"; // Kilobyte
207-
readable = i;
208-
break;
209-
default:
210-
return i.ToString("0 B"); // Byte
211-
}
212-
// Divide by 1024 to get fractional value
213-
readable /= 1024;
214-
// Return formatted number with suffix
215-
return readable.ToString("0.## ") + suffix;
216-
}
217-
218181
public void Dispose()
219182
{
220183
settings.PropertyChanged -= OnIconThemeChanged;

Flow.Launcher.Plugin.OneNote/ResultCreator.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using Flow.Launcher.Plugin.OneNote.Icons;
4+
using Humanizer;
55
using Odotocodot.OneNote.Linq;
66

77
namespace Flow.Launcher.Plugin.OneNote
@@ -23,7 +23,8 @@ public ResultCreator(PluginInitContext context, Settings settings, IconProvider
2323
this.context = context;
2424
}
2525

26-
private static string GetNicePath(IOneNoteItem item, string separator = PathSeparator) => item.RelativePath.Replace(OneNoteApplication.RelativePathSeparator.ToString(), separator);
26+
private static string GetNicePath(IOneNoteItem item, string separator = PathSeparator) =>
27+
item.RelativePath.Replace(OneNoteApplication.RelativePathSeparator.ToString(), separator);
2728

2829
private string GetTitle(IOneNoteItem item, List<int> highlightData)
2930
{
@@ -43,37 +44,6 @@ private string GetTitle(IOneNoteItem item, List<int> highlightData)
4344
return title;
4445

4546
}
46-
//TODO replace with humanizer
47-
private static string GetLastEdited(TimeSpan diff)
48-
{
49-
string lastEdited = "Last edited ";
50-
if (PluralCheck(diff.TotalDays, "day", ref lastEdited)
51-
|| PluralCheck(diff.TotalHours, "hour", ref lastEdited)
52-
|| PluralCheck(diff.TotalMinutes, "min", ref lastEdited)
53-
|| PluralCheck(diff.TotalSeconds, "sec", ref lastEdited))
54-
{
55-
return lastEdited;
56-
}
57-
else
58-
{
59-
return lastEdited += "Now.";
60-
}
61-
62-
static bool PluralCheck(double totalTime, string timeType, ref string lastEdited)
63-
{
64-
var roundedTime = (int)Math.Round(totalTime);
65-
if (roundedTime > 0)
66-
{
67-
string plural = roundedTime == 1 ? string.Empty : "s";
68-
lastEdited += $"{roundedTime} {timeType}{plural} ago.";
69-
return true;
70-
}
71-
else
72-
{
73-
return false;
74-
}
75-
}
76-
}
7747

7848
private string GetAutoCompleteText(IOneNoteItem item)
7949
=> $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{Keywords.NotebookExplorerSeparator}";
@@ -246,7 +216,7 @@ public Result CreatePageResult(OneNotePage page, string query)
246216
public Result CreateRecentPageResult(OneNotePage page)
247217
{
248218
var result = CreateOneNoteItemResult(page, false, null);
249-
result.SubTitle = $"{GetLastEdited(DateTime.Now - page.LastModified)}\t{result.SubTitle}";
219+
result.SubTitle = $"{page.LastModified.Humanize()}\t{result.SubTitle}";
250220
result.IcoPath = iconProvider.Recent;
251221
return result;
252222
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Reflection;
22
using System.Linq;
3-
using System.Text.RegularExpressions;
3+
using Humanizer;
44

55
namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
66
{
7-
public partial class KeywordViewModel : BaseModel
7+
public class KeywordViewModel : BaseModel
88
{
99
private object Instance { get; init; }
1010
private PropertyInfo PropertyInfo { get; init; }
@@ -19,8 +19,7 @@ public string Keyword
1919
OnPropertyChanged();
2020
}
2121
}
22-
23-
22+
2423
public static KeywordViewModel[] GetKeywordViewModels(Keywords keywords)
2524
{
2625
return keywords.GetType()
@@ -29,12 +28,9 @@ public static KeywordViewModel[] GetKeywordViewModels(Keywords keywords)
2928
{
3029
Instance = keywords,
3130
PropertyInfo = p,
32-
Name = NicfyPropertyName().Replace(p.Name, " $1"),
31+
Name = p.Name.Humanize(LetterCasing.Title)
3332
})
3433
.ToArray();
3534
}
36-
37-
[GeneratedRegex("(\\B[A-Z])")]
38-
private static partial Regex NicfyPropertyName();
3935
}
4036
}

Flow.Launcher.Plugin.OneNote/UI/ViewModels/SettingsViewModel.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using System.Windows.Input;
44
using Flow.Launcher.Plugin.OneNote.Icons;
5+
using Humanizer;
56
using Modern = ModernWpf.Controls;
67

78
namespace Flow.Launcher.Plugin.OneNote.UI.ViewModels
@@ -45,7 +46,11 @@ public SettingsViewModel(PluginInitContext context, Settings settings, IconProvi
4546
public Settings Settings { get; }
4647
public KeywordViewModel[] Keywords { get; }
4748
public IconThemeViewModel[] IconThemes { get; }
48-
public string CachedIconsFileSize => iconProvider.GetCachedIconsMemorySize();
49+
public string CachedIconsFileSize => iconProvider.GeneratedImagesDirectoryInfo.EnumerateFiles()
50+
.Select(file => file.Length)
51+
.Aggregate(0L, (a, b) => a + b)
52+
.Bytes()
53+
.Humanize();
4954

5055
public KeywordViewModel SelectedKeyword
5156
{
@@ -60,7 +65,7 @@ private async Task ClearCachedIcons()
6065
{
6166
Title = "Clear Cached Icons",
6267
Content = $"Delete cached notebook and sections icons.\n" +
63-
$"This will delete {iconProvider.CachedIconCount} icon{(iconProvider.CachedIconCount != 1 ? "s" : string.Empty)}.",
68+
$"This will delete {"icon".ToQuantity(iconProvider.CachedIconCount)}.",
6469
PrimaryButtonText = "Yes",
6570
CloseButtonText = "Cancel",
6671
DefaultButton = Modern.ContentDialogButton.Close,

0 commit comments

Comments
 (0)