diff options
author | Daniil Baturin <daniil@baturin.org> | 2018-06-05 16:35:12 +0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2018-06-05 16:35:12 +0700 |
commit | 335560d2c0420a363942d3368dec0b04dd8bf62b (patch) | |
tree | d47f9944e4e5b2881707151c40d9fa5858849ce8 | |
parent | 7f26d95164265b0ffd345afd510bf03ee98cd1d8 (diff) | |
download | vyconf-335560d2c0420a363942d3368dec0b04dd8bf62b.tar.gz vyconf-335560d2c0420a363942d3368dec0b04dd8bf62b.zip |
Add a function for getting sorted children from a node and use it for sorting set commands.
-rw-r--r-- | src/config_tree.ml | 7 | ||||
-rw-r--r-- | src/config_tree.mli | 2 | ||||
-rw-r--r-- | src/vytree.ml | 5 | ||||
-rw-r--r-- | src/vytree.mli | 2 |
4 files changed, 13 insertions, 3 deletions
diff --git a/src/config_tree.ml b/src/config_tree.ml index c6fddb4..9b630d9 100644 --- a/src/config_tree.ml +++ b/src/config_tree.ml @@ -331,12 +331,15 @@ let render_at_level let child_configs = List.map (render ~indent:indent ~reftree:reftree ~cmp:cmp ~showephemeral:showephemeral ~showinactive:showinactive) children in List.fold_left (Printf.sprintf "%s\n%s") "" child_configs -let render_commands ?(reftree=None) ?(alwayssort=false) node path = +let render_commands ?(reftree=None) ?(alwayssort=false) ?(sortchildren=false) node path = let node = match path with | [] -> node | _ -> Vytree.get node path in - let children = Vytree.children_of_node node in + let children = + if sortchildren then Vytree.sorted_children_of_node (BatString.numeric_compare) node + else Vytree.children_of_node node + in let commands = List.map (Renderer.render_commands ~reftree:reftree ~alwayssort:alwayssort path) children in String.concat "\n" commands diff --git a/src/config_tree.mli b/src/config_tree.mli index 0e21f59..79426e3 100644 --- a/src/config_tree.mli +++ b/src/config_tree.mli @@ -75,4 +75,4 @@ val render_at_level : string list -> string -val render_commands: ?reftree:(Reference_tree.t option) -> ?alwayssort:bool -> t -> string list -> string +val render_commands: ?reftree:(Reference_tree.t option) -> ?alwayssort:bool -> ?sortchildren:bool -> t -> string list -> string diff --git a/src/vytree.ml b/src/vytree.ml index 156b326..b200cc4 100644 --- a/src/vytree.ml +++ b/src/vytree.ml @@ -164,3 +164,8 @@ let get_existent_path node path = let children_of_path node path = let node' = get node path in list_children node' + +let sorted_children_of_node cmp node = + let names = list_children node in + let names = List.sort cmp names in + List.map (find_or_fail node) names diff --git a/src/vytree.mli b/src/vytree.mli index 9bc8844..da593f1 100644 --- a/src/vytree.mli +++ b/src/vytree.mli @@ -40,3 +40,5 @@ val get_data : 'a t -> string list -> 'a val exists : 'a t -> string list -> bool val children_of_path : 'a t -> string list -> string list + +val sorted_children_of_node : (string -> string -> int) -> 'a t -> ('a t) list |