Skip to content

Commit d86c6ec

Browse files
authored
Add files via upload
1 parent 47b933c commit d86c6ec

File tree

11 files changed

+243
-0
lines changed

11 files changed

+243
-0
lines changed

CoreShell/bin/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
import importlib
3+
4+
#This file ensures that all files from the bin folder are correctly identified by main.
5+
__all__ = []
6+
current_dir = os.path.dirname(__file__)
7+
8+
for filename in os.listdir(current_dir):
9+
if filename.endswith(".py") and filename != "__init__.py":
10+
module_name = filename[:-3]
11+
__all__.append(module_name)
12+
module = importlib.import_module(f"bin.{module_name}")
13+
globals().update(vars(module))

CoreShell/bin/cl.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from bin.mode import mode
2+
import os
3+
4+
def cl(args=[]):
5+
osName = mode()
6+
7+
if osName == "dos":
8+
clear = os.system("cls")
9+
else:
10+
clear = os.system("clear")
11+
12+
return clear
13+
14+
def man(args=[]):
15+
output = "Clears the screen. Has no positional arguments, providing them will have no effect."
16+
return output
17+

CoreShell/bin/cwd.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
def cwd(args=[]):
4+
cwd = os.getcwd()
5+
return cwd
6+
7+
def man(args=[]):
8+
output = "Prints the current working directory to the screen. This command supports no positional arguments. Providing them will have no effect."
9+
return output

CoreShell/bin/echo.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
def echo(args=[]):
4+
combined = ""
5+
for item in args:
6+
combined += item
7+
8+
output = combined
9+
10+
return combined
11+
12+
def man(args=[]):
13+
output = "Returns any text typed where positional arguments would usually be present as raw text.\nSometimes useful for piping."
14+
return output

CoreShell/bin/kern.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import subprocess
2+
3+
def kern(args=[]):
4+
kernCmd = subprocess.run(args, capture_output=True, text=True)
5+
output = kernCmd.stdout.strip("\n")
6+
7+
return output
8+
9+
def man(args=[]):
10+
output = "Runs commands through your system's terminal.\nYou can perform most basic and some advanced actions through here, but some access is restricted by the subprocess module's limitations.\nA good way to get things done that CoreShell can't do."
11+
return output
12+

CoreShell/bin/ls.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
from bin.sz import sz
3+
import colours
4+
5+
def ls(args=[]):
6+
validArgs = ["--pretty","--bare"]
7+
dirCont = os.listdir()
8+
#args --pretty, --bare, --sep - [SEP]
9+
if len(args) == 0:
10+
response=""
11+
for item in dirCont:
12+
if item != dirCont[-1]:
13+
if os.path.isfile(item):
14+
response+=item+"\n"
15+
else:
16+
response+="\x1B[1m"+item+colours.reset()+"\n"
17+
else:
18+
response+=item
19+
dirCont = response
20+
21+
if len(args) > 0:
22+
if "--pretty" in args:
23+
response = ""
24+
response+="------\n"
25+
26+
for item in dirCont:
27+
if os.path.isfile(item):
28+
response += "🗎 - " + item + " " + str(sz([item,"--unit"])) + "\n"
29+
else:
30+
response += "🗀 - \x1B[1m" + item + colours.reset() + " " + str(sz([item,"--unit"])) + "\n"
31+
32+
response+="------"
33+
dirCont = response
34+
elif "--bare" in args:
35+
pass
36+
elif args[0] not in validArgs:
37+
dirCont = "ls: invalid positional arguments were given."
38+
elif len(args) > 1:
39+
dirCont = "ls: too many positional arguments were provided."
40+
41+
42+
43+
return dirCont
44+
45+
def man(args=[]):
46+
output = "Lists all of the files and directories in the current working directory.\nUse arguments such as --pretty to print the list in a more human readable format, or --bare to print the list in a more managable format for piping."
47+
return output
48+

CoreShell/bin/man.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import importlib
2+
3+
def man(args=[]):
4+
if not args:
5+
output = "man: no command specified.\nUsage: man [cmdname]"
6+
7+
if args[0] != "man":
8+
try:
9+
command_func = globals().get(args[0])
10+
11+
if not command_func:
12+
command_module = importlib.import_module(f"bin.{args[0]}")
13+
command_func = getattr(command_module, "man", None)
14+
15+
if not command_func:
16+
output = f"man: command '{args[0]}' does not exist or lacks a manual."
17+
18+
output = command_func()
19+
except ImportError:
20+
output = f"man: command '{args[0]}' not found in 'bin'."
21+
except Exception as e:
22+
output = f"man: an error occurred while accessing the manual for '{args[0]}': {e}"
23+
else:
24+
output = "Provides you with a manual for a command.\nUsage: man [cmdname]"
25+
26+
return output

CoreShell/bin/mode.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
#Returns the type of operating system shell is being run within.
3+
def mode(args=[]):
4+
if str(os.name).lower() in ["nt","dos"]:
5+
mode = "dos"
6+
elif str(os.name).lower() in ["posix","linux"]:
7+
mode = "linux"
8+
else:
9+
mode = "name"
10+
11+
return mode
12+
13+
def man(args=[]):
14+
output = "Provides you with the name of your host OS. This command supports no positional arguments. Providing them will have no effect."
15+
return output

CoreShell/bin/sz.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
3+
def sz(args=[]):
4+
sizeByte = 0
5+
6+
if len(args) > 0:
7+
if os.path.isfile(args[0]):
8+
sizeByte = os.path.getsize(args[0])
9+
else:
10+
for path, dirs, files in os.walk(args[0]):
11+
try:
12+
for f in files:
13+
fp = os.path.join(path, f)
14+
sizeByte += os.path.getsize(fp)
15+
except:
16+
pass
17+
18+
if "--unit" in args:
19+
if sizeByte < 1000:
20+
size = str(sizeByte) + "B"
21+
elif sizeByte >= 1000 and sizeByte < 1000000:
22+
size = str(round(sizeByte / 1000, 2)) + "KB"
23+
elif sizeByte >= 1000000 and sizeByte < 1000000000:
24+
size = str(round(sizeByte / 1000000, 2)) + "MB"
25+
elif sizeByte >= 1000000000 and sizeByte < 1000000000000:
26+
size = str(round(sizeByte / 1000000000, 2)) + "GB"
27+
elif sizeByte >= 1000000000000 and sizeByte < 1000000000000000:
28+
size = str(round(sizeByte / 1000000000000, 2)) + "TB"
29+
30+
sizeFormat = size
31+
else:
32+
sizeFormat = sizeByte
33+
else:
34+
sizeFormat = "sz: no such file or directory was given."
35+
36+
if sizeFormat == 0 or sizeFormat == "0" or sizeFormat == "0B":
37+
if os.path.exists(args[0]):
38+
if "--unit" in args:
39+
sizeFormat = "0B"
40+
else:
41+
sizeFormat = 0
42+
else:
43+
sizeFormat = f"sz: '{args[0]}' - the file or directory does not exist."
44+
45+
return sizeFormat
46+
47+
def man(args=[]):
48+
output = "Provides you with the size of a given file/directory.\nTakes positional arguments:\n--unit: prints the size in a more human readable fashion with the unit.\nBy default this command provides you the size of the file in bytes."
49+
return output

CoreShell/bin/time.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from datetime import datetime
2+
3+
def time(args=[]):
4+
currentTime = datetime.now()
5+
#args: --date, --bare, --pretty, --12
6+
validArgs = ["--date","--dateus","--bare","--pretty"]
7+
if len(args) == 1:
8+
if "--date" in args:
9+
output = currentTime.strftime("%d/%m/%y")
10+
if "--dateus" in args:
11+
output = currentTime.strftime("%x")
12+
if "--bare" in args:
13+
output = currentTime
14+
if "--pretty" in args:
15+
output = currentTime.strftime("%H:%M:%S, %d %b %Y")
16+
elif len(args) > 1:
17+
output = "time: too many positional arguments were provided."
18+
else:
19+
output = currentTime.strftime("%H:%M:%S")
20+
21+
if len(args) > 0 and args[0] not in validArgs:
22+
output = f"time: positional argument '{args[0]}' not recognised."
23+
24+
return output
25+
26+
def man(args=[]):
27+
output = "Prints the time/date to the screen.\nSupports positional arguments:\n--date: provides the date.\n--dateus: provides the date in US format.\n--bare: provides the date in the standard way that datetime provides the data.\n--pretty: prints the time and the date at the same time in a human readable format."
28+
return output
29+

CoreShell/bin/whoami.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import getpass
3+
4+
def whoami(args=[]):
5+
user = getpass.getuser()
6+
7+
return user
8+
9+
def man(args=[]):
10+
output = "Provides you with your username in the host OS. This command has no positional arguments. Providing them will have no effect."
11+
return output

0 commit comments

Comments
 (0)