Skip to content

Commit b64cae9

Browse files
committed
feat: add -ls/--list flag
1 parent e33498d commit b64cae9

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.0
2+
3+
- Add `-ls`/`--list` flag
4+
15
## 0.6.2
26

37
- Update dependencies

lib/src/base.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Future<void> runScript(String entryName, List<String> args) async {
1010
config.printUsage();
1111
return;
1212
}
13+
if (['-ls', '--list'].contains(entryName)) {
14+
final search = args.isNotEmpty ? args.first : '';
15+
config.printScripts(search);
16+
return;
17+
}
1318
final entry = config.scriptsMap[entryName];
1419
if (entry == null) {
1520
throw StateError(

lib/src/config.dart

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: script_runner
22
description: Run all your project-related scripts in a portable, simple config.
3-
version: 0.6.2
3+
version: 0.7.0
44
homepage: https://casraf.dev/
55
repository: https://github.com/chenasraf/dart_script_runner
66
license: MIT

0 commit comments

Comments
 (0)