From fb708065ff496c0d10de28c72df4c12cdea7c979 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 17 Apr 2026 18:22:16 +0800 Subject: [PATCH] feature: add `String Formatter` for `TextBox` in custom action (#2274) Signed-off-by: leo --- src/Models/CustomAction.cs | 7 +++++++ src/Resources/Locales/en_US.axaml | 2 ++ src/Resources/Locales/zh_CN.axaml | 2 ++ src/Resources/Locales/zh_TW.axaml | 2 ++ src/ViewModels/ExecuteCustomAction.cs | 13 ++++++++++--- src/Views/ConfigureCustomActionControls.axaml | 16 ++++++++++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Models/CustomAction.cs b/src/Models/CustomAction.cs index 8c12bc33..59652d32 100644 --- a/src/Models/CustomAction.cs +++ b/src/Models/CustomAction.cs @@ -51,6 +51,12 @@ namespace SourceGit.Models set => SetProperty(ref _stringValue, value); } + public string StringFormatter + { + get => _stringFormatter; + set => SetProperty(ref _stringFormatter, value); + } + public bool BoolValue { get => _boolValue; @@ -61,6 +67,7 @@ namespace SourceGit.Models private string _label = string.Empty; private string _description = string.Empty; private string _stringValue = string.Empty; + private string _stringFormatter = string.Empty; private bool _boolValue = false; } diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index f5b84018..efd003af 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -278,6 +278,8 @@ Label: Options: Use '|' as delimiter for options + String Formatter: + Optional. Used to format output string. Ignored when input is empty. Please use `${VALUE}` to represent the input string. The built-in variables ${REPO}, ${REMOTE}, ${BRANCH}, ${BRANCH_FRIENDLY_NAME}, ${SHA}, ${FILE}, and ${TAG} remain available here Type: Use Friendly Name: diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index c3c40041..f5d1a64e 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -282,6 +282,8 @@ 名称 : 选项列表 : 选项之间请使用英文 '|' 作为分隔符 + 输出内容格式化字串: + 可选。用于格式化输出结果。当用户输入为空时忽略该操作。请使用`${VALUE}`代替用户输入。 内置变量 ${REPO}, ${REMOTE}, ${BRANCH}, ${BRANCH_FRIENDLY_NAME}, ${SHA}, ${FILE} 与 ${TAG} 在这里仍然可用 类型 : 输出结果带有远程名: diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 9c8d33cd..f457cf03 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -282,6 +282,8 @@ 名稱: 選項列表: 請使用英文「|」符號分隔選項 + 字串格式化器: + 可選。用於格式化輸出字串。當輸入為空時將被忽略。請使用 `${VALUE}` 來表示輸入字串。 內建變數 ${REPO}、${REMOTE}、${BRANCH}、${BRANCH_FRIENDLY_NAME}、${SHA}、${FILE} 及 ${TAG} 在此處仍可使用 類型: 輸出包含遠端的名稱: diff --git a/src/ViewModels/ExecuteCustomAction.cs b/src/ViewModels/ExecuteCustomAction.cs index 0fd0fcfa..376386e5 100644 --- a/src/ViewModels/ExecuteCustomAction.cs +++ b/src/ViewModels/ExecuteCustomAction.cs @@ -18,15 +18,22 @@ namespace SourceGit.ViewModels public string Label { get; set; } public string Placeholder { get; set; } public string Text { get; set; } + public string Formatter { get; set; } - public CustomActionControlTextBox(string label, string placeholder, string defaultValue) + public CustomActionControlTextBox(string label, string placeholder, string defaultValue, string formatter) { Label = label + ":"; Placeholder = placeholder; Text = defaultValue; + Formatter = formatter; } - public string GetValue() => Text; + public string GetValue() + { + if (string.IsNullOrEmpty(Text)) + return string.Empty; + return string.IsNullOrEmpty(Formatter) ? Text : Formatter.Replace("${VALUE}", Text); + } } public class CustomActionControlPathSelector : ObservableObject, ICustomActionControlParameter @@ -187,7 +194,7 @@ namespace SourceGit.ViewModels switch (ctl.Type) { case Models.CustomActionControlType.TextBox: - ControlParameters.Add(new CustomActionControlTextBox(ctl.Label, ctl.Description, PrepareStringByTarget(ctl.StringValue))); + ControlParameters.Add(new CustomActionControlTextBox(ctl.Label, ctl.Description, PrepareStringByTarget(ctl.StringValue), ctl.StringFormatter)); break; case Models.CustomActionControlType.PathSelector: ControlParameters.Add(new CustomActionControlPathSelector(ctl.Label, ctl.Description, ctl.BoolValue, PrepareStringByTarget(ctl.StringValue))); diff --git a/src/Views/ConfigureCustomActionControls.axaml b/src/Views/ConfigureCustomActionControls.axaml index 72368060..520afeff 100644 --- a/src/Views/ConfigureCustomActionControls.axaml +++ b/src/Views/ConfigureCustomActionControls.axaml @@ -196,6 +196,22 @@ + + + + +