fileme.nix 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. { pkgs, ... }: {
  2. programs.fish = {
  3. functions = {
  4. fme = {
  5. body = ''
  6. set -l base_dir "$HOME/nix-dots/data"
  7. argparse 'f/flake' 'l/license' 'list' -- $argv
  8. or return 1
  9. if not set -q _flag_flake; and not set -q _flag_license
  10. echo "Error: Must specify either --flake (-f) or --license (-l)."
  11. return 1
  12. else if set -q _flag_flake; and set -q _flag_license
  13. echo "Error: Cannot specify both --flake and --license at the same time."
  14. return 1
  15. end
  16. set -l mode ""
  17. set -l ext ""
  18. set -l dest ""
  19. if set -q _flag_flake
  20. set mode "flake"
  21. set ext "nix"
  22. set dest "flake.nix"
  23. else if set -q _flag_license
  24. set mode "license"
  25. set ext "txt"
  26. set dest "LICENSE"
  27. end
  28. set -l target_base "$base_dir/$mode"
  29. if set -q _flag_list
  30. if command -q tree
  31. tree --noreport -P "*.$ext" --prune "$target_base"
  32. else
  33. for dir in "$target_base"/*/
  34. if test -d "$dir"
  35. echo (basename "$dir")"/"
  36. for file in "$dir"*."$ext"
  37. if test -f "$file"
  38. echo " └── "(basename "$file")
  39. end
  40. end
  41. end
  42. end
  43. end
  44. return 0
  45. end
  46. set -l tool $argv[1]
  47. set -l name $argv[2]
  48. if test -z "$tool"
  49. echo "Error: No tool/category specified."
  50. return 1
  51. end
  52. set -l target_dir "$target_base/$tool"
  53. if not test -d "$target_dir"
  54. echo "Error: Directory $target_dir does not exist."
  55. return 1
  56. end
  57. set -l target_file ""
  58. if test -n "$name"
  59. set target_file "$target_dir/$name.$ext"
  60. else
  61. set target_file "$target_dir/default.$ext"
  62. end
  63. if not test -f "$target_file"
  64. echo "Error: File $target_file does not exist."
  65. return 1
  66. end
  67. echo "Copying $target_file -> ./$dest"
  68. cp "$target_file" "./$dest"
  69. '';
  70. };
  71. };
  72. completions = {
  73. fme = ''
  74. set -l base_dir "$HOME/nix-dots/data"
  75. complete -c fme -f
  76. set -l no_mode_set "not __fish_contains_opt -s f flake -s l license"
  77. complete -c fme -n "$no_mode_set" -s f -l flake -d "Target dev flakes (flake)"
  78. complete -c fme -n "$no_mode_set" -s l -l license -d "Target licenses (license)"
  79. complete -c fme -l list -d "List available items for selected mode"
  80. set -l get_mode_dir "
  81. set -l cmd (commandline -opc)
  82. if contains -- -l \$cmd; or contains -- --license \$cmd
  83. echo $base_dir/license
  84. else if contains -- -f \$cmd; or contains -- --flake \$cmd
  85. echo $base_dir/flake
  86. end
  87. "
  88. set -l get_ext "
  89. set -l cmd (commandline -opc)
  90. if contains -- -l \$cmd; or contains -- --license \$cmd
  91. echo txt
  92. else if contains -- -f \$cmd; or contains -- --flake \$cmd
  93. echo nix
  94. end
  95. "
  96. complete -c fme -n '__fish_is_nth_token 1' -a "(
  97. set -l dir ($get_mode_dir)
  98. if test -n \"\$dir\" -a -d \"\$dir\"
  99. for d in \$dir/*/
  100. basename \$d
  101. end
  102. end
  103. )" -d "Category"
  104. complete -c fme -n '__fish_is_nth_token 2' -a "(
  105. set -l cmd (commandline -opc)
  106. set -l dir ($get_mode_dir)
  107. set -l ext ($get_ext)
  108. set -l cat \$cmd[-1]
  109. if test -n \"\$cat\" -a -n \"\$dir\" -a -d \"\$dir/\$cat\"
  110. for file in \$dir/\$cat/*.\$ext
  111. if test -f \$file
  112. basename \$file .\$ext
  113. end
  114. end
  115. end
  116. )" -d "Template"
  117. '';
  118. };
  119. };
  120. }