Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated XDG base directory handling #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/lTerm_resources.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let home =
else
""

type xdg_location = Cache | Config | Data
type xdg_location = Cache | Config | Data | State

module XDGBD = struct
let ( / ) = Filename.concat
Expand All @@ -34,23 +34,33 @@ module XDGBD = struct
try
Sys.getenv env_var
with Not_found ->
if Sys.win32 then win32_default else unix_default
if Sys.win32 then
win32_default ()
else
unix_default

let cache = get "XDG_CACHE_HOME" (home / ".cache") (home / "Local Settings" / "Cache")
let config = get "XDG_CONFIG_HOME" (home / ".config") (home / "Local Settings")
let data = get "XDG_DATA_HOME" (home / ".local" / "share") (try Sys.getenv "AppData" with Not_found -> "")
let cache = get "XDG_CACHE_HOME" (home / ".cache") @@ fun () ->
try Sys.getenv "LocalAppData" / "Cache" with Not_found -> home / "AppData" / "Local" / "Cache"
let config = get "XDG_CONFIG_HOME" (home / ".config") @@ fun () ->
try Sys.getenv "AppData" with Not_found -> home / "AppData" / "Roaming"
let data = get "XDG_DATA_HOME" (home / ".local" / "share") @@ fun () ->
try Sys.getenv "AppData" with Not_found -> home / "AppData" / "Roaming"
let state = get "XDG_STATE_HOME" (home / ".local" / "state") @@ fun () ->
try Sys.getenv "LocalAppData" with Not_found -> home / "AppData" / "Local"

let user_dir = function
| Cache -> cache
| Config -> config
| Data -> data
| State -> state
end

let xdgbd_warning loc file_name =
let loc_name = match loc with
| Cache -> "$XDG_CACHE_HOME"
| Config -> "$XDG_CONFIG_HOME"
| Data -> "$XDG_DATA_HOME" in
| Data -> "$XDG_DATA_HOME"
| State -> "$XDG_STATE_HOME" in
Printf.eprintf
"Warning: it is recommended to move `%s` to `%s`, see:\n%s\n"
file_name loc_name
Expand Down
6 changes: 4 additions & 2 deletions src/lTerm_resources.mli
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ val load : string -> t Lwt.t
val home : string
(** The home directory. *)

type xdg_location = Cache | Config | Data
(** The type for user-specific 'cached', 'configuration' and 'data' files. *)
type xdg_location = Cache | Config | Data | State
(** The type for user-specific 'cached', 'configuration',
'data', and 'state' files.
*)

val xdgbd_file : loc:xdg_location -> ?legacy_name:string -> string -> string
(** [xdgbd_file ~loc fn] returns the full file-name for a file [fn] in the
Expand Down