Skip to content

Commit

Permalink
Allow variables in output path (SabreTools#624)
Browse files Browse the repository at this point in the history
* Allow variables in output path and default output path

* Remove angle brackets when normalising path

* Add tooltip hover text for default output path

* Better tooltip formatting

* Use percent sign rather than angle brackets as variable delimiter

* Refactor
  • Loading branch information
Deterous authored Jan 11, 2024
1 parent 53b31f9 commit cfa07c1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions MPF.Core/Data/Drive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public string? FormattedVolumeLabel
return volumeLabel;

if (!string.IsNullOrEmpty(this.VolumeLabel))
{
volumeLabel = this.VolumeLabel;
}
else
{
// No Volume Label found, fallback to something sensible
Expand Down
4 changes: 3 additions & 1 deletion MPF.Core/InfoTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1876,8 +1876,10 @@ public static string NormalizeOutputPaths(string? path, bool getFullPath)
if (string.IsNullOrEmpty(path))
return string.Empty;

// Remove quotes from path
// Remove quotes and angle brackets from path
path = path!.Replace("\"", string.Empty);
path = path!.Replace("<", string.Empty);
path = path!.Replace(">", string.Empty);

// Try getting the combined path and returning that directly
string fullPath = getFullPath ? Path.GetFullPath(path) : path;
Expand Down
46 changes: 45 additions & 1 deletion MPF.Core/UI/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public bool MediaTypeComboBoxEnabled

/// <summary>
/// Currently provided output path
/// Not guaranteed to be a valid path
/// </summary>
public string OutputPath
{
Expand Down Expand Up @@ -1190,7 +1191,7 @@ private DumpEnvironment DetermineEnvironment()
{
return new DumpEnvironment(
this.Options,
this.OutputPath,
EvaluateOutputPath(this.OutputPath),
this.CurrentDrive,
this.CurrentSystem,
this.CurrentMediaType,
Expand Down Expand Up @@ -1299,6 +1300,49 @@ public void EnsureDiscInformation()
}
}

/// <summary>
/// Replaces %-delimited variables inside a path string with their values
/// </summary>
/// <param name="outputPath">Path to be evaluated</param>
/// <returns>String with %-delimited variables evaluated</returns>
public string EvaluateOutputPath(string outputPath)
{
string systemLong = this._currentSystem.LongName() ?? "Unknown System";
if (string.IsNullOrEmpty(systemLong))
systemLong = "Unknown System";
string systemShort = this._currentSystem.ShortName() ?? "unknown";
if (string.IsNullOrEmpty(systemShort))
systemShort = "unknown";
string mediaLong = this._currentMediaType.LongName() ?? "Unknown Media";
if (string.IsNullOrEmpty(mediaLong))
mediaLong = "Unknown Media";
string program = this._currentProgram.ToString() ?? "Unknown Program";
if (string.IsNullOrEmpty(program))
program = "Unknown Program";
string programShort = program == "DiscImageCreator" ? "DIC" : program;
if (string.IsNullOrEmpty(programShort))
programShort = "Unknown Program";
string label = this._currentDrive?.FormattedVolumeLabel ?? "track";
if (string.IsNullOrEmpty(label))
label = "track";
string date = DateTime.Today.ToString("yyyyMMdd");
if (string.IsNullOrEmpty(date))
date = "UNKNOWN";
string datetime = DateTime.Now.ToString("yyyyMMdd-HHmmss");
if (string.IsNullOrEmpty(datetime))
datetime = "UNKNOWN";

return outputPath
.Replace("%SYSTEM%", systemLong)
.Replace("%SYS%", systemShort)
.Replace("%MEDIA%", mediaLong)
.Replace("%PROGRAM%", program)
.Replace("%PROG%", programShort)
.Replace("%LABEL%", label)
.Replace("%DATE%", date)
.Replace("%DATETIME%", datetime);
}

/// <summary>
/// Get the default output directory name from the currently selected drive
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion MPF.UI.Core/Windows/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@

<Label Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Content="Default Output Path" />
<TextBox x:Name="DefaultOutputPathTextBox" Grid.Row="4" Grid.Column="1" Height="22" HorizontalAlignment="Stretch"
Text="{Binding Options.DefaultOutputPath}" VerticalContentAlignment="Center" />
Text="{Binding Options.DefaultOutputPath}" VerticalContentAlignment="Center"
ToolTip="Variables allowed:&#x0a; &#37;SYSTEM&#37;&#9;(System name, long)&#x0a; &#37;SYS&#37;&#9;&#9;(System name, short)&#x0a; &#37;MEDIA&#37;&#9;(Media type)&#x0a; &#37;PROGRAM&#37;&#9;(Program name, long)&#x0a; &#37;PROG&#37;&#9;(Program name, short)&#x0a; &#37;LABEL&#37;&#9;(Volume label)&#x0a; &#37;DATE&#37;&#9;(Current date)&#x0a; &#37;DATETIME&#37;&#9;(Current date and time)"/>
<Button x:Name="DefaultOutputPathButton" Grid.Row="4" Grid.Column="2" Height="22" Width="22" Content="..."
Style="{DynamicResource CustomButtonStyle}" />
</Grid>
Expand Down

0 comments on commit cfa07c1

Please sign in to comment.