Skip to main content

cmdline 1.0.1 copy "cmdline: ^1.0.1" to clipboard
cmdline: ^1.0.1 copied to clipboard

An SDK for console development

example/main.dart

import 'dart:io';

import 'package:cmdline/cmdline.dart';

Future<void> main(List<String> args) async => await cli.start(args);

CLI cli = CLI(
  name: 'demo',
  about: 'Features demo',
  flags: [
    const Flag(
      'all',
      short: 'a',
      help: 'Show prompts, menus and other things that need typing',
    ),
    const Flag(
      'hold',
      help: 'Keep the screen still for a few seconds at the end',
      hidden: true,
    ),
  ],
  options: [
    Option(
      'scene',
      short: 'S',
      help: 'Show only this scene of the showcase',
      valueName: 'number',
      allowed: [for (var i = 1; i <= _scenes.length; i++) '$i'],
    ),
  ],
  commands: [
    Command(
      name: 'spinner',
      about: 'A spinner that turns into a PASS line',
      run: (input) => CommandLineDemo.spinnerDemo(),
    ),
    Command(
      name: 'messages',
      about: 'One message of each log level',
      run: (input) => CommandLineDemo.printMessages(),
    ),
    Command(
      name: 'log-file',
      about: 'Saving log messages to a file',
      run: (input) => CommandLineDemo.logFile(),
    ),
    Command(
      name: 'styling',
      about: 'Text styles, colours and the palette',
      run: (input) => CommandLineDemo.styling(),
    ),
    Command(
      name: 'progress',
      about: 'Progress bars',
      run: (input) => CommandLineDemo.progressBar(),
    ),
    Command(
      name: 'theme',
      about: 'Default and custom themes',
      run: (input) => CommandLineDemo.theme(),
    ),
    Command(
      name: 'format',
      about: 'Boxes and tables',
      run: (input) => CommandLineDemo.format(),
    ),
    Command(
      name: 'paths',
      about: 'Joining and splitting paths',
      run: (input) => CommandLineDemo.paths(),
    ),
    Command(
      name: 'files',
      about: 'File helpers, find and showTree',
      run: (input) => CommandLineDemo.files(),
    ),
    Command(
      name: 'env',
      about: 'Variables, PATH and which',
      run: (input) => CommandLineDemo.environment(),
    ),
    Command(
      name: 'shell',
      about: 'Running commands from strings',
      run: (input) => CommandLineDemo.shell(),
    ),
    Command(
      name: 'folder',
      about: 'A cd that carries over to the next command',
      run: (input) => CommandLineDemo.terminalFolder(),
    ),
    Command(
      name: 'processes',
      about: 'Starting, reading and stopping programs',
      run: (input) => CommandLineDemo.processes(),
    ),
    Command(
      name: 'fetch',
      about: 'Download with a progress bar',
      run: (input) => CommandLineDemo.download(),
    ),
    Command(
      name: 'prompts',
      about: 'ask, confirm and the arrow-key menu',
      run: (input) => CommandLineDemo.prompts(),
    ),
    Command(
      name: 'interactive',
      about: 'A shell command that asks for your name',
      run: (input) => CommandLineDemo.interactiveCommand(),
    ),
    Command(
      name: 'open',
      about: 'Open dart.dev in your browser',
      run: (input) => CommandLineDemo.openApp(),
    ),
  ],
  run: (input) async {
    if (input.flag('all')) return CommandLineDemo.demoAll();

    final scene = input.option('scene');
    await showcase(only: scene == null ? null : int.parse(scene));

    if (input.flag('hold')) await delay(5);
  },
);

final List<(String, Future<void> Function())> _scenes = [
  ('Logging', _logging),
  ('Styling', _styling),
  ('Progress', _progress),
  ('Boxes and tables', _tables),
  ('Files', _files),
  ('Shell commands', _shell),
];

Future<void> showcase({int? only}) async {
  Logger.instance.verbose = true;
  try {
    if (only != null) {
      final (title, body) = _scenes[only - 1];
      await _scene(only, title, body);

      return;
    }

    await _intro();
    for (var i = 0; i < _scenes.length; i++) {
      final (title, body) = _scenes[i];
      await _scene(i + 1, title, body);
    }
    await _outro();
  } finally {
    stdout.write(SHOW_CURSOR);
  }
}

Future<void> _beat() => delay(0.25);

Future<void> _scene(
  int number,
  String title,
  Future<void> Function() body,
) async {
  final theme = CliTheme.current;
  _clear();
  CliTheme.say('');
  CliTheme.say(
    ' $number/${_scenes.length} '.dim + title.bold.foreground(theme.highlight),
  );
  CliTheme.say(' ${'─' * 40}'.dim.foreground(theme.border));
  CliTheme.say('');
  await delay(0.2);
  await body();
  await delay(1.6);
}

Future<void> _intro() async {
  _clear();
  CliTheme.say('');
  CliTheme.say(
    box(
      [
        'cmdline'.bold.foreground(AsciiPalette.sky),
        'Friendly command line tools in Dart'.dim,
      ].join('\n'),
    ),
  );
  await delay(1.5);
}

Future<void> _outro() async {
  _clear();
  CliTheme.say('');
  CliTheme.say(
    box(
      [
        'dart pub add cmdline'.bold.foreground(AsciiPalette.mint),
        'Logging, prompts, styling, files and shell'.dim,
      ].join('\n'),
      title: 'Get started',
    ),
  );
  CliTheme.say('');
  await delay(2);
}

Future<void> _logging() async {
  Logger.instance.startSpinner('Installing dependencies');
  await delay(1.2);
  Logger.instance.stopSpinner('Dependencies installed', Level.pass);
  await _beat();

  log('Building for release');
  await _beat();
  debug('Using cache in .dart_tool/build');
  await _beat();
  trace('Compiled 42 libraries');
  await _beat();
  warn('2 packages have newer versions');
  await _beat();
  fail('Could not reach the update server');
  await _beat();
  pass('Build finished in 3.2s');
}

Future<void> _styling() async {
  CliTheme.say(
    [
      'bold'.bold,
      'dim'.dim,
      'italic'.italic,
      'underline'.underline,
      'curly'.curlyUnderline.underlineColour(AsciiPalette.rose),
      'strike'.strikethrough,
      'inverse'.inverse,
    ].join('  '),
  );
  await delay(0.5);
  CliTheme.say('');

  final palette = {
    'orchid': AsciiPalette.orchid,
    'mint': AsciiPalette.mint,
    'sky': AsciiPalette.sky,
    'lavender': AsciiPalette.lavender,
    'rose': AsciiPalette.rose,
    'peach': AsciiPalette.peach,
    'butter': AsciiPalette.butter,
    'aqua': AsciiPalette.aqua,
  };
  final names = palette.keys.toList();
  for (var row = 0; row < names.length; row += 4) {
    final cells = [
      for (final name in names.skip(row).take(4))
        '${'██'.foreground(palette[name]!)} ${name.padRight(9)}',
    ];
    CliTheme.say(cells.join(' '));
    await _beat();
  }
  CliTheme.say('');

  const width = 48;
  const chunk = 6;
  for (var start = 0; start < width; start += chunk) {
    for (var i = start; i < start + chunk; i++) {
      final amount = 1 - i / (width - 1);
      stdout.write(
        ' '.background(
          AsciiPalette.sky.opacity(amount, over: AsciiPalette.rose),
        ),
      );
    }
    await delay(0.05);
  }
  stdout.writeln();
}

Future<void> _progress() async {
  Future<void> fill(ProgressBar bar, int upTo, double seconds) async {
    const frames = 15;
    for (var frame = 1; frame <= frames; frame++) {
      await delay(seconds / frames);
      bar.update((upTo * frame / frames).round());
    }
  }

  final download = ProgressBar(
    'Downloading',
    total: 60,
    colour: AsciiPalette.sky,
  );
  await fill(download, 60, 1);
  download.done('Downloaded 12.4 MB');
  await _beat();

  final compile = ProgressBar(
    'Compiling  ',
    total: 40,
    colour: AsciiPalette.lavender,
  );
  await fill(compile, 40, 0.8);
  compile.done('Compiled 40 files');
  await _beat();

  final upload = ProgressBar(
    'Uploading  ',
    total: 50,
    colour: AsciiPalette.peach,
  );
  await fill(upload, 35, 0.7);
  await delay(0.3);
  upload.failed('Upload stopped at 70%');
}

Future<void> _tables() async {
  final ok = 'up to date'.foreground(AsciiPalette.mint);
  final old = 'update'.foreground(AsciiPalette.butter);
  CliTheme.say(
    table(
      [
        ['args', '2.7.0', ok],
        ['http', '1.2.2', old],
        ['path', '1.9.1', ok],
        ['yaml', '3.1.2', old],
      ],
      header: ['Package', 'Version', 'Status'],
      rightAligned: {1},
    ),
  );
  await delay(0.6);
  CliTheme.say(
    box(
      '${'2'.bold} packages can be updated.\n'
      'Run ${'dart pub upgrade'.foreground(AsciiPalette.sky)} to get them.',
      title: 'Summary',
    ),
  );
}

Future<void> _files() async {
  final temp = Directory.systemTemp.createTempSync('cmdline_gif_').path;
  final app = join(temp, 'my_app');
  try {
    join(app, 'pubspec.yaml').write('name: my_app');
    join(app, 'README.md').write('# my_app');
    join(app, 'bin', 'my_app.dart').write('void main() {}');
    joinAll([app, 'lib', 'src', 'app.dart']).write('// app');
    join(app, 'test', 'app_test.dart').write('// tests');

    showTree(app);
    await delay(0.6);
    CliTheme.say('');

    final found = await find('*.dart', workingDirectory: app).toList();
    pass('Found ${found.length} Dart files');
    await _beat();
    for (final file in found) {
      CliTheme.say('  ${CliTheme.current.pointer} ${basename(file)}'.dim);
      await _beat();
    }
  } finally {
    deleteDir(temp);
  }
}

Future<void> _shell() async {
  await 'dart --version'.run;
  await _beat();
  await 'git rev-parse --verify no-such-branch'.start(nothrow: true);
}

void _clear() => stdout.write('$CLEAR_SCREEN$CURSOR_HOME$HIDE_CURSOR');