AllowedType.cs 3.2 KB

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