1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

lib/shell: add formatShellArrayContent function for intelligent width optimization

Add shared utility function that formats shell arrays with smart width optimization.
Packs multiple items per line based on available width (~78 chars per line) while
maintaining readability. Includes helper functions wrapLines and formatMultiLine.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman 2025-09-26 16:43:31 -05:00
parent 11cc3d55de
commit a8dbb4e325

View file

@ -41,6 +41,59 @@ rec {
# statement for each set entry.
exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars);
# Wrap a list of strings to a given line width.
# Packs as many items as possible per line without exceeding maxWidth.
# Returns a list of strings, each representing a line.
#
# Example: wrapLines ["item1" "item2" "very-long-item" "item3"] 20
# => ["item1 item2" "very-long-item" "item3"]
wrapLines =
items: maxWidth:
let
step =
acc: item:
let
potentialLine = if acc.currentLine == "" then item else "${acc.currentLine} ${item}";
in
if lib.stringLength potentialLine <= maxWidth then
acc // { currentLine = potentialLine; }
else
acc
// {
finishedLines = acc.finishedLines ++ [ acc.currentLine ];
currentLine = item;
};
foldResult = lib.foldl' step {
finishedLines = [ ];
currentLine = "";
} items;
in
foldResult.finishedLines ++ lib.optional (foldResult.currentLine != "") foldResult.currentLine;
# Formats a list of items for shell array content with intelligent width optimization.
# IMPORTANT: This formats the CONTENTS of an array (what goes inside parentheses),
# not a complete array definition. Use lib.hm.zsh.define for complete definitions.
#
# Uses lib.escapeShellArg for robust shell escaping (handles spaces, quotes, special chars).
# Packs multiple items per line to optimize terminal width (~78 chars per line).
# Short arrays (≤3 items, ≤80 chars total) use single-line format.
#
# Example outputs:
# Empty: ""
# Simple: item1 item2 item3
# With spaces: 'item one' 'item two' 'item three'
# Long arrays: \n item1 item2 item3\n item4 item5\n
#
# Built from composable helpers: wrapLines, formatMultiLine
formatShellArrayContent =
items:
let
quotedItems = lib.map lib.escapeShellArg items;
formatMultiLine = lines: "\n ${lib.concatStringsSep "\n " lines}\n";
wrapped = wrapLines quotedItems 78;
in
formatMultiLine wrapped;
mkBashIntegrationOption = mkShellIntegrationOption "Bash";
mkFishIntegrationOption = mkShellIntegrationOption "Fish";
mkIonIntegrationOption = mkShellIntegrationOption "Ion";