From 52ddcdddc33d7ee658261a339e5be6b101c82115 Mon Sep 17 00:00:00 2001 From: adroslice Date: Thu, 21 Nov 2019 19:46:33 +0100 Subject: [PATCH] Added Number-Operation + UI --- BFR/MainWindow.xaml | 30 +++++++++++++++ BFR/MainWindowUIProperties.cs | 3 +- BFR/Operations/Number.cs | 69 +++++++++++++++++++++++++++++++++++ BFR/Operations/NumberMode.cs | 40 ++++++++++++++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 BFR/Operations/Number.cs create mode 100644 BFR/Operations/NumberMode.cs diff --git a/BFR/MainWindow.xaml b/BFR/MainWindow.xaml index b5e5193..cd35980 100644 --- a/BFR/MainWindow.xaml +++ b/BFR/MainWindow.xaml @@ -230,5 +230,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BFR/MainWindowUIProperties.cs b/BFR/MainWindowUIProperties.cs index 4dd69f3..7ae2a2a 100644 --- a/BFR/MainWindowUIProperties.cs +++ b/BFR/MainWindowUIProperties.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using Avalonia; -using Avalonia.Collections; using Avalonia.Controls; +using Avalonia.Collections; using BFR.DataModels; using BFR.Operations; @@ -26,6 +26,7 @@ namespace BFR OperationType.Make(), OperationType.Make(), OperationType.Make(), + OperationType.Make(), }; public static readonly AvaloniaProperty IsCommitButtonEnabledProperty = diff --git a/BFR/Operations/Number.cs b/BFR/Operations/Number.cs new file mode 100644 index 0000000..ca477f7 --- /dev/null +++ b/BFR/Operations/Number.cs @@ -0,0 +1,69 @@ +using System.Xml.Serialization; +using System.Collections.Generic; + +using Avalonia; + +using BFR.Helpers; +using BFR.DataModels; + +namespace BFR.Operations +{ + public class Number : Operation + { + public override string Name => nameof(Number); + public override string Description => "Numbers all file names according to how they are sorted."; + + public static readonly AvaloniaProperty ModeProperty = + AvaloniaProperty.Register(nameof(Mode), NumberMode.Prefix); + public NumberMode Mode { get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); } + + [XmlIgnore] + public NumberOperationMode[] Modes => NumberOperationMode.Modes; + + public int InsertPosition { get; set; } = 0; + public int Increment { get; set; } = 1; + public int StartIndex { get; set; } = 1; + public int Padding { get; set; } = 0; + public bool AutoPadding { get; set; } = false; + public bool FullName { get; set; } = false; + public bool UseRegex { get; set; } = false; + public string Pattern { get; set; } = ""; + + protected override void ApplyToInternal(IList files) + { + var index = StartIndex; + var padding = AutoPadding + ? (StartIndex + Increment * (files.Count - 1)).ToString().Length + : Padding; + + foreach (var file in files) + { + var newName = FullName ? file.FullName : file.Name; + var text = PadInteger(index, padding); + + newName = Mode switch + { + NumberMode.Prefix => $"{text}{newName}", + NumberMode.Suffix => $"{newName}{text}", + NumberMode.Insert => newName.Insert(InsertPosition, text), + NumberMode.Replace => newName.Replace(Pattern, text, UseRegex, false), + _ => newName + }; + + if (FullName) file.FullName = newName; + else file.Name = newName; + + index += Increment; + } + } + + private static string PadInteger(int value, int padding) + { + var valueString = value.ToString(); + while (valueString.Length < padding) + valueString = $"0{valueString}"; + + return valueString; + } + } +} diff --git a/BFR/Operations/NumberMode.cs b/BFR/Operations/NumberMode.cs new file mode 100644 index 0000000..be20beb --- /dev/null +++ b/BFR/Operations/NumberMode.cs @@ -0,0 +1,40 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +using BFR.Helpers; + +namespace BFR.Operations +{ + public class NumberOperationMode : OperationMode + { + public bool IsPrefix => Index == NumberMode.Prefix; + public bool IsSuffix => Index == NumberMode.Suffix; + public bool IsInsert => Index == NumberMode.Insert; + public bool IsReplace => Index == NumberMode.Replace; + + public readonly static NumberOperationMode[] Modes = All(); + + public NumberOperationMode(NumberMode index, string name, string description) : + base(index, name, description) { } + + protected static NumberOperationMode[] All() => ((IEnumerable)Enum.GetValues(typeof(NumberMode))).Select(x => + new NumberOperationMode( + x, + x.GetAttribute().DisplayName, + x.GetAttribute().Description + )).ToArray(); + } + + public enum NumberMode + { + [OperationMode(nameof(Prefix), "Inserts the numbering before the file name.")] + Prefix, + [OperationMode(nameof(Suffix), "Inserts the numbering after the file name.")] + Suffix, + [OperationMode(nameof(Insert), "Inserts the numbering at the specified index.")] + Insert, + [OperationMode(nameof(Replace), "Replaces the given pattern with the numbering.")] + Replace, + } +}