Skip to content

Commit a496168

Browse files
committed
ls command
1 parent a209f58 commit a496168

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import sys
2+
import os
3+
import pwd
4+
import grp
5+
import stat
6+
from datetime import datetime
7+
from pathlib import Path
8+
9+
10+
def main():
11+
args = sys.argv[1:]
12+
flags = []
13+
parentDir = ".."
14+
currentDir = "."
15+
path = currentDir
16+
for item in args:
17+
if item[0] == "-":
18+
flags.append(item)
19+
else:
20+
path = item
21+
scanResult = os.scandir(path)
22+
isHiddenToShow = "-a" in flags
23+
isStatsToList = "-l" in flags
24+
content = sortDirContent(scanResult, isHiddenToShow)
25+
26+
if not isHiddenToShow and not isStatsToList:
27+
output = ""
28+
for item in content:
29+
output += (
30+
addColorToStr("\033[34m", item.name) + " "
31+
if Path(item).is_dir()
32+
else item.name + " "
33+
)
34+
print(output)
35+
elif isHiddenToShow and not isStatsToList:
36+
output = (
37+
addColorToStr("\033[34m", currentDir)
38+
+ " "
39+
+ addColorToStr("\033[34m", parentDir)
40+
+ " "
41+
)
42+
for item in content:
43+
output += (
44+
addColorToStr("\033[34m", item.name) + " "
45+
if Path(item).is_dir()
46+
else item.name + " "
47+
)
48+
print(output)
49+
elif not isHiddenToShow and isStatsToList:
50+
print("total " + str(calcTotalSpace(content)))
51+
printContentStats(content)
52+
elif isHiddenToShow and isStatsToList:
53+
print("total " + str(calcTotalSpace(content)))
54+
currentDirObj = Path(currentDir)
55+
parentDirObj = Path(parentDir)
56+
printContentStats([currentDirObj, parentDirObj])
57+
printContentStats(content)
58+
59+
60+
def calcTotalSpace(content):
61+
sum = 0
62+
for item in content:
63+
sum += item.stat().st_blocks
64+
return sum // 2
65+
66+
67+
def addColorToStr(color, str):
68+
reset = "\033[0m"
69+
return color + str + reset
70+
71+
72+
def sortDirContent(scanResult, isHiddenToShow):
73+
files = []
74+
directories = []
75+
hidden = []
76+
for item in scanResult:
77+
if item.name[0] == ".":
78+
hidden.append(item)
79+
elif os.path.isfile(item):
80+
files.append(item)
81+
elif os.path.isdir(item):
82+
directories.append(item)
83+
content = files + directories + hidden if isHiddenToShow else files + directories
84+
return content
85+
86+
87+
def readFilePermissions(path):
88+
permissions = os.stat(path)
89+
perm_str = stat.filemode(permissions.st_mode)
90+
return perm_str
91+
92+
93+
def printContentStats(content):
94+
for item in content:
95+
perm = stat.filemode(item.stat().st_mode)
96+
links = str(item.stat().st_nlink)
97+
owner = pwd.getpwuid(item.stat().st_uid).pw_name
98+
group = grp.getgrgid(item.stat().st_gid).gr_name
99+
size = str(item.stat().st_size).rjust(4)
100+
lastModTime = formatTime(item.stat().st_mtime)
101+
itemName = str(item) if str(item) == "." else item.name
102+
if item.is_dir():
103+
itemName = addColorToStr("\033[34m", itemName)
104+
print(
105+
perm
106+
+ " "
107+
+ links
108+
+ " "
109+
+ owner
110+
+ " "
111+
+ group
112+
+ " "
113+
+ size
114+
+ " "
115+
+ lastModTime
116+
+ " "
117+
+ itemName
118+
)
119+
120+
121+
def formatTime(timestamp):
122+
time = datetime.fromtimestamp(timestamp)
123+
month = time.strftime("%b") # str(time.month)
124+
day = str(time.day)
125+
hour = str(time.hour)
126+
minute = str(time.minute)
127+
return month + " " + day + " " + hour.rjust(2, "0") + ":" + minute.rjust(2, "0")
128+
129+
130+
if __name__ == "__main__":
131+
main()

0 commit comments

Comments
 (0)