Commit
+59 -10 +/-2 browse
1 | diff --git a/lib/config.ml b/lib/config.ml |
2 | index a15d752..d642ec0 100644 |
3 | --- a/lib/config.ml |
4 | +++ b/lib/config.ml |
5 | @@ -38,6 +38,27 @@ module Encoding = struct |
6 | | key -> failwith (sprintf "unsupported encoding type: %s" key) |
7 | end |
8 | |
9 | + module Column = struct |
10 | + type t = [ `Title | `Tags | `WordCount | `Slug ] |
11 | + |
12 | + let to_string = function |
13 | + | `Title -> "title" |
14 | + | `Tags -> "tags" |
15 | + | `WordCount -> "words" |
16 | + | `Slug -> "slug" |
17 | + |
18 | + let of_string = function |
19 | + | "title" -> `Title |
20 | + | "tags" -> `Tags |
21 | + | "words" -> `WordCount |
22 | + | "slug" -> `Slug |
23 | + | key -> failwith (sprintf "unsupported column type: %s" key) |
24 | + |
25 | + let to_string_list t = String.concat ~sep:"," (List.map ~f:to_string t) |
26 | + |
27 | + let of_string_list str = List.map ~f:of_string (String.split ~on:',' str) |
28 | + end |
29 | + |
30 | module Key = struct |
31 | type t = |
32 | [ `StateDir |
33 | @@ -45,10 +66,19 @@ module Key = struct |
34 | | `Editor |
35 | | `OnModification |
36 | | `ListStyle |
37 | - | `Encoding ] |
38 | + | `Encoding |
39 | + | `ColumnList ] |
40 | |
41 | let all = |
42 | - [ `StateDir; `LockFile; `Editor; `OnModification; `ListStyle; `Encoding ] |
43 | + [ |
44 | + `StateDir; |
45 | + `LockFile; |
46 | + `Editor; |
47 | + `OnModification; |
48 | + `ListStyle; |
49 | + `Encoding; |
50 | + `ColumnList; |
51 | + ] |
52 | |
53 | let of_string = function |
54 | | "state_dir" -> `StateDir |
55 | @@ -57,6 +87,7 @@ module Key = struct |
56 | | "on_modification" -> `OnModification |
57 | | "list_style" -> `ListStyle |
58 | | "encoding" -> `Encoding |
59 | + | "column_list" -> `ColumnList |
60 | | key -> failwith (sprintf "bad configuration key %s" key) |
61 | |
62 | let to_string = function |
63 | @@ -66,6 +97,7 @@ module Key = struct |
64 | | `OnModification -> "on_modification" |
65 | | `ListStyle -> "list_style" |
66 | | `Encoding -> "encoding" |
67 | + | `ColumnList -> "column_list" |
68 | end |
69 | |
70 | type t = Yaml.value |
71 | @@ -76,6 +108,7 @@ type value = |
72 | | String of string option |
73 | | ListStyle of ListStyle.t option |
74 | | Encoding of Encoding.t option |
75 | + | ColumnList of Column.t list option |
76 | |
77 | let get_default = function |
78 | | `StateDir -> String (Some (Filename.concat base_xdg_share_path "/note")) |
79 | @@ -84,6 +117,7 @@ let get_default = function |
80 | | `OnModification -> String None |
81 | | `ListStyle -> ListStyle (Some `Fixed) |
82 | | `Encoding -> Encoding (Some `Raw) |
83 | + | `ColumnList -> ColumnList (Some [ `Title; `Tags; `WordCount; `Slug ]) |
84 | |
85 | let value_of_string key s = |
86 | match key with |
87 | @@ -93,6 +127,7 @@ let value_of_string key s = |
88 | | `OnModification -> String (Some s) |
89 | | `ListStyle -> ListStyle (Some (ListStyle.of_string s)) |
90 | | `Encoding -> Encoding (Some (Encoding.of_string s)) |
91 | + | `ColumnList -> ColumnList (Some (Column.of_string_list s)) |
92 | |
93 | let value_to_string value = |
94 | match value with |
95 | @@ -101,10 +136,14 @@ let value_to_string value = |
96 | match value with Some v -> ListStyle.to_string v | None -> "" ) |
97 | | Encoding value -> ( |
98 | match value with Some v -> Encoding.to_string v | None -> "" ) |
99 | + | ColumnList value -> ( |
100 | + match value with Some v -> Column.to_string_list v | None -> "" ) |
101 | |
102 | let get t key = |
103 | match Ezjsonm.find_opt t [ Key.to_string key ] with |
104 | - | Some json -> value_of_string key (Ezjsonm.get_string json) |
105 | + | Some json -> |
106 | + value_of_string key (Ezjsonm.get_string json) |
107 | + |
108 | | None -> get_default key |
109 | |
110 | let set t key value = |
111 | @@ -127,7 +166,8 @@ let get_string t key = |
112 | let load = |
113 | let cfg = |
114 | match Sys.file_exists config_path with |
115 | - | `Yes -> Yaml.of_string_exn (In_channel.read_all config_path) |
116 | + | `Yes -> |
117 | + Yaml.of_string_exn (In_channel.read_all config_path) |
118 | | `No | `Unknown -> |
119 | Unix.mkdir_p (Filename.dirname config_path); |
120 | Out_channel.write_all config_path |
121 | @@ -135,6 +175,7 @@ let load = |
122 | Yaml.of_string_exn (In_channel.read_all config_path) |
123 | in |
124 | |
125 | + (* intiailize the state directory if it is missing *) |
126 | let state_dir = get_string cfg `StateDir in |
127 | match Sys.file_exists state_dir with |
128 | | `Yes -> cfg |
129 | diff --git a/lib/config.mli b/lib/config.mli |
130 | index 47f088e..96fb0a3 100644 |
131 | --- a/lib/config.mli |
132 | +++ b/lib/config.mli |
133 | @@ -1,7 +1,7 @@ |
134 | open Base |
135 | |
136 | module ListStyle : sig |
137 | - type t = [`Fixed | `Wide | `Simple] |
138 | + type t = [ `Fixed | `Wide | `Simple ] |
139 | |
140 | val of_string : string -> t |
141 | |
142 | @@ -9,7 +9,15 @@ module ListStyle : sig |
143 | end |
144 | |
145 | module Encoding : sig |
146 | - type t = [`Json | `Yaml | `Raw] |
147 | + type t = [ `Json | `Yaml | `Raw ] |
148 | + |
149 | + val of_string : string -> t |
150 | + |
151 | + val to_string : t -> string |
152 | + end |
153 | + |
154 | + module Column : sig |
155 | + type t = [ `Title | `Tags | `WordCount | `Slug ] |
156 | |
157 | val of_string : string -> t |
158 | |
159 | @@ -17,13 +25,14 @@ module Encoding : sig |
160 | end |
161 | |
162 | module Key : sig |
163 | - type t = [ |
164 | - | `StateDir |
165 | + type t = |
166 | + [ `StateDir |
167 | | `LockFile |
168 | | `Editor |
169 | | `OnModification |
170 | | `ListStyle |
171 | - | `Encoding ] |
172 | + | `Encoding |
173 | + | `ColumnList ] |
174 | |
175 | val all : t list |
176 | |
177 | @@ -63,4 +72,3 @@ val get_string : t -> Key.t -> string |
178 | |
179 | val get_string_opt : t -> Key.t -> string option |
180 | (** get a string option by key *) |
181 | - |