-
Notifications
You must be signed in to change notification settings - Fork 1
/
ls.py
277 lines (230 loc) · 7.82 KB
/
ls.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/python3
import os
from sys import argv
from pathlib import Path
import random
class Extensions:
def __init__(self):
# Change this to custom if you want
# to specify a color for each filetype
# if it is custom, it will use self.filetype_color dictionary
# to the colorscheme
self.colortype = 'rainbow'
self.end = "\n"
self.show_hidden = False
self.only_hidden = False
self.only_dirs = False
self.excluded = []
self.files = os.listdir()
# self.colortype = 'custom'
self.extensions = {
'folder': '',
'.config': '',
'iso': '',
'mp3': '',
'flac': '',
'm4a': '',
'wav': '',
'conf': '',
'lua': '',
'html': '',
'htm': '',
'css': '',
'js': '',
'json': '',
'rs': '',
'c': '',
'h': '',
'ppt': '',
'docx': '',
'doc': '',
'xls': '',
'pdf': '',
'gz': '',
'xz': '',
'bz2': '',
'bz': '',
'zip': '',
'rar': '',
'zst': '',
'tar': '',
'tgz': '',
'cpp': '',
'hpp': '',
'cs': '',
'png': '',
'jpg': '',
'jpeg': '',
'bmp': '',
'tif': '',
'tiff': '',
'git': '',
'svg': '',
'mp4': '',
'avi': '',
'mkv': '',
'vim': '',
'.vim': '',
'ts': 'ﯤ',
'deb': '',
'md': '',
'java': '',
'py': '',
'pyc': '',
'php': '',
'ui': '类',
'exe': '',
'appimage': '',
'sh': '',
'sql': '',
'db': '',
'sqlite': '',
'sqlite3': '',
'default': ''
}
# You can change the filetype color here
# or you can also add more customized filetypes
# the colors are in Red, Green and Blue format
# filetype = (r, g, b)
self.filetype_color = dict(
directory = (230, 255, 230),
file = (255, 230, 230),
# Examples for different filetypes
# python = (252, 244, 3),
# lua = (0, 0, 255)
)
def generate_rgb_color(self) -> int:
return random.randint(180, 255)
def return_filetype(self, file):
# File extension, you can play with this to
# add custom colors to different filetypes.
# Remember that the value that this function
# will return, will be used to get the key from
# the dictionary self.filetype_color
extension = Path(file).suffix.lower().replace('.', '')
if os.path.isdir(file):
return 'directory'
# Different filetype examples
'''
elif extension == 'py':
return 'python'
elif extension == 'lua':
return 'lua'
'''
# If it is none of them, it will return
# the default value
return 'file'
"""
This class will return every single file in a directory in colors
"""
class Ls(Extensions):
def __init__(self, args):
self.args = args
self.path = os.getcwd()
super().__init__()
# Available arguments
for arg in args:
if arg in ['-sh', '-a', '--show-hidden']:
self.show_hidden = True
elif arg in ['-oh', '-hi', '--only-hidden']:
self.only_hidden = True
elif arg in ['-od', '-d', '--only-dirs']:
self.only_dirs = True
elif arg in ['-ex', '--exclude']:
self.excluded.append(args[args.index(arg)+1])
elif os.path.isdir(arg):
self.files = os.listdir(arg)
self.path = arg
elif arg in ['-h', '--help']:
self.help()
exit()
def help(self):
print('''
Thanks for using my script!
Available options
-sh -a --show-hidden\t\tShows the hidden files
-oh -hi --only-hidden\t\tShows ONLY the hidden files
-ex --exclude \t\tExcludes a file extension, for example:
\t\t\tls -ex 'py'
\t\t\tThis will exclude all the python files
-od -d --only-dirs \t\tThis will display only the directories
''')
def show_files(self):
"""
This function will iterate through every single file with the specified flags
"""
new_files = []
for file in self.files:
if Path(file).suffix.lower().replace('.', '') in self.excluded:
pass
elif self.show_hidden:
new_files.append(file)
elif self.only_dirs:
if os.path.isdir(file):
new_files.append(file)
elif self.only_hidden:
if self.is_hidden(file):
new_files.append(file)
else:
if not self.is_hidden(file):
new_files.append(file)
self.files = sorted(new_files)
def is_hidden(self, file):
"""
Checks if a file is hidden, by checking if it has a dot in the start of its name
"""
return file.startswith('.')
def is_config_file(self, file):
"""
Checks if its a configuration file
"""
for config_ext in ['conf', 'rc']:
if self.is_hidden(file) or file.startswith(config_ext) or file.endswith(config_ext):
return True
return False
def file_icon(self, file: str):
"""
Returns the relationed icon with the filename
"""
try:
file_extension = Path(file).suffix.lower().replace('.', '')
if file == '.config':
return self.extensions['.config']
elif os.path.isdir(f"{self.path}/{file}"):
return self.extensions['folder']
elif self.is_config_file(file):
return self.extensions['conf']
elif file_extension in self.extensions:
return self.extensions[file_extension]
else:
return self.extensions['default']
except Exception as e:
print(type(e).__name__, e)
exit()
def get_color_escape(self, r, g, b):
return f'\033[38;2;{r};{g};{b}m'
def print_(self, file):
"""
this will print the file with its icon
"""
colorscheme = (self.generate_rgb_color() for _ in range(3))
if self.colortype == 'custom':
filetype = self.return_filetype(file)
colorscheme = self.filetype_color[filetype]
print(self.get_color_escape(*colorscheme),
self.file_icon(file), '\033[0m',
file, end=self.end)
else:
print(
self.get_color_escape(*colorscheme),
self.file_icon(file), '\033[0m',
file, end=self.end)
def run_(self):
"""
Prints every single file in a list
"""
for file_index in range(len(self.files)):
self.print_(self.files[file_index])
ls = Ls(argv)
ls.show_files()
ls.run_()