Skip to content

Commit

Permalink
Add filename template variable for text contents
Browse files Browse the repository at this point in the history
  • Loading branch information
eltos committed Aug 28, 2024
1 parent c7a7c3e commit 493b06b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
15 changes: 12 additions & 3 deletions PasteIntoFile/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,20 @@ private void updateUiFromSettings() {
_disableUiEvents = false;
}

public static string formatFilenameTemplate(string template, DateTime timestamp, int count) {
return String.Format(template, timestamp, count);
public static string formatFilenameTemplate(string template, DateTime timestamp, int count, string textcontent) {
return string.Format(template, timestamp, count, textcontent);
}
public static string formatFilenameTemplate(string template, ClipboardContents contents, int count) {
var text = contents.Contents.OfType<TextContent>().FirstOrDefault()?.Text;
if (text != null) {
text = text.Trim().Split('\n').First().Trim(); // only first non-empty line
text = text.Substring(0, Math.Min(text.Length, 64)); // limit to 64 characters
text = Path.GetInvalidFileNameChars().Aggregate(text, (str, c) => str.Replace(c, '_')); // replace invalid chars
}
return formatFilenameTemplate(template, contents.Timestamp, count, text);
}
public string formatFilenameTemplate(string template) {
return formatFilenameTemplate(template, clipData.Timestamp, saveCount);
return formatFilenameTemplate(template, clipData, saveCount);
}
public void updateFilename(string filenameTemplate = null) {
try {
Expand Down
2 changes: 1 addition & 1 deletion PasteIntoFile/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public static IDataObject PatchedClipboardContents() {
// Analyze clipboard data
if (Clipboard.ContainsFileDropList()) return null;
var clipData = ClipboardContents.FromClipboard();
var filename = Dialog.formatFilenameTemplate(Settings.Default.filenameTemplate, clipData.Timestamp, saveCount);
var filename = Dialog.formatFilenameTemplate(Settings.Default.filenameTemplate, clipData, saveCount);
var ext = Dialog.determineExtension(clipData);
var contentToSave = clipData.ForExtension(ext);
if (contentToSave == null) return null;
Expand Down
5 changes: 3 additions & 2 deletions PasteIntoFile/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions PasteIntoFile/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ Tooltip</comment>
</data>
<data name="str_template_edit_info" xml:space="preserve">
<value>Die Vorlage wird zum Formatieren des Standarddateinamens verwendet. Folgende Variablen stehen zur Verfügung:
{0} - Datum und Zeit
{1} - Zahl gespeicherter Dateien (Stapelverarbeitung)
{0} Datum und Zeit
{1} Zahl gespeicherter Dateien (Stapelverarbeitung)
{2} Text in der Zwischenablage
Für Details zur Formatierung &lt;a&gt;hier klicken&lt;/a&gt;.</value>
</data>
<data name="str_template_edit_info_url" xml:space="preserve">
Expand Down
5 changes: 3 additions & 2 deletions PasteIntoFile/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ Do you want to overwrite it?</value>
</data>
<data name="str_template_edit_info" xml:space="preserve">
<value>The template is used to format the filename. The following variables are supported:
{0} - Date and time
{1} - Save count in batch mode
{0} Date and time
{1} Save count in batch mode
{2} Clipboard text
For details on the format specifiers &lt;a&gt;click here&lt;/a&gt;.</value>
</data>
<data name="str_template_edit_info_url" xml:space="preserve">
Expand Down
1 change: 1 addition & 0 deletions PasteIntoFile/TemplateEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public TemplateEdit() {
"{0:yyyyMMdd_HHmmss}",
"{0:yyyy-MM-dd}_{1:000}",
"PasteIntoFile_{1:000}_{0:fffffff}",
"{2}",
});
textTemplate.Text = Settings.Default.filenameTemplate;

Expand Down

0 comments on commit 493b06b

Please sign in to comment.