this repo has no description
at main 109 lines 2.7 kB view raw
1{ }: 2 3let 4 colors = { 5 blue = "\\033[1;34m"; 6 dim = "\\033[0;2m"; 7 yellow = "\\033[1;33m"; 8 green = "\\033[0;32m"; 9 nc = "\\033[0m"; 10 }; 11in 12rec { 13 mkShellBrief = 14 { 15 pkgs, 16 banner, 17 setup ? [ ], 18 commands ? [ ], 19 }: 20 pkgs.writeShellScriptBin "brief" '' 21 clear 22 23 ${banner} 24 25 echo 26 echo 27 28 ${mkSections { 29 inherit pkgs setup commands; 30 }} 31 ''; 32 33 mkSections = 34 { 35 pkgs, 36 setup ? [ ], 37 commands ? [ ], 38 }: 39 let 40 lib = pkgs.lib; 41 42 mkVarName = name: "status_" + (builtins.replaceStrings [ " " "-" "." ] [ "_" "_" "_" ] name); 43 allSetupVars = map (item: mkVarName item.name) setup; 44 okValue = "${colors.green}Ok${colors.nc}"; 45 46 padTo = 47 target: str: 48 let 49 strLen = builtins.stringLength str; 50 diff = target - strLen; 51 actualDiff = if diff > 0 then diff else 1; 52 spaces = lib.concatStrings (builtins.genList (_: " ") actualDiff); 53 in 54 "${str}${spaces}"; 55 in 56 '' 57 # 1. State Calculation 58 ${lib.concatStringsSep "\n" ( 59 map (item: '' 60 ${mkVarName item.name}="${okValue}" 61 if ! ${item.condition}; then 62 ${mkVarName item.name}="${colors.yellow}${item.suggestion}${colors.nc}" 63 fi 64 '') setup 65 )} 66 67 # 2. Render Setup Section (Only if at least one step is not "Ok") 68 ${lib.optionalString (setup != [ ]) '' 69 SHOW_SETUP=false 70 for var in ${lib.concatStringsSep " " allSetupVars}; do 71 if [ "''${!var}" != "${okValue}" ]; then 72 SHOW_SETUP=true 73 break 74 fi 75 done 76 77 if [ "$SHOW_SETUP" = true ]; then 78 echo -e "${colors.blue}Setup${colors.nc}" 79 ${lib.concatStringsSep "\n" ( 80 lib.imap0 ( 81 i: item: 82 let 83 isLast = i == (builtins.length setup - 1); 84 icon = if isLast then "" else ""; 85 paddedName = padTo 20 item.name; 86 in 87 "echo -e \"${colors.dim} ${icon} ${colors.nc}${paddedName}\${${mkVarName item.name}}\"" 88 ) setup 89 )} 90 echo 91 fi 92 ''} 93 94 # 3. Render Commands Section 95 ${lib.optionalString (commands != [ ]) '' 96 echo -e "${colors.blue}Commands${colors.nc}" 97 ${lib.concatStringsSep "\n" ( 98 map ( 99 item: 100 let 101 paddedName = padTo 22 item.name; 102 in 103 "echo -e \" ${paddedName}${colors.dim}# ${item.help}${colors.nc}\"" 104 ) commands 105 )} 106 echo 107 ''} 108 ''; 109}