aboutsummaryrefslogtreecommitdiffstats
path: root/lib/guile/utils.scm
blob: ad1b1edac7ebd70a523dce24af1128049815e923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(define-module (utils)
  #:export (-> ->> expand-file))

(define-syntax ->
  (syntax-rules ()
    ((_ value) value)
    ((_ value (f1 . body) next ...) (-> (f1 value . body) next ...))
    ((_ value fun next ...) (-> (fun value) next ...))))

(define-syntax ->>
  (syntax-rules ()
    ((_ value) value)
    ((_ value (f ...) rest ...) (->> (f ... value) rest ...))
    ((_ value f rest ...) (->> (f value) rest ...))))

(define (expand-file f)
  ;; https://irreal.org/blog/?p=83
  (cond ((char=? (string-ref f 0) #\/) f)
        ((string=? (substring f 0 2) "~/")
         (let ((prefix (passwd:dir (getpwuid (geteuid)))))
           (string-append prefix (substring f 1 (string-length f)))))
        ((char=? (string-ref f 0) #\~)
         (let* ((user-end (string-index f #\/))
                (user (substring f 1 user-end))
                (prefix (passwd:dir (getpwnam user))))
           (string-append prefix (substring f user-end (string-length f)))))
        (else (string-append (getcwd) "/" f))))