FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rich/examples/tree.py at main · Textualize/rich · GitHub
Textualize
/
rich
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
2.4k
Star
57.4k
Code
Issues
245
Pull requests
133
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rich
/
examples
/
tree.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
55 lines (49 loc) · 1.71 KB
Breadcrumbs
rich
/
examples
/
tree.py
Copy path
File metadata and controls
55 lines (49 loc) · 1.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Demonstrates how to display a tree of files / directories with the Tree renderable.
"""
import
os
import
pathlib
import
sys
from
rich
import
print
from
rich
.
filesize
import
decimal
from
rich
.
markup
import
escape
from
rich
.
text
import
Text
from
rich
.
tree
import
Tree
def
walk_directory
(
directory
:
pathlib
.
Path
,
tree
:
Tree
)
->
None
:
"""Recursively build a Tree with directory contents."""
# Sort dirs first then by filename
paths
=
sorted
(
pathlib
.
Path
(
directory
).
iterdir
(),
key
=
lambda
path
: (
path
.
is_file
(),
path
.
name
.
lower
()),
)
for
path
in
paths
:
# Remove hidden files
if
path
.
name
.
startswith
(
"."
):
continue
if
path
.
is_dir
():
style
=
"dim"
if
path
.
name
.
startswith
(
"__"
)
else
""
branch
=
tree
.
add
(
f"[bold magenta]:open_file_folder: [link file://
{
path
}
]
{
escape
(
path
.
name
)
}
"
,
style
=
style
,
guide_style
=
style
,
)
walk_directory
(
path
,
branch
)
else
:
text_filename
=
Text
(
path
.
name
,
"green"
)
text_filename
.
highlight_regex
(
r"\..*$"
,
"bold red"
)
text_filename
.
stylize
(
f"link file://
{
path
}
"
)
file_size
=
path
.
stat
().
st_size
text_filename
.
append
(
f" (
{
decimal
(
file_size
)
}
)"
,
"blue"
)
icon
=
"🐍 "
if
path
.
suffix
==
".py"
else
"📄 "
tree
.
add
(
Text
(
icon
)
+
text_filename
)
try
:
directory
=
os
.
path
.
abspath
(
sys
.
argv
[
1
])
except
IndexError
:
print
(
"[b]Usage:[/] python tree.py <DIRECTORY>"
)
else
:
tree
=
Tree
(
f":open_file_folder: [link file://
{
directory
}
]
{
directory
}
"
,
guide_style
=
"bold bright_blue"
,
)
walk_directory
(
pathlib
.
Path
(
directory
),
tree
)
print
(
tree
)
Back
|
FazBrowse Home
|
New Git URL