mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-04 22:21:23 +12:00
add nameparser parameter support
parameter = {n} for entries i, ra, rn.
This commit is contained in:
parent
49bb9f7e80
commit
59abcded74
|
@ -24,6 +24,7 @@
|
||||||
#endregion License Information (GPL v3)
|
#endregion License Information (GPL v3)
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace ShareX.HelpersLib
|
namespace ShareX.HelpersLib
|
||||||
|
@ -266,5 +267,19 @@ public static string[] Lines(this string text)
|
||||||
{
|
{
|
||||||
return text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
|
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);
|
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 @@
|
||||||
|
|
||||||
using ShareX.HelpersLib.Properties;
|
using ShareX.HelpersLib.Properties;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
@ -177,6 +178,10 @@ public string Parse(string pattern)
|
||||||
if (sb.ToString().Contains(ReplCodeMenuEntry.i.ToPrefixString()))
|
if (sb.ToString().Contains(ReplCodeMenuEntry.i.ToPrefixString()))
|
||||||
{
|
{
|
||||||
AutoIncrementNumber++;
|
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());
|
sb.Replace(ReplCodeMenuEntry.i.ToPrefixString(), AutoIncrementNumber.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,6 +196,15 @@ public string Parse(string pattern)
|
||||||
|
|
||||||
string result = sb.ToString();
|
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.rn.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Numbers).ToString());
|
||||||
result = result.ReplaceAll(ReplCodeMenuEntry.ra.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString());
|
result = result.ReplaceAll(ReplCodeMenuEntry.ra.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString());
|
||||||
|
|
||||||
|
@ -218,5 +232,17 @@ public string Parse(string pattern)
|
||||||
|
|
||||||
return result;
|
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 class Resources {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
internal static string ReplCodeMenuEntry_i_Auto_increment_number {
|
internal static string ReplCodeMenuEntry_i_Auto_increment_number {
|
||||||
get {
|
get {
|
||||||
|
@ -1754,7 +1754,7 @@ internal class Resources {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
internal static string ReplCodeMenuEntry_ra_Random_alphanumeric_char {
|
internal static string ReplCodeMenuEntry_ra_Random_alphanumeric_char {
|
||||||
get {
|
get {
|
||||||
|
@ -1763,7 +1763,7 @@ internal class Resources {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
internal static string ReplCodeMenuEntry_rn_Random_number_0_to_9 {
|
internal static string ReplCodeMenuEntry_rn_Random_number_0_to_9 {
|
||||||
get {
|
get {
|
||||||
|
|
|
@ -197,13 +197,13 @@
|
||||||
<value>Unix timestamp</value>
|
<value>Unix timestamp</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="ReplCodeMenuEntry_i_Auto_increment_number" xml:space="preserve">
|
<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>
|
||||||
<data name="ReplCodeMenuEntry_rn_Random_number_0_to_9" xml:space="preserve">
|
<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>
|
||||||
<data name="ReplCodeMenuEntry_ra_Random_alphanumeric_char" xml:space="preserve">
|
<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>
|
||||||
<data name="ReplCodeMenuEntry_width_Gets_image_width" xml:space="preserve">
|
<data name="ReplCodeMenuEntry_width_Gets_image_width" xml:space="preserve">
|
||||||
<value>Image width</value>
|
<value>Image width</value>
|
||||||
|
|
Loading…
Reference in a new issue