Skip to content

Commit

Permalink
Allow to specify maximum string length
Browse files Browse the repository at this point in the history
  • Loading branch information
eltos committed Aug 28, 2024
1 parent 5b10280 commit c9a3a15
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 19 deletions.
4 changes: 2 additions & 2 deletions PasteIntoFile/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ private void updateUiFromSettings() {
}

public static string formatFilenameTemplate(string template, DateTime timestamp, int count, string textcontent) {
return string.Format(template, timestamp, count, textcontent);
return string.Format(new CustomFormatProvider(), 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 = text.Substring(0, Math.Min(text.Length, 256)); // limit to 256 characters
text = Path.GetInvalidFileNameChars().Aggregate(text, (str, c) => str.Replace(c, '_')); // replace invalid chars
}
return formatFilenameTemplate(template, contents.Timestamp, count, text);
Expand Down
26 changes: 26 additions & 0 deletions PasteIntoFile/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,30 @@ public static async Task<bool> CheckForUpdates() {
private const int ATTACH_PARENT_PROCESS = -1;

}


/// <summary>
/// A custom format provider that allows to
/// - For strings, specify the maximum length, e.g. "{0:15}" for max 15 characters
/// </summary>
public class CustomFormatProvider : IFormatProvider, ICustomFormatter {

public object GetFormat(Type formatType) {
return formatType == typeof(ICustomFormatter) ? this : null;
}

public string Format(string format, object arg, IFormatProvider formatProvider) {
// strings
if (arg is string str && !string.IsNullOrEmpty(format)) {
if (int.TryParse(format, out var length) && length > 0) {
return str.Substring(0, Math.Min(str.Length, length));
}
throw new FormatException("Invalid format string '" + format + "'. Expected a positive integer specifying maximum length.");
}
// default formatting
return string.Format("{0:" + format + "}", arg);
}

}

}
2 changes: 1 addition & 1 deletion PasteIntoFile/Properties/Resources.Designer.cs

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

3 changes: 0 additions & 3 deletions PasteIntoFile/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ Tooltip</comment>
{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">
<value>https://docs.microsoft.com/de-de/dotnet/standard/base-types/custom-date-and-time-format-strings</value>
</data>
<data name="str_filename_template" xml:space="preserve">
<value>Dateinamen-Vorlage</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions PasteIntoFile/Properties/Resources.it.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ Vuoi sovrascriverlo?</value>
{1} - Salva conteggio in modalità batch
Per i dettagli sugli specificatori di formato &lt;a&gt;clicca qui&lt;/a&gt;.</value>
</data>
<data name="str_template_edit_info_url" xml:space="preserve">
<value>https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings</value>
</data>
<data name="str_filename_template" xml:space="preserve">
<value>Modello nome file</value>
</data>
Expand Down
3 changes: 0 additions & 3 deletions PasteIntoFile/Properties/Resources.ko.resx
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ Tooltip</comment>
{1} - 저장 횟수 (배치 모드)
서식 지정자에 대한 자세한 내용은 &lt;a&gt;링크를 참고하세요&lt;/a&gt;.</value>
</data>
<data name="str_template_edit_info_url" xml:space="preserve">
<value>https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings</value>
</data>
<data name="str_filename_template" xml:space="preserve">
<value>파일명 템플릿</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion PasteIntoFile/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Do you want to overwrite it?</value>
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">
<value>https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings</value>
<value>https://github.com/eltos/PasteIntoFile#template-format</value>
</data>
<data name="str_filename_template" xml:space="preserve">
<value>Filename template</value>
Expand Down
3 changes: 0 additions & 3 deletions PasteIntoFile/Properties/Resources.uk-UA.resx
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ Tooltip</comment>
{1} - Збереження кількості у пакетному режимі
Детальніше про специфікатори форматування &lt;a&gt;натисніть тут&lt;/a&gt;.</value>
</data>
<data name="str_template_edit_info_url" xml:space="preserve">
<value>https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings</value>
</data>
<data name="str_filename_template" xml:space="preserve">
<value>Шаблон імені файлу</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion PasteIntoFile/TemplateEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public TemplateEdit() {
"{0:yyyyMMdd_HHmmss}",
"{0:yyyy-MM-dd}_{1:000}",
"PasteIntoFile_{1:000}_{0:fffffff}",
"{2}",
"{2:10}",
});
textTemplate.Text = Settings.Default.filenameTemplate;

Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ Otherwise, a dialog will prompt for filename and type.
By holding `SHIFT` when the program starts, the autosave mode setting can be temporarily inverted (show the dialog even though autosave is enabled, or skip the dialog even though autosave is disabled).
When running Paste Into File from the start menu or tray, the dialog will always be shown.

The **filename template** can be edited from the dialog or via command line.
The **filename template** can be edited from the dialog or via command line (see [below](#template-format) for a description of the available formats).
When holding `CTRL` while the program starts, the file will be saved to a subdirectory.
The corresponding template can be configured via command line.
The corresponding template can be configured via command line (see below).

The available **file extensions** depend on the formats available in the clipboard.
For example, if you copy a range of cells from a spreadsheet, the data is available not only as text, but also in DIF, RTF, SLK and HTML formats and even as screenshot.
Expand Down Expand Up @@ -117,6 +117,15 @@ Currently, image and text files are supported. If the file format is not underst
For non-text files, the file path is copied as text in addition to the image contents and file.


<a id="template-format"></a>
### Filename template format
The filename and/or subfolder template string may contain the following placeholders:
- `{0}`: The current date and time.
The format can be specified, e.g. `{0:yyyy-MM-dd HH-mm-ss}`, see [format specifiers](https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings) for details.
- `{1}`: A counter for batch mode.
The format can be specified, e.g. `{1:000}` for zero padding, see [format specifiers](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings) for details.
- `{2}`: The clipboard text.
The format can be specified, e.g. `{2:15}` to limit it to 15 characters.

### Command Line

Expand Down

0 comments on commit c9a3a15

Please sign in to comment.