file.scm 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (library (file)
  2. (export remove-stat
  3. get-file
  4. get-filename
  5. get-children
  6. flat-children
  7. hidden?)
  8. (import (except (rnrs base) error)
  9. (only (guile)
  10. lambda* λ)
  11. ;; lists
  12. (srfi srfi-1)
  13. ;; strings
  14. (srfi srfi-13)
  15. ;; match-lambda
  16. (ice-9 match)
  17. ;; file-system-tree
  18. (ice-9 ftw)
  19. (alias))
  20. ;; from:
  21. ;; https://www.gnu.org/software/guile/manual/html_node/File-Tree-Walk.html
  22. (define remove-stat
  23. ;; Remove the `stat' object the `file-system-tree' provides
  24. ;; for each file in the tree.
  25. (match-lambda
  26. [(name stat) ; flat file
  27. name]
  28. [(name stat children ...) ; directory
  29. (list name (map remove-stat children))]))
  30. (define get-filename
  31. (λ (file)
  32. (car file)))
  33. (define get-file
  34. (λ (filename)
  35. (let ([fs-tree (file-system-tree filename)])
  36. (match fs-tree
  37. [(name stat) fs-tree]
  38. [(name stat children ...) (list name stat)]))))
  39. (define hidden?
  40. (λ (file)
  41. (string-prefix? "." (get-filename file))))
  42. (define get-children
  43. (lambda* (filename #:key (hidden #t))
  44. (match (file-system-tree filename)
  45. [(name stat children ...)
  46. (if hidden
  47. (filter hidden? children)
  48. children)]
  49. [other
  50. '()])))
  51. (define flat-children
  52. (λ (children)
  53. "Turns a file system tree into merely the first layer without the
  54. recursive children."
  55. (map (λ (child)
  56. ;; filter out its children, if it is a directory
  57. (match child
  58. ;; dir case
  59. [(name stat children ...)
  60. (list name stat)]
  61. ;; simple file case
  62. [name-and-stat name-and-stat]
  63. ;; what about symlinks?
  64. ))
  65. children)))
  66. #;(define hidden?
  67. (λ (file)
  68. "Check, whether a file is a hidden file."
  69. ;; assuming, that an empty filename is not possible
  70. (match file
  71. ;; dir case
  72. [(filename stat children ...)
  73. (char=? (string-ref filename 0) #\.)]
  74. ;; simple file case
  75. [(filename stat)
  76. (char=? (string-ref filename 0) #\.)])))
  77. )