generateFish static method

String generateFish(
  1. ToolDefinition tool
)

Generate fish completion script.

Implementation

static String generateFish(ToolDefinition tool) {
  final buf = StringBuffer();
  final name = tool.name;

  buf.writeln('# Fish completion for $name');
  buf.writeln('# Generated by tom_build_base v2');
  buf.writeln('');

  // Disable file completion by default
  buf.writeln('complete -c $name -f');
  buf.writeln('');

  // Commands
  buf.writeln('# Commands');
  for (final cmd in tool.commands) {
    buf.writeln(
      'complete -c $name -n "__fish_use_subcommand" -a ":${cmd.name}" -d "${cmd.description}"',
    );
  }
  buf.writeln('');

  // Global options
  buf.writeln('# Global options');
  for (final opt in tool.allGlobalOptions) {
    final short = opt.abbr != null ? '-s ${opt.abbr} ' : '';
    buf.writeln(
      'complete -c $name $short-l ${opt.name} -d "${opt.description}"',
    );
  }
  buf.writeln('');

  // Command-specific options
  for (final cmd in tool.commands) {
    buf.writeln('# :${cmd.name} options');
    for (final opt in cmd.options) {
      final short = opt.abbr != null ? '-s ${opt.abbr} ' : '';
      buf.writeln(
        'complete -c $name -n "__fish_seen_subcommand_from :${cmd.name}" $short-l ${opt.name} -d "${opt.description}"',
      );
    }
    buf.writeln('');
  }

  return buf.toString();
}