You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.elv 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. fn abort [&code=1 a]{
  2. echo (styled $a red)
  3. exit $code
  4. }
  5. fn set-title [a]{
  6. print "\e]0;"$a"\e\\"
  7. }
  8. fn merge-list [a b]{
  9. for c $b { a = [$@a $c] }
  10. put $a
  11. }
  12. fn merge-map [a b]{
  13. for k $b { a[$k] = $b[$k] }
  14. put $a
  15. }
  16. fn if-not-zero [a b]{
  17. if (not-eq $a 0) { $b }
  18. }
  19. fn switch [a b]{
  20. $b[$a]
  21. }
  22. fn pad [a b &with=' ' &left=$true]{
  23. p = (repeat (- $b (count $a)) $with | joins '')
  24. if $left {
  25. put $p$a
  26. } else {
  27. put $a$p
  28. }
  29. }
  30. fn add-before-readline [@hooks]{
  31. for hook $hooks {
  32. if (not (has-value $edit:before-readline $hook)) {
  33. edit:before-readline=[ $@edit:before-readline $hook ]
  34. }
  35. }
  36. }
  37. fn add-after-readline [@hooks]{
  38. for hook $hooks {
  39. if (not (has-value $edit:after-readline $hook)) {
  40. edit:after-readline=[ $@edit:after-readline $hook ]
  41. }
  42. }
  43. }
  44. fn fold [a b]{
  45. s = $a[0]
  46. for i [(range 1 (count $a))] {
  47. s = ($b $s $a[$i])
  48. }
  49. put $s
  50. }
  51. fn randselect [a]{
  52. put $a[(randint 0 (count $a))]
  53. }
  54. fn to-upper-ascii [a]{ if (and (< (ord $a) 123) (> (ord $a) 96)) { chr (- (ord $a) 32) } else { put $a } }
  55. fn to-lower-ascii [a]{ if (and (< (ord $a) 91 ) (> (ord $a) 64)) { chr (+ (ord $a) 32) } else { put $a } }