Few %rf related changes

This commit is contained in:
Jaex 2018-02-01 20:04:24 +03:00
parent 1493f0a017
commit 246cde1fe5
4 changed files with 34 additions and 14 deletions

View file

@ -269,7 +269,7 @@ public static string GetAllCharacters()
public static string GetRandomLine(string text) public static string GetRandomLine(string text)
{ {
string[] lines = text.Lines(); string[] lines = text.Trim().Lines();
if (lines != null && lines.Length > 0) if (lines != null && lines.Length > 0)
{ {
return lines[MathHelpers.Random(0, lines.Length - 1)]; return lines[MathHelpers.Random(0, lines.Length - 1)];
@ -277,16 +277,10 @@ public static string GetRandomLine(string text)
return null; return null;
} }
public static string GetRandomLineFromFile(string path, bool isPreviewMode = false) public static string GetRandomLineFromFile(string path)
{ {
try string text = File.ReadAllText(path);
{ return GetRandomLine(text);
return GetRandomLine(File.ReadAllText(path));
}
catch (Exception e) when (isPreviewMode)
{
return e.Message;
}
} }
public static string GetValidFileName(string fileName, string separator = "") public static string GetValidFileName(string fileName, string separator = "")

View file

@ -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 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 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 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 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 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); public static readonly CodeMenuEntryFilename un = new CodeMenuEntryFilename("un", Resources.ReplCodeMenuEntry_un_User_name, Resources.ReplCodeMenuCategory_Computer);

View file

@ -240,9 +240,28 @@ public string Parse(string pattern)
result = result.ReplaceAll(CodeMenuEntryFilename.ranimal.ToPrefixString(), result = result.ReplaceAll(CodeMenuEntryFilename.ranimal.ToPrefixString(),
() => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Helpers.GetRandomLine(Resources.animals))); () => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Helpers.GetRandomLine(Resources.animals)));
foreach (Tuple<string, string[]> entry in ListEntryWithArguments(result, CodeMenuEntryFilename.rf.ToPrefixString(), 1)) foreach (Tuple<string, string> 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<string, int> entry in ListEntryWithValue(result, CodeMenuEntryFilename.rn.ToPrefixString())) foreach (Tuple<string, int> entry in ListEntryWithValue(result, CodeMenuEntryFilename.rn.ToPrefixString()))
@ -310,6 +329,14 @@ private IEnumerable<Tuple<string, string[]>> ListEntryWithArguments(string text,
} }
} }
private IEnumerable<Tuple<string, string>> ListEntryWithArgument(string text, string entry)
{
foreach (Tuple<string, string[]> o in ListEntryWithArguments(text, entry, 1))
{
yield return new Tuple<string, string>(o.Item1, o.Item2[0]);
}
}
private IEnumerable<Tuple<string, int[]>> ListEntryWithValues(string text, string entry, int elements) private IEnumerable<Tuple<string, int[]>> ListEntryWithValues(string text, string entry, int elements)
{ {
foreach (Tuple<string, string[]> o in ListEntryWithArguments(text, entry, elements)) foreach (Tuple<string, string[]> o in ListEntryWithArguments(text, entry, elements))

View file

@ -1025,6 +1025,6 @@ Voulez-vous la télécharger ?</value>
<value>Diagonale en avant</value> <value>Diagonale en avant</value>
</data> </data>
<data name="ReplCodeMenuEntry_rf_Random_line_from_file" xml:space="preserve"> <data name="ReplCodeMenuEntry_rf_Random_line_from_file" xml:space="preserve">
<value>Line aléatoire d'un fichier. Utilisez {chemin} pour indiquer le fichier</value> <value>Ligne aléatoire d'un fichier. Utilisez {chemin} pour indiquer le fichier</value>
</data> </data>
</root> </root>