|
1 |
| -import 'dart:convert'; |
2 |
| -import 'dart:io'; |
3 |
| - |
4 |
| -import 'package:terrun/core/command_matcher.dart'; |
5 |
| - |
6 |
| -import 'core/core.dart'; |
7 |
| -import 'services/services.dart'; |
| 1 | +import 'package:args/command_runner.dart'; |
| 2 | +import 'package:terrun/features/configure/configure.dart'; |
| 3 | +import 'package:terrun/features/loop.dart'; |
| 4 | +import 'package:terrun/services/services.dart'; |
| 5 | +import 'package:terrun/services/shell/shell_service.dart'; |
| 6 | +import 'package:terrun/services/shell/shell_service_impl.dart'; |
8 | 7 |
|
9 | 8 | Future<void> app(List<String> args) async {
|
10 |
| - final matcher = CommandMatcher(); |
11 |
| - final RunnerService runner = ProcessRunnerService(); |
12 |
| - final DisplayService display = ConsoleDisplaySevice()..init(); |
13 |
| - |
14 |
| - final configContent = ConfigReader().read(); |
15 |
| - final config = ConfigParser().parse(configContent); |
16 |
| - final commands = config.commands; |
17 |
| - |
18 |
| - display.drawMatchingCommands('', commands); |
| 9 | + var env = Env.load(); |
19 | 10 |
|
20 |
| - while (true) { |
21 |
| - String input = ''; |
22 |
| - bool isPrefix = true; |
23 |
| - Command? selectedCommand; |
24 |
| - |
25 |
| - while (isPrefix) { |
26 |
| - final character = utf8.decode([stdin.readByteSync()]); |
27 |
| - final newInput = input + character; |
28 |
| - isPrefix = matcher.getPotentialMatches(commands, newInput) > 0; |
29 |
| - if (isPrefix) { |
30 |
| - input = newInput; |
31 |
| - display.drawMatchingCommands(input, commands); |
32 |
| - selectedCommand = matcher.getFromTree(commands, input); |
33 |
| - |
34 |
| - if (selectedCommand != null) { |
35 |
| - input = ''; |
36 |
| - stdout.writeln('running command:$selectedCommand'); |
37 |
| - break; |
38 |
| - } |
39 |
| - } else { |
40 |
| - input = ''; |
41 |
| - display.clear(); |
42 |
| - display.drawMatchingCommands(input, commands); |
43 |
| - |
44 |
| - var errorMessage = 'Error: "$newInput" didnt matchin any of keys above'; |
45 |
| - print(errorMessage.colored(1)); |
46 |
| - } |
47 |
| - } |
| 11 | + final ShellService shell = ShellServiceImpl(); |
| 12 | + final RunnerService shellRunner = ProcessRunnerService(shell); |
| 13 | + final DisplayService display = ConsoleDisplaySevice()..init(); |
48 | 14 |
|
49 |
| - if (selectedCommand != null) { |
50 |
| - if (selectedCommand.script != null) { |
51 |
| - await runner.run( |
52 |
| - selectedCommand.script!, |
53 |
| - config.hooks, |
54 |
| - ); |
55 |
| - } |
56 |
| - selectedCommand = null; |
57 |
| - display.drawMatchingCommands('', commands); |
58 |
| - } |
59 |
| - } |
| 15 | + final commandRunner = CommandRunner( |
| 16 | + 'terrun', |
| 17 | + 'run anything with minimum keystrokes', |
| 18 | + ); |
| 19 | + |
| 20 | + commandRunner |
| 21 | + ..addCommand(ConfigureCommand( |
| 22 | + display, |
| 23 | + shell, |
| 24 | + env, |
| 25 | + )) |
| 26 | + ..addCommand(LoopCommand( |
| 27 | + shellRunner, |
| 28 | + display, |
| 29 | + env, |
| 30 | + )); |
| 31 | + |
| 32 | + await commandRunner.run(args); |
60 | 33 | }
|
0 commit comments