Merge pull request #3113 from AtlasTheBot/master

Add %rf to use a random line from a file
This commit is contained in:
Jaex 2018-02-01 19:22:08 +03:00 committed by GitHub
commit 1493f0a017
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 1 deletions

View file

@ -277,6 +277,18 @@ public static string GetRandomLine(string text)
return null;
}
public static string GetRandomLineFromFile(string path, bool isPreviewMode = false)
{
try
{
return GetRandomLine(File.ReadAllText(path));
}
catch (Exception e) when (isPreviewMode)
{
return e.Message;
}
}
public static string GetValidFileName(string fileName, string separator = "")
{
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();

View file

@ -59,6 +59,8 @@ public class CodeMenuEntryFilename : CodeMenuEntry
// TODO: Translate
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);

View file

@ -53,6 +53,11 @@ public class NameParser
public string ProcessName { get; set; } // %pn
public TimeZoneInfo CustomTimeZone { get; set; }
// If we're trying to preview via TaskSettings or not
// Used so that %rf throws "File not found" exceptions and brings up a popup on upload
// But only returns an error message when previewing to avoid popup spam
public bool IsPreviewMode { get; set; } = false;
protected NameParser()
{
}
@ -235,6 +240,11 @@ public string Parse(string pattern)
result = result.ReplaceAll(CodeMenuEntryFilename.ranimal.ToPrefixString(),
() => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Helpers.GetRandomLine(Resources.animals)));
foreach (Tuple<string, string[]> entry in ListEntryWithArguments(result, CodeMenuEntryFilename.rf.ToPrefixString(), 1))
{
result = result.ReplaceAll(entry.Item1, () => Helpers.GetRandomLineFromFile(entry.Item2[0], IsPreviewMode));
}
foreach (Tuple<string, int> entry in ListEntryWithValue(result, CodeMenuEntryFilename.rn.ToPrefixString()))
{
result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Numbers).ToString()));

View file

@ -2644,6 +2644,15 @@ internal static string ReplCodeMenuEntry_ra_Random_alphanumeric_char {
}
}
/// <summary>
/// Looks up a localized string similar to Random line from a file. Use {path} to determine the file.
/// </summary>
internal static string ReplCodeMenuEntry_rf_Random_line_from_file {
get {
return ResourceManager.GetString("ReplCodeMenuEntry_rf_Random_line_from_file", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Random number 0 to 9. Repeat using {n}.
/// </summary>

View file

@ -579,4 +579,7 @@ Tamaño de archivo: {2:n0} / {3:n0} KB</value>
<data name="ReplCodeMenuEntry_uln_User_login_name" xml:space="preserve">
<value>Nombre de inicio de sesión de usuario</value>
</data>
<data name="ReplCodeMenuEntry_rf_Random_line_from_file" xml:space="preserve">
<value>Línea aleatorio de un archivo. Usa {ruta} para indicar el archivo</value>
</data>
</root>

View file

@ -1024,4 +1024,7 @@ Voulez-vous la télécharger ?</value>
<data name="LinearGradientMode_ForwardDiagonal" xml:space="preserve">
<value>Diagonale en avant</value>
</data>
<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>
</data>
</root>

View file

@ -1145,4 +1145,7 @@ Would you like to download it?</value>
<data name="animals" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\animals.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="ReplCodeMenuEntry_rf_Random_line_from_file" xml:space="preserve">
<value>Random line from a file. Use {path} to determine the file</value>
</data>
</root>

View file

@ -1142,7 +1142,8 @@ private void UpdateNameFormatPreviews()
ImageHeight = 1080,
MaxNameLength = TaskSettings.AdvancedSettings.NamePatternMaxLength,
MaxTitleLength = TaskSettings.AdvancedSettings.NamePatternMaxTitleLength,
CustomTimeZone = TaskSettings.UploadSettings.UseCustomTimeZone ? TaskSettings.UploadSettings.CustomTimeZone : null
CustomTimeZone = TaskSettings.UploadSettings.UseCustomTimeZone ? TaskSettings.UploadSettings.CustomTimeZone : null,
IsPreviewMode = true
};
lblNameFormatPatternPreview.Text = Resources.TaskSettingsForm_txtNameFormatPatternActiveWindow_TextChanged_Preview_ + " " +