From 8bcfd6b334fe16294df853e11bfef3e2d3f6ca16 Mon Sep 17 00:00:00 2001 From: Stephen Sherratt Date: Fri, 25 Oct 2024 16:44:08 +1100 Subject: [PATCH] Tweaks to whitespace in generated bash --- src/climate/completion.ml | 16 +++++++++++----- src/climate/shell_dsl.ml | 10 +++++----- src/climate/shell_dsl.mli | 2 ++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/climate/completion.ml b/src/climate/completion.ml index eb81f2d..7687052 100644 --- a/src/climate/completion.ml +++ b/src/climate/completion.ml @@ -597,12 +597,13 @@ let rec functions_of_spec subcommand_completions @ subcommand_and_positional_arg_completion ;; -let bash_header ~program_name ~global_symbol_prefix ~local_variable_style = +let bash_header ~program_name ~global_symbol_prefix ~local_variable_style ~indent_size = let open Stmt in [ raw "#!/usr/bin/env bash" ; comment (sprintf "Completion script for %s. Generated by climate." program_name) ] - |> List.map ~f:(Bash.stmt_to_string ~global_symbol_prefix ~local_variable_style) + |> List.map + ~f:(Bash.stmt_to_string ~global_symbol_prefix ~local_variable_style ~indent_size) |> String.concat ~sep:"\n" ;; @@ -751,18 +752,23 @@ let generate_bash | `Random -> make_random_prefix () | `Custom s -> s in + let indent_size = if options.no_whitespace then 0 else 2 in let local_variable_style = if options.minify_local_variables then `Short else `Full in String.concat ~sep:(if options.no_whitespace then "\n" else "\n\n") - ([ bash_header ~program_name ~global_symbol_prefix ~local_variable_style ] + ([ bash_header ~program_name ~global_symbol_prefix ~local_variable_style ~indent_size + ] @ List.map all_functions ~f: - (Bash.global_named_value_to_string ~global_symbol_prefix ~local_variable_style) + (Bash.global_named_value_to_string + ~global_symbol_prefix + ~local_variable_style + ~indent_size) @ [ Stmt.raw_with_global_name (Global_named_value.name (List.hd all_functions)) ~f:(fun complete_entry -> sprintf "complete -F %s %s" complete_entry program_name) - |> Bash.stmt_to_string ~global_symbol_prefix ~local_variable_style + |> Bash.stmt_to_string ~global_symbol_prefix ~local_variable_style ~indent_size ]) ;; diff --git a/src/climate/shell_dsl.ml b/src/climate/shell_dsl.ml index a7a3756..1c00f58 100644 --- a/src/climate/shell_dsl.ml +++ b/src/climate/shell_dsl.ml @@ -534,8 +534,7 @@ module Bash = struct @ [ { indent = 0; text = "}" } ] ;; - let lines_to_string lines = - let indent_size = 4 in + let lines_to_string ~indent_size lines = List.map lines ~f:(fun { indent; text } -> let indent_string = String.make (indent * indent_size) ' ' in String.cat indent_string text) @@ -545,20 +544,21 @@ module Bash = struct let global_named_value_to_string ~global_symbol_prefix ~local_variable_style + ~indent_size global_named_value = global_named_value_to_lines ~global_symbol_prefix ~local_variable_style global_named_value - |> lines_to_string + |> lines_to_string ~indent_size ;; - let stmt_to_string ~global_symbol_prefix ~local_variable_style stmt = + let stmt_to_string ~global_symbol_prefix ~local_variable_style ~indent_size stmt = stmt_to_bash_lines_with_indent ~ctx:{ global_symbol_prefix; local_variable_style } ~indent:0 stmt - |> lines_to_string + |> lines_to_string ~indent_size ;; end diff --git a/src/climate/shell_dsl.mli b/src/climate/shell_dsl.mli index 0fbf895..d95e4c3 100644 --- a/src/climate/shell_dsl.mli +++ b/src/climate/shell_dsl.mli @@ -140,12 +140,14 @@ module Bash : sig val global_named_value_to_string : global_symbol_prefix:string -> local_variable_style:[ `Full | `Short ] + -> indent_size:int -> Global_named_value.t -> string val stmt_to_string : global_symbol_prefix:string -> local_variable_style:[ `Full | `Short ] + -> indent_size:int -> Stmt.t -> string end