ls.scm 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. (commands ls)
  2. ;; TODO: use previous result
  3. (define ls
  4. (lambda* (#:key
  5. (location (receive (result _) (pwd) (car result)))
  6. (show-hidden? #f)
  7. ;; command interface
  8. (previous-result '())
  9. (shell-state default-shell-state))
  10. "List files in the given LOCATION. If LOCATION is not specified, then
  11. list files in the current working directory. Return a list of file
  12. objects."
  13. (let ([result
  14. (let ([fs-tree (file-system-tree location)])
  15. (match fs-tree
  16. ;; directory
  17. [(name stat children ...)
  18. (let ([first-level-children
  19. ;; filter out hidden files if they are not wanted
  20. (filter (λ (child)
  21. (or show-hidden?
  22. (and (not show-hidden?)
  23. (not (hidden? child)))))
  24. (flat-children children))])
  25. (for-each (λ (child)
  26. (match child
  27. [(name stat)
  28. (simple-format #t "~a\n" name)]))
  29. first-level-children)
  30. first-level-children)]
  31. ;; flat file
  32. [(name stat)
  33. (simple-format #t "~a\n" stat)]))])
  34. (values (list result)
  35. shell-state))))
  36. (alias list-dir ls)