From 246cde1fe533d58a4871f316af177cbf5050dc0d Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 1 Feb 2018 20:04:24 +0300 Subject: [PATCH] Few %rf related changes --- ShareX.HelpersLib/Helpers/Helpers.cs | 14 +++------ .../NameParser/CodeMenuEntryFilename.cs | 1 - ShareX.HelpersLib/NameParser/NameParser.cs | 31 +++++++++++++++++-- .../Properties/Resources.fr.resx | 2 +- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index 8bb37443d..964929dfe 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -269,7 +269,7 @@ public static string GetAllCharacters() public static string GetRandomLine(string text) { - string[] lines = text.Lines(); + string[] lines = text.Trim().Lines(); if (lines != null && lines.Length > 0) { return lines[MathHelpers.Random(0, lines.Length - 1)]; @@ -277,16 +277,10 @@ public static string GetRandomLine(string text) return null; } - public static string GetRandomLineFromFile(string path, bool isPreviewMode = false) + public static string GetRandomLineFromFile(string path) { - try - { - return GetRandomLine(File.ReadAllText(path)); - } - catch (Exception e) when (isPreviewMode) - { - return e.Message; - } + string text = File.ReadAllText(path); + return GetRandomLine(text); } public static string GetValidFileName(string fileName, string separator = "") diff --git a/ShareX.HelpersLib/NameParser/CodeMenuEntryFilename.cs b/ShareX.HelpersLib/NameParser/CodeMenuEntryFilename.cs index f7ef27b96..eae8cec33 100644 --- a/ShareX.HelpersLib/NameParser/CodeMenuEntryFilename.cs +++ b/ShareX.HelpersLib/NameParser/CodeMenuEntryFilename.cs @@ -60,7 +60,6 @@ public class CodeMenuEntryFilename : CodeMenuEntry public static readonly CodeMenuEntryFilename radjective = new CodeMenuEntryFilename("radjective", "Random adjective", Resources.ReplCodeMenuCategory_Random); public static readonly CodeMenuEntryFilename ranimal = new CodeMenuEntryFilename("ranimal", "Random animal", Resources.ReplCodeMenuCategory_Random); public static readonly CodeMenuEntryFilename rf = new CodeMenuEntryFilename("rf", Resources.ReplCodeMenuEntry_rf_Random_line_from_file, Resources.ReplCodeMenuCategory_Random); - public static readonly CodeMenuEntryFilename width = new CodeMenuEntryFilename("width", Resources.ReplCodeMenuEntry_width_Gets_image_width, Resources.ReplCodeMenuCategory_Image); public static readonly CodeMenuEntryFilename height = new CodeMenuEntryFilename("height", Resources.ReplCodeMenuEntry_height_Gets_image_height, Resources.ReplCodeMenuCategory_Image); public static readonly CodeMenuEntryFilename un = new CodeMenuEntryFilename("un", Resources.ReplCodeMenuEntry_un_User_name, Resources.ReplCodeMenuCategory_Computer); diff --git a/ShareX.HelpersLib/NameParser/NameParser.cs b/ShareX.HelpersLib/NameParser/NameParser.cs index 61c4ce22b..95959903e 100644 --- a/ShareX.HelpersLib/NameParser/NameParser.cs +++ b/ShareX.HelpersLib/NameParser/NameParser.cs @@ -240,9 +240,28 @@ public string Parse(string pattern) result = result.ReplaceAll(CodeMenuEntryFilename.ranimal.ToPrefixString(), () => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Helpers.GetRandomLine(Resources.animals))); - foreach (Tuple entry in ListEntryWithArguments(result, CodeMenuEntryFilename.rf.ToPrefixString(), 1)) + foreach (Tuple entry in ListEntryWithArgument(result, CodeMenuEntryFilename.rf.ToPrefixString())) { - result = result.ReplaceAll(entry.Item1, () => Helpers.GetRandomLineFromFile(entry.Item2[0], IsPreviewMode)); + result = result.ReplaceAll(entry.Item1, () => + { + try + { + string path = entry.Item2; + + if (Helpers.IsTextFile(path)) + { + return Helpers.GetRandomLineFromFile(path); + } + else + { + throw new Exception("Valid text file path is required."); + } + } + catch (Exception e) when (IsPreviewMode) + { + return e.Message; + } + }); } foreach (Tuple entry in ListEntryWithValue(result, CodeMenuEntryFilename.rn.ToPrefixString())) @@ -310,6 +329,14 @@ private IEnumerable> ListEntryWithArguments(string text, } } + private IEnumerable> ListEntryWithArgument(string text, string entry) + { + foreach (Tuple o in ListEntryWithArguments(text, entry, 1)) + { + yield return new Tuple(o.Item1, o.Item2[0]); + } + } + private IEnumerable> ListEntryWithValues(string text, string entry, int elements) { foreach (Tuple o in ListEntryWithArguments(text, entry, elements)) diff --git a/ShareX.HelpersLib/Properties/Resources.fr.resx b/ShareX.HelpersLib/Properties/Resources.fr.resx index ee813acb4..d6312198a 100644 --- a/ShareX.HelpersLib/Properties/Resources.fr.resx +++ b/ShareX.HelpersLib/Properties/Resources.fr.resx @@ -1025,6 +1025,6 @@ Voulez-vous la télécharger ? Diagonale en avant - Line aléatoire d'un fichier. Utilisez {chemin} pour indiquer le fichier + Ligne aléatoire d'un fichier. Utilisez {chemin} pour indiquer le fichier \ No newline at end of file