postProcessBranches function

List<FigSuggestion> Function(String, [List<String>?]) postProcessBranches([
  1. PostProcessBranchesOptions options = const PostProcessBranchesOptions()
])

Implementation

List<FigSuggestion> Function(String, [List<String>?]) postProcessBranches(
    [PostProcessBranchesOptions options = const PostProcessBranchesOptions()]) {
  final insertWithoutRemotes = options.insertWithoutRemotes;

  return (String out, [List<String>? tokens]) {
    final output = filterMessages(out);
    if (output.startsWith('fatal:')) return [];

    final seen = <String>{};
    return output
        .split('\n')
        .where((line) => !line.trim().startsWith('HEAD'))
        .map((branch) {
          var name = branch.trim();
          final parts = RegExp(r'\S+')
              .allMatches(branch)
              .map((m) => m.group(0)!)
              .toList();
          if (parts.length > 1) {
            if (parts[0] == '*') {
              if (branch.contains('HEAD detached')) return null;
              return FigSuggestion(
                name: branch.replaceFirst('*', '').trim(),
                description: 'Current branch',
                priority: 100,
                icon: '⭐️',
              );
            }
            if (parts[0] == '+') {
              name = branch.replaceFirst('+', '').trim();
            }
          }

          var description = 'Branch';
          if (insertWithoutRemotes && name.startsWith('remotes/')) {
            final idx = name.indexOf('/', 8);
            name = idx != -1 ? name.substring(idx + 1) : name;
            description = 'Remote branch';
          }
          final space = name.indexOf(' ');
          if (space != -1) name = name.substring(0, space);

          return FigSuggestion(
            name: name,
            description: description,
            icon: 'fig://icon?type=git',
            priority: 75,
          );
        })
        .whereType<FigSuggestion>()
        .where((s) {
          if (seen.contains(s.nameSingle ?? '')) return false;
          seen.add(s.nameSingle ?? '');
          return true;
        })
        .toList();
  };
}