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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. a = (to-string $a)
  24. p = (repeat (- $b (count $a)) $with | joins '')
  25. if $left {
  26. put $p$a
  27. } else {
  28. put $a$p
  29. }
  30. }
  31. fn add-before-readline [@hooks]{
  32. for hook $hooks {
  33. if (not (has-value $edit:before-readline $hook)) {
  34. edit:before-readline=[ $@edit:before-readline $hook ]
  35. }
  36. }
  37. }
  38. fn add-after-readline [@hooks]{
  39. for hook $hooks {
  40. if (not (has-value $edit:after-readline $hook)) {
  41. edit:after-readline=[ $@edit:after-readline $hook ]
  42. }
  43. }
  44. }
  45. fn fold [a b]{
  46. s = $a[0]
  47. for i [(range 1 (count $a))] {
  48. s = ($b $s $a[$i])
  49. }
  50. put $s
  51. }
  52. fn randselect [a]{
  53. put $a[(randint 0 (count $a))]
  54. }
  55. fn to-upper-ascii [a]{ if (and (< (ord $a) 123) (> (ord $a) 96)) { chr (- (ord $a) 32) } else { put $a } }
  56. fn to-lower-ascii [a]{ if (and (< (ord $a) 91 ) (> (ord $a) 64)) { chr (+ (ord $a) 32) } else { put $a } }