AllowedType.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #nullable disable
  2. 
  3. namespace Terminal.Gui.Views;
  4. /// <summary>Interface for <see cref="FileDialog"/> restrictions on which file type(s) the user is allowed to select/enter.</summary>
  5. public interface IAllowedType
  6. {
  7. /// <summary>
  8. /// Returns true if the file at <paramref name="path"/> is compatible with this allow option. Note that the file
  9. /// may not exist (e.g. in the case of saving).
  10. /// </summary>
  11. /// <param name="path"></param>
  12. /// <returns></returns>
  13. bool IsAllowed (string path);
  14. }
  15. /// <summary><see cref="IAllowedType"/> that allows selection of any types (*.*).</summary>
  16. public class AllowedTypeAny : IAllowedType
  17. {
  18. /// <inheritdoc/>
  19. public bool IsAllowed (string path) { return true; }
  20. /// <summary>Returns a string representation of this <see cref="AllowedTypeAny"/>.</summary>
  21. /// <returns></returns>
  22. public override string ToString () { return Strings.fdAnyFiles + "(*.*)"; }
  23. }
  24. /// <summary>
  25. /// Describes a requirement on what <see cref="FileInfo"/> can be selected. This can be combined with other
  26. /// <see cref="IAllowedType"/> in a <see cref="FileDialog"/> to for example show only .csv files but let user change to
  27. /// open any if they want.
  28. /// </summary>
  29. public class AllowedType : IAllowedType
  30. {
  31. /// <summary>Initializes a new instance of the <see cref="AllowedType"/> class.</summary>
  32. /// <param name="description">The human-readable text to display.</param>
  33. /// <param name="extensions">Extension(s) to match e.g. .csv.</param>
  34. public AllowedType (string description, params string [] extensions)
  35. {
  36. if (extensions.Length == 0)
  37. {
  38. throw new ArgumentException ("You must supply at least one extension");
  39. }
  40. Description = description;
  41. Extensions = extensions;
  42. }
  43. /// <summary>Gets or Sets the human-readable description for the file type e.g. "Comma Separated Values".</summary>
  44. public string Description { get; set; }
  45. /// <summary>Gets or Sets the permitted file extension(s) (e.g. ".csv").</summary>
  46. public string [] Extensions { get; set; }
  47. /// <inheritdoc/>
  48. public bool IsAllowed (string path)
  49. {
  50. if (string.IsNullOrWhiteSpace (path))
  51. {
  52. return false;
  53. }
  54. string extension = Path.GetExtension (path);
  55. if (Extensions.Any (e => path.EndsWith (e, StringComparison.InvariantCultureIgnoreCase)))
  56. {
  57. return true;
  58. }
  59. // There is a requirement to have a particular extension and we have none
  60. if (string.IsNullOrEmpty (extension))
  61. {
  62. return false;
  63. }
  64. return Extensions.Any (e => e.Equals (extension, StringComparison.InvariantCultureIgnoreCase));
  65. }
  66. /// <summary>Returns <see cref="Description"/> plus all <see cref="Extensions"/> separated by semicolons.</summary>
  67. public override string ToString ()
  68. {
  69. const int maxLength = 30;
  70. var desc = $"{Description} ({string.Join (";", Extensions.Select (e => '*' + e).ToArray ())})";
  71. if (desc.Length > maxLength)
  72. {
  73. return desc.Substring (0, maxLength - 2) + "…";
  74. }
  75. return desc;
  76. }
  77. }