From 6b1dc6724f77063a68c1324ca022b3ce8c8173d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Thu, 20 Jan 2022 13:36:25 +0100 Subject: [PATCH] Refactor --- .../FileTime.App.Core/Command/Commands.cs | 9 +++- .../FileTime.ConsoleUI.App/Application.cs | 6 +-- src/Core/FileTime.Core/Components/Tab.cs | 50 +++++++++++++------ .../Extensions/FormatExtensions.cs | 5 +- .../FileTime.Providers.Local/LocalFile.cs | 2 + 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/AppCommon/FileTime.App.Core/Command/Commands.cs b/src/AppCommon/FileTime.App.Core/Command/Commands.cs index 95bc09d..5ccb72f 100644 --- a/src/AppCommon/FileTime.App.Core/Command/Commands.cs +++ b/src/AppCommon/FileTime.App.Core/Command/Commands.cs @@ -16,7 +16,12 @@ namespace FileTime.App.Core.Command CreateElement, MoveCursorUpPage, MoveCursorDownPage, - GoToTop, - GoToBottom, + MoveToTop, + MoveToBottom, + MoveToFirst, + MoveToLast, + GoToRoot, + GoToProvider, + GoToHome, } } \ No newline at end of file diff --git a/src/ConsoleApp/FileTime.ConsoleUI.App/Application.cs b/src/ConsoleApp/FileTime.ConsoleUI.App/Application.cs index e514b78..fceed1e 100644 --- a/src/ConsoleApp/FileTime.ConsoleUI.App/Application.cs +++ b/src/ConsoleApp/FileTime.ConsoleUI.App/Application.cs @@ -88,7 +88,7 @@ namespace FileTime.ConsoleUI.App new CommandBinding("open", Commands.Open, new[] { new ConsoleKeyInfo('→', ConsoleKey.RightArrow, false, false, false) }, Open), new CommandBinding( "go to top", - Commands.GoToTop, + Commands.MoveToTop, new[] { new ConsoleKeyInfo('g', ConsoleKey.G, false, false, false), @@ -97,7 +97,7 @@ namespace FileTime.ConsoleUI.App MoveCursorToTop), new CommandBinding( "go to bottom", - Commands.GoToBottom, + Commands.MoveToBottom, new[] { new ConsoleKeyInfo('G', ConsoleKey.G, true, false, false) @@ -194,7 +194,7 @@ namespace FileTime.ConsoleUI.App var key = keyinfo.Key; _previousKeys.Add(keyinfo); - CommandBinding? selectedCommandBinding = _commandBindings.Find(c => AreKeysEqual(c.Keys, _previousKeys)); + var selectedCommandBinding = _commandBindings.Find(c => AreKeysEqual(c.Keys, _previousKeys)); if (keyinfo.Key == ConsoleKey.Escape) { diff --git a/src/Core/FileTime.Core/Components/Tab.cs b/src/Core/FileTime.Core/Components/Tab.cs index 929433e..45c1678 100644 --- a/src/Core/FileTime.Core/Components/Tab.cs +++ b/src/Core/FileTime.Core/Components/Tab.cs @@ -4,43 +4,45 @@ namespace FileTime.Core.Components { public class Tab { - private IItem? currentSelectedItem; - private IContainer currentLocation; + private IItem? _currentSelectedItem; + private IContainer _currentLocation; public IContainer CurrentLocation { - get => currentLocation; + get => _currentLocation; private set { - if (currentLocation != value) + if (_currentLocation != value) { - if (currentLocation != null) + if (_currentLocation != null) { - currentLocation.Refreshed -= HandleCurrentLocationRefresh; + _currentLocation.Refreshed -= HandleCurrentLocationRefresh; } - currentLocation = value; + _currentLocation = value; CurrentLocationChanged?.Invoke(this, EventArgs.Empty); CurrentSelectedItem = CurrentLocation.Items.Count > 0 ? CurrentLocation.Items[0] : null; - currentLocation.Refreshed += HandleCurrentLocationRefresh; + _currentLocation.Refreshed += HandleCurrentLocationRefresh; } } } public IItem? CurrentSelectedItem { - get => currentSelectedItem; - private set + get => _currentSelectedItem; + set { - if (currentSelectedItem != value && (currentLocation.Items.Contains(value) || value == null)) + if (_currentSelectedItem != value && (_currentLocation.Items.Contains(value) || value == null)) { - currentSelectedItem = value; + _currentSelectedItem = value; CurrentSelectedIndex = GetItemIndex(value); + CurrentSelectedItemChanged?.Invoke(this, EventArgs.Empty); } } } public int CurrentSelectedIndex { get; private set; } public event EventHandler CurrentLocationChanged; + public event EventHandler CurrentSelectedItemChanged; public Tab(IContainer currentPath) { @@ -53,7 +55,7 @@ namespace FileTime.Core.Components var currentSelectedName = CurrentSelectedItem?.FullName; if (currentSelectedName != null) { - CurrentSelectedItem = CurrentLocation.Items.FirstOrDefault(i => i.FullName == currentSelectedName) ?? currentLocation.Items.FirstOrDefault(); + CurrentSelectedItem = CurrentLocation.Items.FirstOrDefault(i => i.FullName == currentSelectedName) ?? _currentLocation.Items.FirstOrDefault(); } else if (CurrentLocation.Items.Count > 0) { @@ -120,6 +122,26 @@ namespace FileTime.Core.Components } } + public void GoToProvider() + { + if (CurrentLocation == null) return; + + CurrentLocation = CurrentLocation.Provider; + } + + public void GoToRoot() + { + if (CurrentLocation == null) return; + + var root = CurrentLocation; + while (root!.GetParent() != null) + { + root = root.GetParent(); + } + + CurrentLocation = root; + } + public void GoUp() { var lastCurrentLocation = CurrentLocation; @@ -142,7 +164,7 @@ namespace FileTime.Core.Components public void Open() { - if (currentSelectedItem is IContainer childContainer) + if (_currentSelectedItem is IContainer childContainer) { if (CurrentLocation is VirtualContainer currentVirtuakContainer) { diff --git a/src/Providers/FileTime.Providers.Local/Extensions/FormatExtensions.cs b/src/Providers/FileTime.Providers.Local/Extensions/FormatExtensions.cs index 5bba837..b688884 100644 --- a/src/Providers/FileTime.Providers.Local/Extensions/FormatExtensions.cs +++ b/src/Providers/FileTime.Providers.Local/Extensions/FormatExtensions.cs @@ -19,7 +19,10 @@ namespace FileTime.Providers.Local.Extensions _ => (fileSizeD, "B") }; - return string.Format("{0:N" + precision + "}", size).TrimEnd('0').Replace(',', '.').TrimEnd('.') + " " + suffix; + var result = string.Format("{0:N" + precision + "}", size).Replace(',', '.'); + + if (result.Contains('.')) result = result.TrimEnd('0').TrimEnd('.'); + return result + " " + suffix; } } } \ No newline at end of file diff --git a/src/Providers/FileTime.Providers.Local/LocalFile.cs b/src/Providers/FileTime.Providers.Local/LocalFile.cs index eeb6f6b..54303a7 100644 --- a/src/Providers/FileTime.Providers.Local/LocalFile.cs +++ b/src/Providers/FileTime.Providers.Local/LocalFile.cs @@ -10,6 +10,8 @@ namespace FileTime.Providers.Local { private readonly FileInfo _file; + public FileInfo File => _file; + public string Name { get; } public string FullName { get; }