Skip to content

Commit

Permalink
Tweaks to whitespace in generated bash
Browse files Browse the repository at this point in the history
  • Loading branch information
gridbugs committed Oct 25, 2024
1 parent a457f52 commit 8bcfd6b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/climate/completion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
;;

Expand Down Expand Up @@ -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
])
;;
10 changes: 5 additions & 5 deletions src/climate/shell_dsl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
2 changes: 2 additions & 0 deletions src/climate/shell_dsl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8bcfd6b

Please sign in to comment.