@@ -75,7 +75,8 @@ class ScriptRunnerConfig {
7575 final sourceMap = await _tryFindConfig (fs, startDir);
7676
7777 if (sourceMap.isEmpty) {
78- throw StateError ('Must provide scripts in either pubspec.yaml or script_runner.yaml' );
78+ throw StateError (
79+ 'Must provide scripts in either pubspec.yaml or script_runner.yaml' );
7980 }
8081
8182 final source = sourceMap.values.first;
@@ -100,7 +101,9 @@ class ScriptRunnerConfig {
100101 List <dynamic >? scriptsRaw, {
101102 FileSystem ? fileSystem,
102103 }) {
103- final scripts = (scriptsRaw ?? []).map ((script) => RunnableScript .fromMap (script, fileSystem: fileSystem)).toList ();
104+ final scripts = (scriptsRaw ?? [])
105+ .map ((script) => RunnableScript .fromMap (script, fileSystem: fileSystem))
106+ .toList ();
104107 return scripts.map ((s) => s..preloadScripts = scripts).toList ();
105108 }
106109
@@ -126,14 +129,7 @@ class ScriptRunnerConfig {
126129 final titleStyle = [TerminalColor .bold, TerminalColor .brightWhite];
127130 printColor ('Built-in flags:' , titleStyle);
128131 print ('' );
129- var maxLen = '-h, --help' .length;
130- for (final scr in scripts) {
131- maxLen = math.max (maxLen, scr.name.length);
132- }
133- final padLen = maxLen + 6 ;
134- print (' ${colorize ('-h, --help' .padRight (padLen , ' ' ), [
135- TerminalColor .yellow
136- ])} ${colorize ('Print this help message' , [TerminalColor .gray ])}' );
132+ printBuiltins ();
137133 print ('' );
138134
139135 print (
@@ -145,29 +141,83 @@ class ScriptRunnerConfig {
145141 (configSource? .isNotEmpty == true
146142 ? [
147143 colorize (' on ' , titleStyle),
148- colorize (configSource! , [...titleStyle, TerminalColor .underline]),
144+ colorize (
145+ configSource! , [...titleStyle, TerminalColor .underline]),
149146 colorize (':' , titleStyle)
150147 ].join ('' )
151148 : ':' ),
152149 ].join ('' ),
153150 );
154151 print ('' );
152+ printScripts ();
153+ }
154+
155+ int _getPadLen (List <String > lines, [int ? initial]) {
156+ var maxLen = initial ?? 0 ;
155157 for (final scr in scripts) {
158+ maxLen = math.max (maxLen, scr.name.length);
159+ }
160+ final padLen = maxLen + 6 ;
161+ return padLen;
162+ }
163+
164+ /// Prints the list of scripts in the config.
165+ ///
166+ /// If [search] is provided, it filters the scripts to only those that contain the search string.
167+ void printScripts ([String search = '' ]) {
168+ var maxLen = '-h, --help' .length;
169+
170+ final filtered = search.isEmpty
171+ ? scripts
172+ : scripts
173+ .where ((scr) => [scr.name, scr.description]
174+ .any ((s) => s != null && s.contains (search)))
175+ .toList ();
176+
177+ final mapped = filtered
178+ .map ((scr) => TableRow (scr.name,
179+ scr.description ?? '\$ ${[scr .cmd , ...scr .args ].join (' ' )}' ))
180+ .toList ();
181+
182+ final padLen = _getPadLen (mapped.map ((r) => r.name).toList (), maxLen);
183+
184+ _printTable (mapped, padLen);
185+ }
186+
187+ /// Prints the list of scripts in the config.
188+ ///
189+ /// If [search] is provided, it filters the scripts to only those that contain the search string.
190+ void printBuiltins ([String search = '' ]) {
191+ final builtins = [
192+ TableRow ('-ls, --list [search]' ,
193+ 'List available scripts. Add search term to filter.' ),
194+ TableRow ('-h, --help' , 'Print this help message' ),
195+ ];
196+
197+ final padLen = _getPadLen (builtins.map ((b) => b.name).toList ());
198+
199+ _printTable (builtins, padLen);
200+ }
201+
202+ void _printTable (List <TableRow > filtered, int padLen) {
203+ for (final scr in filtered) {
156204 final lines = chunks (
157- scr.description ?? ' \$ ${[ scr . cmd , ... scr . args ]. join ( ' ' )}' ,
205+ scr.description,
158206 lineLength - padLen,
159207 stripColors: true ,
160208 wrapLine: (line) => colorize (line, [TerminalColor .gray]),
161209 );
162- printColor (' ${scr .name .padRight (padLen , ' ' )} ${lines .first }' , [TerminalColor .yellow]);
210+ printColor (' ${scr .name .padRight (padLen , ' ' )} ${lines .first }' ,
211+ [TerminalColor .yellow]);
163212 for (final line in lines.sublist (1 )) {
164213 print (' ${'' .padRight (padLen , ' ' )} $line ' );
165214 }
166215 print ('' );
167216 }
168217 }
169218
170- static Future <Map <String , Map >> _tryFindConfig (FileSystem fs, String startDir) async {
219+ static Future <Map <String , Map >> _tryFindConfig (
220+ FileSystem fs, String startDir) async {
171221 final explorer = Unaconfig ('script_runner' , fs: fs);
172222 final config = await explorer.search ();
173223 if (config != null ) {
@@ -286,3 +336,11 @@ enum OS {
286336 linux,
287337 // other
288338}
339+
340+ class TableRow {
341+ final String name;
342+ final String description;
343+
344+ TableRow (this .name, this .description);
345+ }
346+
0 commit comments