2# +==== BEGIN tty_ov =================+
4# ..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5# .@...........................#@
6# @############################.@
7# @...........................@.@
8# @..#######################..@.@
9# @.#########################.@.@
10# @.##>_#####################.@.@
11# @.#########################.@.@
12# @.#########################.@.@
13# @.#########################.@.@
14# @.#########################.@.@
15# @..#######################..@.@
16# @...........................@.@
17# @..+----+______________.....@.@
18# @..+....+______________+....@.@
19# @..+----+...................@.@
20# @...........................@.#
21# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@#.
25# CREATION DATE: 11-02-2026
26# LAST Modified: 19:35:41 11-02-2026
28# A module that emulates a few core functionalities of a tty (see the inner help for a list of functions).
30# COPYRIGHT: (c) Henry Letellier
31# PURPOSE: This is the file that contains the implementation attempt of the ls command.
33# +==== END tty_ov =================+
37from typing
import Union
41from prettytable
import PrettyTable
46 The basics of the ls function
47 This code is borrowed from: https://github.com/connormason/lspython/tree/master
49 - fixed this code's errors
50 - made it cross-platform
51 - implemented it into the program
52 - adapted the code to fit into the shell's functionalities
55 def __init__(self, success: int = 0, error: int = 84) ->
None:
59 "white":
"\x1b[01;37m",
60 "gray":
"\x1b[00;37m",
61 "purple":
"\x1b[00;35m",
62 "cyan":
"\x1b[01;36m",
63 "green":
"\x1b[01;32m",
64 "red":
"\x1b[01;05;37;41m"
71 """ Check if the ncurse library is present in the system for the colour management """
72 if not hasattr(stream,
"isatty"):
74 if not stream.isatty():
79 """ Get the type of document in order to apply some colour """
84 if stat.S_ISDIR(mode):
87 elif stat.S_ISLNK(mode):
90 link = os.readlink(filename)
91 if not os.path.exists(filename):
93 elif stat.S_ISREG(mode):
94 if mode & (stat.S_IXGRP | stat.S_IXUSR | stat.S_IXOTH):
97 if filename[0] ==
'.':
102 mode = stat.S_IMODE(mode)
104 for who
in "USR",
"GRP",
"OTH":
105 for what
in "R",
"W",
"X":
106 if mode & getattr(stat,
"S_I" + what + who):
107 perms = perms + what.lower()
111 return (perms, color, link)
114 """ Get the info of the user """
117 user_info = pwd.getpwuid(uid)
118 return user_info.pw_name
123 """ Get the pid of the active groupe """
126 group_info = grp.getgrgid(gid)
127 return group_info.gr_name
132 """ List the files contained in the path """
146 locale.setlocale(locale.LC_ALL,
'')
147 files.sort(key=
lambda x: x.lower())
149 now = int(time.time())
150 recent = now - (6 * 30 * 24 * 60 * 60)
152 does_have_colors = self.
has_colors(sys.stdout)
154 for filename
in files:
156 stat_info = os.lstat(filename)
158 sys.stderr.write(f
"{filename}: No such file or directory\n")
159 global_status = self.
error
167 nlink = f
"{stat_info.st_nlink:4d}%4d"
170 size = f
"{stat_info.st_size:8d}"
172 time_stamp = stat_info.st_mtime
173 if (time_stamp < recent)
or (time_stamp > now):
174 time_fmt =
"%b %e %Y"
176 time_fmt =
"%b %e %R"
177 time_str = time.strftime(time_fmt, time.gmtime(time_stamp))
179 if self.
colors[color]
and does_have_colors:
180 filename_str = self.
colors[color] + filename +
"\x1b[00m"
182 filename_str = filename
185 filename_str +=
" -> "
200 table.align[
"Permissions"] =
'l'
201 table.align[
"# Links"] =
'r'
202 table.align[
"Owner"] =
'l'
203 table.align[
"Group"] =
'l'
204 table.align[
"Size"] =
'r'
205 table.align[
"Last Mod"] =
'l'
206 table.align[
"Name"] =
'l'
210 def ls(self, path: Union[str, list] =
"") -> int:
212 A basic loop manager to make this P.O.S POC a minimum functional and feel like the core of the real ls
215 if path
in (
"",
"."):
216 content = os.listdir(
".")
221 content = os.listdir(
".")
225 if isinstance(path, list):
228 print(f
"Content of: {item}")
230 if os.path.isdir(item):
231 content = os.listdir(item)
234 global_status = self.
error
236 if os.path.isdir(path):
237 files = os.listdir(path)
241 except Exception
as err:
242 print(f
"The pseudo Ls has crashed: {err}")
get_mode_info(self, mode, filename)
int list_files(self, list files)
str get_group_info(self, gid)
bool has_colors(self, stream)
None __init__(self, int success=0, int error=84)
int ls(self, Union[str, list] path="")
str get_user_info(self, uid)