274 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			274 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| // Copyright (C) 2022 WYATT GROUP
 | |
| // Please see the AUTHORS file for details.
 | |
| //
 | |
| // This program is free software: you can redistribute it and/or modify
 | |
| // it under the terms of the GNU General Public License as published by
 | |
| // the Free Software Foundation, either version 3 of the License, or
 | |
| // any later version.
 | |
| //
 | |
| // This program is distributed in the hope that it will be useful,
 | |
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | |
| // GNU General Public License for more details.
 | |
| //
 | |
| // You should have received a copy of the GNU General Public License
 | |
| // along with this program. If not, see <https://www.gnu.org/licenses/>.
 | |
| 
 | |
| import 'dart:io';
 | |
| import 'dart:math';
 | |
| 
 | |
| import 'package:wyatt_cli_toolbox/wyatt_cli_toolbox.dart';
 | |
| 
 | |
| void testStylish() {
 | |
|   print('my beautiful text in magenta'.magenta());
 | |
|   print('you can combine multiple styles'.yellow().bgBlue().bold());
 | |
| 
 | |
|   print('\x1B[36mstart style');
 | |
|   print('style continues on this line');
 | |
|   print('but not on this line'.reset());
 | |
| 
 | |
|   print('\x1B[41mnot stripped\x1B[49m');
 | |
|   print('\x1B[41mstripped\x1B[49m'.strip());
 | |
| 
 | |
|   Stylish.config(enable: false);
 | |
|   print('you can disable extension'.fg(r: 20, g: 160, b: 80));
 | |
|   Stylish.config(enable: true);
 | |
|   print('and reenable it on the fly'.fg(r: 20, g: 160, b: 80));
 | |
|   print('custom bg color'.bg(r: 80, g: 110, b: 30));
 | |
| }
 | |
| 
 | |
| void testSequences() {
 | |
|   Sequences.clearEntireScreen();
 | |
|   print('Clear entire screen');
 | |
| 
 | |
|   print('Hide cursor');
 | |
|   Sequences.hideCursor();
 | |
|   sleep(Duration(seconds: 1));
 | |
|   print('Unhide cursor');
 | |
|   Sequences.unhideCursor();
 | |
|   sleep(Duration(seconds: 1));
 | |
| 
 | |
|   print('Save cursor position');
 | |
|   Sequences.savePosition();
 | |
|   sleep(Duration(seconds: 1));
 | |
| 
 | |
|   print('Move cursor');
 | |
|   Sequences.moveCursor(6, 3);
 | |
|   sleep(Duration(seconds: 1));
 | |
| 
 | |
|   Sequences.restorePosition();
 | |
|   Sequences.moveDown(1);
 | |
|   sleep(Duration(seconds: 1));
 | |
|   print('Restore cursor position');
 | |
| }
 | |
| 
 | |
| Future<void> testSpinners() async {
 | |
|   print('Starting computation 1...');
 | |
|   await Spinner.start(SpinnerType.fistBump);
 | |
|   sleep(Duration(seconds: 2));
 | |
|   Spinner.stop();
 | |
| 
 | |
|   print('Starting computation 2...');
 | |
|   await Spinner.start(SpinnerType.dots5, text: 'Loading...');
 | |
|   sleep(Duration(seconds: 3));
 | |
|   Spinner.stop();
 | |
| 
 | |
|   print('Starting computation 3...');
 | |
|   await Spinner.custom(
 | |
|     Indicator(
 | |
|       name: 'Eyes',
 | |
|       interval: 100,
 | |
|       frames: <String>['◡◡', '⊙⊙', '◠◠']
 | |
|           .map<String>((String e) => '\x1B[36m$e\x1B[39m')
 | |
|           .toList(),
 | |
|       label: 'Computing...',
 | |
|     ),
 | |
|   );
 | |
|   await Future<void>.delayed(Duration(seconds: 2));
 | |
|   Spinner.stop();
 | |
| 
 | |
|   print('Starting computation 4...');
 | |
|   await Spinner.start(SpinnerType.pipe, text: 'Linking...');
 | |
|   sleep(Duration(seconds: 3));
 | |
|   Spinner.update(
 | |
|     SpinnerType.circle.indicator.copyWith(label: 'Compiling...'),
 | |
|   );
 | |
|   sleep(Duration(seconds: 3));
 | |
|   Spinner.stop();
 | |
| }
 | |
| 
 | |
| void testPrompt() {
 | |
|   final Prompt pr = Prompt();
 | |
|   final StringQuestion q1 = StringQuestion(
 | |
|     "What's your name ?",
 | |
|     identifier: 'name',
 | |
|   );
 | |
|   final PasswordQuestion q8 = PasswordQuestion(
 | |
|     'Password ?',
 | |
|     comment: 'Secret shhht.',
 | |
|     identifier: 'password',
 | |
|   );
 | |
|   final NumberQuestion q7 = NumberQuestion(
 | |
|     'How old are you?',
 | |
|     identifier: 'age',
 | |
|   );
 | |
|   final BooleanQuestion q2 = BooleanQuestion(
 | |
|     'Do you like Dart ?',
 | |
|     identifier: 'dart',
 | |
|   );
 | |
|   final ListQuestion q3 = ListQuestion(
 | |
|     'Which target ?',
 | |
|     <String>['android', 'ios', 'windows', 'macos'],
 | |
|     identifier: 'platform',
 | |
|   );
 | |
|   final Message q4 = Message(
 | |
|     'Oh oh !',
 | |
|     level: MessageLevel.warn,
 | |
|     identifier: 'warn',
 | |
|   );
 | |
|   final Message q5 = Message(
 | |
|     'Oh damn :(',
 | |
|     level: MessageLevel.fail,
 | |
|     identifier: 'error',
 | |
|   );
 | |
|   final Message q6 = Message(
 | |
|     'Thank you, bye! 🤙',
 | |
|     comment: 'Pretty comment nah?',
 | |
|     identifier: 'bye',
 | |
|   );
 | |
| 
 | |
|   pr
 | |
|     ..addAll(<Question<dynamic>>[q1, q8, q7, q2, q3, q4, q5, q6])
 | |
|     ..ask();
 | |
| }
 | |
| 
 | |
| Future<void> testWorkflow() async {
 | |
|   final Task<int> t1 = Task<int>(
 | |
|     name: 'Analyzing',
 | |
|     id: 'analyze',
 | |
|     spinner: SpinnerType.circle,
 | |
|     task: () {
 | |
|       return waitFor<int>(
 | |
|         Future<int>.delayed(Duration(seconds: 3), () => 13),
 | |
|       );
 | |
|     },
 | |
|     onFinish: (Object? i) {
 | |
|       return '$i files analyzed';
 | |
|     },
 | |
|   );
 | |
|   final Task<int> t2 = Task<int>(
 | |
|     name: 'Formatting',
 | |
|     id: 'format',
 | |
|     spinner: SpinnerType.circle,
 | |
|     task: () {
 | |
|       return waitFor<int>(
 | |
|         Future<int>.delayed(Duration(seconds: 4), () => 1287),
 | |
|       );
 | |
|     },
 | |
|     onFinish: (Object? i) {
 | |
|       return '$i files modified';
 | |
|     },
 | |
|   );
 | |
|   final Task<void> t3 = Task<void>(
 | |
|     name: 'Building',
 | |
|     id: 'build',
 | |
|     task: () {
 | |
|       final List<String> _tasks = <String>[
 | |
|         'main.c',
 | |
|         'crypto.c',
 | |
|         'workflow.c',
 | |
|         'wyatt.c',
 | |
|         'logger.c',
 | |
|         'task.c',
 | |
|       ];
 | |
|       for (final String i in _tasks) {
 | |
|         Task.print(i);
 | |
|         sleep(Duration(seconds: Random().nextInt(4) + 1));
 | |
|       }
 | |
|     },
 | |
|     onFinish: (_) {
 | |
|       return 'Compilation success';
 | |
|     },
 | |
|   );
 | |
|   final Task<void> t4 = Task<void>(
 | |
|     name: 'Testing',
 | |
|     id: 'test',
 | |
|     task: () {
 | |
|       final List<String> _tasks = <String>[
 | |
|         'wyatt.h',
 | |
|         'crypto.h',
 | |
|       ];
 | |
|       for (final String i in _tasks) {
 | |
|         Task.print(i);
 | |
|         sleep(Duration(seconds: Random().nextInt(3)));
 | |
|       }
 | |
|       throw Exception('An error occured during ${'test::crypto.h'.red()}');
 | |
|     },
 | |
|     onFinish: (_) {
 | |
|       return 'All tests passed';
 | |
|     },
 | |
|     onError: (Object e) {
 | |
|       return e.toString();
 | |
|     },
 | |
|   );
 | |
|   final Task<void> t5 = Task<void>(
 | |
|     name: 'Bundling',
 | |
|     id: 'bundle',
 | |
|     task: () {
 | |
|       sleep(Duration(seconds: 3));
 | |
|       throw SkipException('Bundle skipped.');
 | |
|     },
 | |
|     onFinish: (_) {
 | |
|       return 'Files generated in dist/';
 | |
|     },
 | |
|     onSkip: (SkipException e) {
 | |
|       return e.toString();
 | |
|     },
 | |
|   );
 | |
| 
 | |
|   final Workflow w = await Workflow.create(<Task<dynamic>>[t1, t2, t3, t4, t5]);
 | |
|   w.start();
 | |
| 
 | |
|   print('\n${w.results.toString().dim()}');
 | |
| }
 | |
| 
 | |
| void testNewPrompt() {
 | |
|   final name = Input(message: 'Your name').prompt();
 | |
|   stdout.writeln(name);
 | |
| 
 | |
|   final email = Input(
 | |
|     message: 'Your email',
 | |
|     validator: (String x) {
 | |
|       if (x.contains('@')) {
 | |
|         return true;
 | |
|       } else {
 | |
|         throw ValidationError('Not a valid email');
 | |
|       }
 | |
|     },
 | |
|   ).prompt();
 | |
|   stdout.writeln(email);
 | |
| 
 | |
|   final planet = Input(
 | |
|     message: 'Your planet',
 | |
|     defaultValue: 'Earth',
 | |
|   ).prompt();
 | |
|   stdout.writeln(planet);
 | |
| 
 | |
|   final galaxy = Input(
 | |
|     message: 'Your galaxy',
 | |
|     initialValue: 'Andromeda',
 | |
|   ).prompt();
 | |
|   stdout.writeln(galaxy);
 | |
| }
 | |
| 
 | |
| Future<void> main() async {
 | |
|   // sleep(Duration(seconds: 2));
 | |
|   // testPrompt();
 | |
|   // await testWorkflow();
 | |
|   // testStylish();
 | |
|   // testSequences();
 | |
|   // await testSpinners();
 | |
|   // testPrompt();
 | |
|   testNewPrompt();
 | |
| }
 |