Skip to content

Commit

Permalink
XDG Base Directory Compliance (#431)
Browse files Browse the repository at this point in the history
This uses xdg to load files in the right place.
  • Loading branch information
Skyb0rg007 committed Jul 3, 2023
1 parent 21439f5 commit 9729963
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
@@ -1,6 +1,9 @@
2.13.0 (unreleased)
-------------------

* Load files from XDG directories (the legacy paths still work). (#431,
@Skyb0rg007)

* Remove deprecated values `prompt_continue`, `prompt_comment`, `smart_accept`,
`new_prompt_hooks`, `at_new_prompt` (#..., @emillon)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -63,7 +63,7 @@ Customization
### Colors

To add colors to utop, copy one of the files `utoprc-dark` or
`utoprc-light` to `~/.utoprc`. `utoprc-dark` is for terminals with
`utoprc-light` to `~/.config/utop/utoprc`. `utoprc-dark` is for terminals with
dark colors (such as white on black) and `utoprc-light` is for
terminals with light colors (such as black on white).

Expand Down
3 changes: 2 additions & 1 deletion dune-project
Expand Up @@ -26,4 +26,5 @@
(zed (>= 3.2.0))
(react (>= 1.0.0))
(cppo (>= 1.1.2))
(alcotest :with-test)))
(alcotest :with-test)
(xdg (>= 3.9.0))))
2 changes: 1 addition & 1 deletion src/lib/dune
Expand Up @@ -4,7 +4,7 @@
(wrapped false)
(modes byte)
(libraries compiler-libs.toplevel findlib.top lambda-term logs.lwt threads
zed)
xdg zed)
(preprocess
(action
(run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file}))))
Expand Down
3 changes: 2 additions & 1 deletion src/lib/uTop.ml
Expand Up @@ -23,9 +23,10 @@ let version = "%%VERSION%%"
(* +-----------------------------------------------------------------+
| History |
+-----------------------------------------------------------------+ *)
module Default_paths = UTop_private.Default_paths

let history = LTerm_history.create []
let history_file_name = ref (Some (Filename.concat LTerm_resources.home ".utop-history"))
let history_file_name = ref (Some (Default_paths.history_file_name))
let history_file_max_size = ref None
let history_file_max_entries = ref None
let stashable_session_history = UTop_history.create ()
Expand Down
23 changes: 22 additions & 1 deletion src/lib/uTop_private.ml
Expand Up @@ -9,6 +9,27 @@

open Lwt_react

module Default_paths = struct
let ( / ) = Filename.concat
let xdg = Xdg.create ~env:Sys.getenv_opt ()

let resolve ~legacy ~filename =
if Sys.file_exists legacy then
legacy
else
filename

let history_file_name =
resolve
~legacy:(LTerm_resources.home / ".utop-history")
~filename:(Xdg.state_dir xdg / "utop-history")

let config_file_name =
resolve
~legacy:(LTerm_resources.home / ".utoprc")
~filename:(Xdg.config_dir xdg / "utoprc")
end

let size, set_size =
let ev, set_size = E.create () in
let init = S.const { LTerm_geom.rows = 25; LTerm_geom.cols = 80 } in
Expand All @@ -27,7 +48,7 @@ let ui, set_ui = S.create Console

let error_style = ref LTerm_style.none

(* Config from ~/.utoprc *)
(* Config from $XDG_CONFIG_HOME/utop/utoprc *)
let autoload = ref true

let margin_function, set_margin_function =
Expand Down
3 changes: 2 additions & 1 deletion src/lib/uTop_styles.ml
Expand Up @@ -12,6 +12,7 @@ open UTop_token
let return, (>>=) = Lwt.return, Lwt.(>>=)

module String_set = Set.Make (String)
module Default_paths = UTop_private.Default_paths

type styles = {
mutable style_keyword : LTerm_style.t;
Expand Down Expand Up @@ -56,7 +57,7 @@ let styles = {
}

let load () =
let fn = Filename.concat LTerm_resources.home ".utoprc" in
let fn = Default_paths.config_file_name in
Lwt.catch
(fun () ->
LTerm_resources.load fn >>= fun res ->
Expand Down
1 change: 1 addition & 0 deletions utop.opam
Expand Up @@ -23,6 +23,7 @@ depends: [
"react" {>= "1.0.0"}
"cppo" {>= "1.1.2"}
"alcotest" {with-test}
"xdg" {>= "3.9.0"}
]
build: [
["dune" "subst"] {pinned}
Expand Down
2 changes: 1 addition & 1 deletion utoprc-dark
@@ -1,6 +1,6 @@
! -*- conf-xdefaults -*-

! Copy this file to ~/.utoprc
! Copy this file to $XDG_CONFIG_HOME/utoprc (~/.config/utoprc)

! Common resources

Expand Down
2 changes: 1 addition & 1 deletion utoprc-light
@@ -1,6 +1,6 @@
! -*- conf-xdefaults -*-

! Copy this file to ~/.utoprc
! Copy this file to $XDG_CONFIG_HOME/utoprc (~/.config/utoprc)

! Common resources

Expand Down

0 comments on commit 9729963

Please sign in to comment.