Skip to content

Commit

Permalink
ls/ls-url: introduce --size
Browse files Browse the repository at this point in the history
I really need it only for studio right now, but this is really nice to have in a daily life
as well.

E.g.

```
$ dvc ls-url . --size
108    Metadata-and-Results.dvc
97     AudioMP3.dvc
97     AudioWAV.dvc
12181  README.md
54     .gitignore
168    .gitattributes
99     VideoFlash.dvc
312    LICENSE.txt
139    .dvcignore
96     AudioMP3
96     Asset
192    .dvc
64     Metadata-and-Results
64     AudioWAV
192    Scripts
64     VideoFlash
384    .git
96     .vscode
```

```
$ dvc list . --size
139    .dvcignore
168    .gitattributes
54     .gitignore
96     .vscode
96     Asset
96     AudioMP3
97     AudioMP3.dvc
64     AudioWAV
97     AudioWAV.dvc
312    LICENSE.txt
64     Metadata-and-Results
108    Metadata-and-Results.dvc
12181  README.md
192    Scripts
64     VideoFlash
99     VideoFlash.dvc
```
  • Loading branch information
efiop committed Aug 17, 2023
1 parent 152991d commit a20309c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
20 changes: 16 additions & 4 deletions dvc/commands/ls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
logger = logging.getLogger(__name__)


def _prettify(entries, with_color=False):
def format_entries(entries, with_color=False, with_size=False):
if with_color:
ls_colors = LsColors()
fmt = ls_colors.format
Expand All @@ -20,7 +20,12 @@ def _prettify(entries, with_color=False):
def fmt(entry):
return entry["path"]

return [fmt(entry) for entry in entries]
def _row(entry):
if with_size:
return (entry.get("size") or ""), fmt(entry)
return (fmt(entry),)

return [_row(entry) for entry in entries]


class CmdList(CmdBaseNoRepo):
Expand All @@ -41,8 +46,10 @@ def run(self):
if self.args.json:
ui.write_json(entries)
elif entries:
entries = _prettify(entries, with_color=True)
ui.write("\n".join(entries))
entries = format_entries(
entries, with_color=True, with_size=self.args.size
)
ui.table(entries)
return 0
except DvcException:
logger.exception("failed to list '%s'", self.args.url)
Expand Down Expand Up @@ -106,6 +113,11 @@ def add_parser(subparsers, parent_parser):
"specified by '--remote') in the target repository."
),
)
list_parser.add_argument(
"--size",
action="store_true",
help="Show sizes.",
)
list_parser.add_argument(
"path",
nargs="?",
Expand Down
11 changes: 8 additions & 3 deletions dvc/commands/ls_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dvc.cli.utils import append_doc_link
from dvc.ui import ui

from .ls import _prettify
from .ls import format_entries

logger = logging.getLogger(__name__)

Expand All @@ -16,8 +16,8 @@ def run(self):

entries = Repo.ls_url(self.args.url, recursive=self.args.recursive)
if entries:
entries = _prettify(entries, with_color=True)
ui.write("\n".join(entries))
entries = format_entries(entries, with_color=True, with_size=self.args.size)
ui.table(entries)
return 0


Expand All @@ -40,4 +40,9 @@ def add_parser(subparsers, parent_parser):
action="store_true",
help="Recursively list files.",
)
lsurl_parser.add_argument(
"--size",
action="store_true",
help="Show sizes.",
)
lsurl_parser.set_defaults(func=CmdListUrl)
1 change: 1 addition & 0 deletions dvc/repo/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def _ls(
"isout": dvc_info.get("isout", False),
"isdir": info["type"] == "directory",
"isexec": info.get("isexec", False),
"size": info.get("size"),
}

return ret
1 change: 1 addition & 0 deletions dvc/repo/ls_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def ls_url(url, *, config=None, recursive=False):
ls_info = {
"path": fs.path.relpath(info["name"], fs_path),
"isdir": info["type"] == "directory",
"size": info.get("size"),
}
ret.append(ls_info)

Expand Down

0 comments on commit a20309c

Please sign in to comment.