listCommand method

  1. @override
String listCommand(
  1. String args
)
override

A command, in this dialect, that lists a directory including hidden entries and with human-readable sizes (the ls -alh equivalent). args is the raw text typed after the shortcut (paths, extra flags), appended verbatim; empty lists the current directory.

Implementation

@override
String listCommand(String args) {
  // `-Force` includes hidden and system items. `Length` is raw bytes, so the
  // table swaps it for a right-aligned 1024-based size (`512`, `4.2K`,
  // `1.3G`); directories show no size, as their `Length` is meaningless.
  const table =
      r"Format-Table Mode,LastWriteTime,@{N='Size';A='Right';E={"
      r"if($_.PSIsContainer){''}else{$n=[double]$_.Length;$i=0;"
      r"while($n -ge 1024 -and $i -lt 4){$n/=1024;$i++};"
      r"if($i -eq 0){'{0}' -f $n}else{'{0:0.#}{1}' -f $n,'BKMGT'[$i]}}}},"
      r'Name -AutoSize';
  final target = args.isEmpty ? '' : ' $args';
  return 'Get-ChildItem -Force$target | $table';
}