enhance: use ~ to represent the home dir of current user
Some checks are pending
Continuous Integration / Build (push) Waiting to run
Continuous Integration / Prepare version string (push) Waiting to run
Continuous Integration / Package (push) Blocked by required conditions

This commit is contained in:
leo 2024-09-22 11:00:30 +08:00
parent 0b6ecc0388
commit 81b72f7c1c
No known key found for this signature in database
2 changed files with 19 additions and 4 deletions

View file

@ -1,4 +1,5 @@
using System.IO; using System;
using System.IO;
using Avalonia.Data.Converters; using Avalonia.Data.Converters;
@ -7,9 +8,23 @@ namespace SourceGit.Converters
public static class PathConverters public static class PathConverters
{ {
public static readonly FuncValueConverter<string, string> PureFileName = public static readonly FuncValueConverter<string, string> PureFileName =
new FuncValueConverter<string, string>(fullpath => Path.GetFileName(fullpath) ?? ""); new(v => Path.GetFileName(v) ?? "");
public static readonly FuncValueConverter<string, string> PureDirectoryName = public static readonly FuncValueConverter<string, string> PureDirectoryName =
new FuncValueConverter<string, string>(fullpath => Path.GetDirectoryName(fullpath) ?? ""); new(v => Path.GetDirectoryName(v) ?? "");
public static readonly FuncValueConverter<string, string> RelativeToHome =
new(v =>
{
if (OperatingSystem.IsWindows())
return v;
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var prefixLen = home.EndsWith('/') ? home.Length - 1 : home.Length;
if (v.StartsWith(home, StringComparison.Ordinal))
return "~" + v.Substring(prefixLen);
return v;
});
} }
} }

View file

@ -140,7 +140,7 @@
Margin="8,0" Margin="8,0"
HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalAlignment="Right" VerticalAlignment="Center"
Foreground="{DynamicResource Brush.FG2}" Foreground="{DynamicResource Brush.FG2}"
Text="{Binding Id}" Text="{Binding Id, Converter={x:Static c:PathConverters.RelativeToHome}}"
IsVisible="{Binding IsRepository}"/> IsVisible="{Binding IsRepository}"/>
</Grid> </Grid>
</DataTemplate> </DataTemplate>