Skip to content

Commit

Permalink
Added support for appending dirs (new arities for all functions).
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Nov 28, 2023
1 parent bfc30c8 commit ac033d6
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 58 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ lfe> (dirs:executable)
;; Mac: undefined
```

Note that all `dirs:*` functions support an additional arity, allowing one to append one or more path segments to the given directory:

``` erlang
1> dirs:home("lab").
"/Users/oubiwann/lab"
2> dirs:home(["lab", "lfe"]).
"/Users/oubiwann/lab/lfe"
3> dirs:video(["Conferences", "EUC", 2019, drafts]).
"/Users/oubiwann/Movies/Conferences/EUC/2019/drafts"
4> dirs:cache([2023,11,27,d34db33fcafe,0,1,5,12,42]).
"/Users/oubiwann/Library/Caches/2023/11/27/d34db33fcafe/0/1/5/12/42"
```

## Design Goals

- The _dirs_ library is a low-level crate designed to provide the paths to standard directories
Expand Down
32 changes: 30 additions & 2 deletions src/dirs-common.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(export
(abs-path? 1)
(home 0)
(home-subdir 1)))
(home-subdir 1)
(norm-path 2)))

(defun abs-path?
((`(#\/ . ,_)) 'true)
Expand All @@ -13,4 +14,31 @@
home))

(defun home-subdir (path-segs)
(filename:join (++ (dirs-common:home) path-segs)))
(norm-path (home) path-segs))

(defun norm-path
((prefix segs) (when (is_list segs))
(if (io_lib:printable_unicode_list segs)
(norm-path prefix (list segs))
(filename:join (++ (list prefix) (norm-segs segs)))))
((prefix seg)
(norm-path prefix (list (norm-seg seg)))))

(defun norm-segs (segs)
(norm-segs segs '()))

(defun norm-segs
(('() acc)
acc)
((`(,seg . ,rest) acc)
(norm-segs rest (++ acc (list (norm-seg seg))))))

(defun norm-seg
((seg) (when (is_list seg))
seg)
((seg) (when (is_atom seg))
(atom_to_list seg))
((seg) (when (is_binary seg))
(binary_to_list seg))
((seg)
(io_lib:format "~p" (list seg))))
4 changes: 2 additions & 2 deletions src/dirs-lin.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
;;; Private Functions

(defun config ()
(env-or-default "XDG_CONFIG_HOME" (dirs-common:home-subdir '(".config"))))
(env-or-default "XDG_CONFIG_HOME" (dirs-common:home-subdir ".config")))

(defun data ()
(env-or-default "XDG_DATA_HOME" (dirs-common:home-subdir '(".local" "share"))))

(defun env-or-default (env-var default)
(case (os:getenv env-var)
("" default)
Expand Down
26 changes: 17 additions & 9 deletions src/dirs-mac.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
(('runtime) 'undefined)
(('state) 'undefined)
(('template) 'undefined)

;; Erlang-supported
(('cache) (filename:basedir 'user_cache ""))

(('config) (app-support))
(('config-local) (app-support))
(('data) (app-support))
(('data-local) (app-support))

;; Custom
(('home) (dirs-common:home))
(('preference) (filename:join (list (library) "Preferences")))
(('font) (filename:join (list (library) "Fonts")))
(('audio) (dirs-common:home-subdir '("Music")))
(('desktop) (dirs-common:home-subdir '("Desktop")))
(('document) (dirs-common:home-subdir '("Documents")))
(('download) (dirs-common:home-subdir '("Downloads")))
(('picture) (dirs-common:home-subdir '("Pictures")))
(('public) (dirs-common:home-subdir '("Public")))
(('video) (dirs-common:home-subdir '("Movies"))))

(('preference) (library-subdir "Preferences"))
(('font) (library-subdir "Fonts"))

(('audio) (dirs-common:home-subdir "Music"))
(('desktop) (dirs-common:home-subdir "Desktop"))
(('document) (dirs-common:home-subdir "Documents"))
(('download) (dirs-common:home-subdir "Downloads"))
(('picture) (dirs-common:home-subdir "Pictures"))
(('public) (dirs-common:home-subdir "Public"))
(('video) (dirs-common:home-subdir "Movies")))

;;; Private Functions

Expand All @@ -33,3 +38,6 @@

(defun library ()
(filename:dirname (app-support)))

(defun library-subdir (path)
(dirs-common:norm-path (library) path))
14 changes: 5 additions & 9 deletions src/dirs-win.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
(export
(assemble 1)))

;;extern crate dirs_sys;

;;use std::path::PathBuf;

;;pub fn home_dir() -> Option<PathBuf> { dirs_sys::known_folder_profile() }

;;pub fn cache_dir() -> Option<PathBuf> { data_local_dir() }
Expand Down Expand Up @@ -35,9 +31,9 @@
(('state) 'undefined)
(('font) 'undefined)
;; Erlang-supported
(('cache) (filename:basedir 'user_cache ""))
(('config) (filename:basedir 'user_config ""))
(('config-local) (filename:basedir 'user_config ""))
(('data) (filename:basedir 'user_config "share"))
;;(('cache) (filename:basedir 'user_cache ""))
;;(('config) (filename:basedir 'user_config ""))
;;(('config-local) (filename:basedir 'user_config ""))
;;(('data) (filename:basedir 'user_config "share"))
;; Custom
)
)
Loading

0 comments on commit ac033d6

Please sign in to comment.