mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-03 02:36:50 +13:00
Merge pull request #931 from davidruhmann/nameparser-parameter-support
nameparser parameter support
This commit is contained in:
commit
77df6e7c95
5 changed files with 57 additions and 6 deletions
|
@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
|
|||
#endregion License Information (GPL v3)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
|
@ -266,5 +267,19 @@ public static string[] Lines(this string text)
|
|||
{
|
||||
return text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
|
||||
}
|
||||
|
||||
public static IEnumerable<Tuple<string, string>> ForEachBetween(this string text, string front, string back)
|
||||
{
|
||||
int f = 0;
|
||||
int b = 0;
|
||||
while (text.Length > f
|
||||
&& 0 < (f = text.IndexOf(front, f))
|
||||
&& 0 < (b = text.IndexOf(back, f + front.Length)))
|
||||
{
|
||||
string result = text.Substring(f, (b + back.Length) - f);
|
||||
yield return new Tuple<string, string>(result, result.Substring(front.Length, (result.Length - back.Length) - front.Length));
|
||||
f += front.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -881,5 +881,15 @@ public static bool IsAdministrator()
|
|||
{
|
||||
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
|
||||
}
|
||||
|
||||
public static string RepeatGenerator(int count, Func<string> generator)
|
||||
{
|
||||
string result = "";
|
||||
for (int x = count; x > 0; x--)
|
||||
{
|
||||
result += generator();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License
|
|||
|
||||
using ShareX.HelpersLib.Properties;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
@ -177,6 +178,10 @@ public string Parse(string pattern)
|
|||
if (sb.ToString().Contains(ReplCodeMenuEntry.i.ToPrefixString()))
|
||||
{
|
||||
AutoIncrementNumber++;
|
||||
foreach (Tuple<string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.i.ToPrefixString()))
|
||||
{
|
||||
sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber, Math.Max(0, entry.Item2)));
|
||||
}
|
||||
sb.Replace(ReplCodeMenuEntry.i.ToPrefixString(), AutoIncrementNumber.ToString());
|
||||
}
|
||||
|
||||
|
@ -191,6 +196,15 @@ public string Parse(string pattern)
|
|||
|
||||
string result = sb.ToString();
|
||||
|
||||
foreach (Tuple<string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.rn.ToPrefixString()))
|
||||
{
|
||||
result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Numbers).ToString()));
|
||||
}
|
||||
foreach (Tuple<string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.ra.ToPrefixString()))
|
||||
{
|
||||
result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString()));
|
||||
}
|
||||
|
||||
result = result.ReplaceAll(ReplCodeMenuEntry.rn.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Numbers).ToString());
|
||||
result = result.ReplaceAll(ReplCodeMenuEntry.ra.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString());
|
||||
|
||||
|
@ -218,5 +232,17 @@ public string Parse(string pattern)
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
private IEnumerable<Tuple<string, int>> ListEntryWithValue(string text, string entry)
|
||||
{
|
||||
foreach (Tuple<string, string> s in text.ForEachBetween(entry + "{", "}"))
|
||||
{
|
||||
int n = 0;
|
||||
if (int.TryParse(s.Item2, out n))
|
||||
{
|
||||
yield return new Tuple<string, int>(s.Item1, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1673,7 +1673,7 @@ internal static string ReplCodeMenuEntry_height_Gets_image_height {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Auto increment number.
|
||||
/// Looks up a localized string similar to Auto increment number. 0 pad left using {n}.
|
||||
/// </summary>
|
||||
internal static string ReplCodeMenuEntry_i_Auto_increment_number {
|
||||
get {
|
||||
|
@ -1754,7 +1754,7 @@ internal static string ReplCodeMenuEntry_pn_Process_name_of_active_window {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Random alphanumeric char.
|
||||
/// Looks up a localized string similar to Random alphanumeric char. Repeat using {n}.
|
||||
/// </summary>
|
||||
internal static string ReplCodeMenuEntry_ra_Random_alphanumeric_char {
|
||||
get {
|
||||
|
@ -1763,7 +1763,7 @@ internal static string ReplCodeMenuEntry_ra_Random_alphanumeric_char {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Random number 0 to 9.
|
||||
/// Looks up a localized string similar to Random number 0 to 9. Repeat using {n}.
|
||||
/// </summary>
|
||||
internal static string ReplCodeMenuEntry_rn_Random_number_0_to_9 {
|
||||
get {
|
||||
|
|
|
@ -197,13 +197,13 @@
|
|||
<value>Unix timestamp</value>
|
||||
</data>
|
||||
<data name="ReplCodeMenuEntry_i_Auto_increment_number" xml:space="preserve">
|
||||
<value>Auto increment number</value>
|
||||
<value>Auto increment number. 0 pad left using {n}</value>
|
||||
</data>
|
||||
<data name="ReplCodeMenuEntry_rn_Random_number_0_to_9" xml:space="preserve">
|
||||
<value>Random number 0 to 9</value>
|
||||
<value>Random number 0 to 9. Repeat using {n}</value>
|
||||
</data>
|
||||
<data name="ReplCodeMenuEntry_ra_Random_alphanumeric_char" xml:space="preserve">
|
||||
<value>Random alphanumeric char</value>
|
||||
<value>Random alphanumeric char. Repeat using {n}</value>
|
||||
</data>
|
||||
<data name="ReplCodeMenuEntry_width_Gets_image_width" xml:space="preserve">
|
||||
<value>Image width</value>
|
||||
|
|
Loading…
Reference in a new issue