handleList method

Future<void> handleList(
  1. ArgResults flags,
  2. List<String> pathArgs
)

Implementation

Future<void> handleList(ArgResults flags, List<String> pathArgs) async {
  await _prepareClient();
  final path = pathArgs.isNotEmpty ? pathArgs.join(' ') : '/';
  final bool showFullUUIDs = flags['uuids'] || flags['detailed'];
  final bool detailed = flags['detailed'];

  final res = await client.resolvePath(path);

  if (res['type'] == 'file') {
    print('\ud83d\udcc4 File: ${p.basename(path)} (${res['uuid']})');
    return;
  }

  final uuid = res['uuid'];
  print('\ud83d\udcc2 ${res['path']} (UUID: ${uuid.substring(0, 8)}...)\n');

  final folders = await client.listFoldersAsync(uuid, detailed: detailed);
  final files = await client.listFolderFiles(uuid, detailed: detailed);
  final items = [...folders, ...files];

  if (items.isEmpty) {
    print('   (empty)');
    return;
  }

  const int nameWidth = 40;
  const int sizeWidth = 12;
  const int dateWidth = 10;
  final int uuidWidth = showFullUUIDs ? 36 : 11;

  String header;
  String top;
  String footer;

  if (detailed) {
    header =
        '\u2551  Type    ${"Name".padRight(nameWidth)}  ${"Size".padLeft(sizeWidth)}  ${"Modified".padLeft(dateWidth)}  ${"UUID".padRight(uuidWidth)} \u2551';
    top =
        '\u2554${"=" * 9}${"=" * nameWidth}${"=" * (sizeWidth + 2)}${"=" * (dateWidth + 2)}${"=" * (uuidWidth + 2)}\u2557';
    footer =
        '\u255a${"=" * 9}${"=" * nameWidth}${"=" * (sizeWidth + 2)}${"=" * (dateWidth + 2)}${"=" * (uuidWidth + 2)}\u255d';
  } else {
    header =
        '\u2551  Type    ${"Name".padRight(nameWidth)}  ${"Size".padLeft(sizeWidth)}  ${"UUID".padRight(uuidWidth)} \u2551';
    top =
        '\u2554${"=" * 9}${"=" * nameWidth}${"=" * (sizeWidth + 2)}${"=" * (uuidWidth + 2)}\u2557';
    footer =
        '\u255a${"=" * 9}${"=" * nameWidth}${"=" * (sizeWidth + 2)}${"=" * (uuidWidth + 2)}\u255d';
  }

  print(top);
  print(header);
  print(
      '\u2560${"=" * 9}${"=" * nameWidth}${"=" * (sizeWidth + 2)}${detailed ? "=" * (dateWidth + 2) : ""}${"=" * (uuidWidth + 2)}\u2563');

  int folderCount = 0;
  int fileCount = 0;

  for (var i in items) {
    final type = i['type'] == 'folder' ? '\ud83d\udcc1' : '\ud83d\udcc4';
    if (i['type'] == 'folder')
      folderCount++;
    else
      fileCount++;

    var name = i['name'] ?? 'Unknown';
    if (name.length > nameWidth)
      name = name.substring(0, nameWidth - 3) + '...';
    name = name.padRight(nameWidth);

    final size =
        (i['type'] == 'folder' ? '<DIR>' : formatSize(i['size'] ?? 0))
            .padLeft(sizeWidth);
    final itemUuid = i['uuid'] ?? 'N/A';
    final uuidDisplay =
        (showFullUUIDs ? itemUuid : '${itemUuid.substring(0, 8)}...')
            .padRight(uuidWidth);

    if (detailed) {
      final modified = i['lastModified'] ?? i['timestamp'];
      final dateDisplay = formatDate(modified).padLeft(dateWidth);
      print('\u2551  $type  $name  $size  $dateDisplay  $uuidDisplay \u2551');
    } else {
      print('\u2551  $type  $name  $size  $uuidDisplay \u2551');
    }
  }

  print(footer);
  print(
      '\n\ud83d\udcca Total: ${items.length} items ($folderCount folders, $fileCount files)');
}