match.ion 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. for i in [1 2 3 4 5 6]
  2. match $((i % 2))
  3. case 0; echo "Even!"
  4. case 1; echo "Odd!"
  5. end
  6. end
  7. let out1 = "/foo/bar/baz.tar.gz: application/x-gzip";
  8. let out2 = "/foo/bar/baz.quxx: application/quxx-archive";
  9. fn analyze output
  10. match @split(output)[1]
  11. case application/x-gzip; echo "Use tar -xzf"
  12. case _ ; echo "Unknown file type"
  13. end
  14. end
  15. analyze $out1
  16. analyze $out2
  17. fn analyze_regex output
  18. match output
  19. case ".*application/x-gzip"; echo "Use tar -xzf"
  20. case _ ; echo "Unknown file type"
  21. end
  22. end
  23. analyze $out1
  24. analyze $out2
  25. fn wildcard input
  26. match $input
  27. case _; echo "WILDCARD!"
  28. case huh; echo "U N R E A C H A B L E"
  29. end
  30. end
  31. wildcard "FOOOOO"
  32. wildcard "huh"
  33. fn report usage
  34. match $usage
  35. case @(seq 0 25); echo "Plenty of space my guy"
  36. case @(seq 26 50); echo "Almost half full (or half empty)"
  37. case @(seq 51 75); echo "Getting close to full :O"
  38. case @(seq 76 99); echo "Time for spring cleaning, almost full!"
  39. case _; echo "How did you even do this..."
  40. end
  41. end
  42. report 37
  43. report 55
  44. report 98
  45. report FOOOO
  46. fn animated filetype
  47. match $filetype
  48. case ["image/jpeg" "image/png"]
  49. echo "Static :("
  50. case "image/gif"
  51. echo "Animated :D"
  52. end
  53. end
  54. animated "image/jpeg"
  55. animated "image/png"
  56. animated "image/gif"
  57. fn in_range min max val
  58. if test $min -gt $max
  59. let min max = $max $min
  60. end
  61. match $val
  62. case @(seq $min $max);
  63. if test $val -eq $min
  64. echo "$val at minimum"
  65. else if test $val -eq $max
  66. echo "$val at maximum"
  67. else
  68. echo "$val is between $min and $max"
  69. end
  70. case _
  71. if test $val -lt $min
  72. echo "$val is less than min=$min"
  73. else
  74. echo "$val is more than max=$max"
  75. end
  76. end
  77. end
  78. in_range 0 10 5
  79. in_range 0 10 0
  80. in_range 0 10 10
  81. in_range 1 10 0
  82. in_range 0 9 10