Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 196a476

Browse files
committed
sync, truncate: Add --progress/--no-progress support
Progress bars for coreutils are an extension specific to python-userland.
1 parent b8cb848 commit 196a476

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ build-backend = "setuptools.build_meta"
66
name = "userland"
77
version = "0.0.2"
88
requires-python = ">=3.13"
9+
dependencies = [
10+
"tqdm>=4.67.1"
11+
]
912
authors = [
1013
{ name="Expertcoderz", email="[email protected]"}
1114
]

userland/utilities/sync.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
import os
22
import sys
33

4+
from tqdm import tqdm
5+
46
from .. import core
57

68

79
parser = core.create_parser(
8-
usage=("%prog [FILE]...",),
10+
usage=("%prog [OPTION] [FILE]...",),
911
description="Sync the filesystem or write each FILE's blocks to disk.",
1012
)
1113

14+
parser.add_option(
15+
"--progress",
16+
dest="progress",
17+
action="store_true",
18+
help="show a progress bar when syncing files",
19+
)
20+
parser.add_option(
21+
"--no-progress",
22+
dest="progress",
23+
action="store_false",
24+
default=False,
25+
help="do not show a progress bar (default)",
26+
)
27+
1228

1329
@core.command(parser)
14-
def python_userland_sync(_, args):
30+
def python_userland_sync(opts, args):
1531
if args:
1632
failed = False
1733

18-
for name in args:
34+
for name in (
35+
tqdm(args, ascii=True, desc="Syncing files") if opts.progress else args
36+
):
1937
try:
2038
with open(name, "rb+") as io:
2139
os.fsync(io)

userland/utilities/truncate.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pathlib import Path
33
from typing import Callable
44

5+
from tqdm import tqdm
6+
57
from .. import core
68

79

@@ -13,6 +15,20 @@
1315
description="Shrink or extend each FILE to SIZE.",
1416
)
1517

18+
parser.add_option(
19+
"--progress",
20+
dest="progress",
21+
action="store_true",
22+
help="show a progress bar when truncating files",
23+
)
24+
parser.add_option(
25+
"--no-progress",
26+
dest="progress",
27+
action="store_false",
28+
default=False,
29+
help="do not show a progress bar (default)",
30+
)
31+
1632
parser.add_option("-c", "--no-create", action="store_true", help="do not create files")
1733
parser.add_option("-s", "--size", help="set or adjust file size by SIZE bytes")
1834
parser.add_option(
@@ -79,7 +95,9 @@ def python_userland_truncate(opts, args):
7995
print(e, file=sys.stderr)
8096
return 1
8197

82-
for file in map(Path, args):
98+
for file in map(
99+
Path, tqdm(args, ascii=True, desc="Truncating files") if opts.progress else args
100+
):
83101
if not file.exists() and opts.no_create:
84102
continue
85103

0 commit comments

Comments
 (0)