ledgerutil 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/zsh -f
  2. if [[ $# -eq 0 ]] ; then
  3. echo "ledgerutil [ name | list | new name [ perl | shell | python ] | edit name ]"
  4. exit
  5. fi
  6. function list-utilities () {
  7. for utility in ~/.ledger/utilities/*.(sh|pl|py) ;
  8. do
  9. case $utility in
  10. *.sh)
  11. echo " - $(basename -- ${utility} .sh)"
  12. ;;
  13. *.pl)
  14. echo " - $(basename -- ${utility} .pl)"
  15. ;;
  16. *.py)
  17. echo " - $(basename -- ${utility} .py)"
  18. esac
  19. done
  20. }
  21. function new-utility () {
  22. name=$1
  23. shift
  24. type=$1
  25. case $type in
  26. perl)
  27. cat <<EOF > ~/.ledger/utilities/${name}.pl
  28. #!/bin/perl
  29. use strict;
  30. use warnings;
  31. # Ledger Utility ${name}
  32. EOF
  33. ;;
  34. python)
  35. cat <<EOF > ~/.ledger/utilities/${name}.py
  36. #!/bin/python
  37. import ledger
  38. # Ledger Utility ${name}
  39. EOF
  40. ;;
  41. *)
  42. cat <<EOF > ~/.ledger/utilities/${name}.sh
  43. #!/bin/sh
  44. # Ledger Utility ${name}
  45. EOF
  46. esac
  47. edit-utility ${name}
  48. }
  49. function edit-utility () {
  50. name=$1
  51. shift
  52. if [[ -e ~/.ledger/utilities/${name}.sh ]] ; then
  53. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.sh
  54. elif [[ -e ~/.ledger/utilities/${name}.pl ]] ; then
  55. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.pl
  56. elif [[ -e ~/.ledger/utilities/${name}.py ]] ; then
  57. emacsclient --alternate-editor="" -n ~/.ledger/utilities/${name}.py
  58. fi
  59. }
  60. function run-utility () {
  61. name=$1
  62. shift
  63. if [[ -e ~/.ledger/utilities/${name}.sh ]] ; then
  64. ~/.ledger/utilities/${name}.sh $*
  65. elif [[ -e ~/.ledger/utilities/${name}.pl ]] ; then
  66. ~/.ledger/utilities/${name}.pl $*
  67. elif [[ -e ~/.ledger/utilities/${name}.py ]] ; then
  68. ~/.ledger/utilities/${name}.py $*
  69. else
  70. echo Utility ${name} not defined.
  71. fi
  72. }
  73. name=$1
  74. shift
  75. case ${name} in
  76. list)
  77. list-utilities
  78. ;;
  79. new)
  80. new-utility $*
  81. ;;
  82. edit)
  83. edit-utility $*
  84. ;;
  85. *)
  86. run-utility $name $*
  87. esac