Skip to content

Commit

Permalink
Updated to version 2
Browse files Browse the repository at this point in the history
Version 2.0 aims simply to be a more polished version 1.0
  • Loading branch information
D1j1t0 authored May 4, 2023
1 parent cea0e5a commit 3e1d88c
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 138 deletions.
130 changes: 75 additions & 55 deletions boot.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,75 @@
#this runs your .omnirc file
exec(open('.omnirc.py').read())

#This section imports the dependent libraries
import sys
import subprocess
import os
import theme
import options
from rich.console import Console
from rich.prompt import Prompt as prompt
import shutil
from importlib import reload
from pathlib import Path
import time

#defines the login sequence
def login():
f = open("password.txt", "r")
lgin = (f.read())
usr = input("Please enter your Username: ")
usr = (usr + "\n")
pss = input("Now enter your Password: ")
usrpss = (usr + pss)
if lgin == usrpss:
print("Correct!")
else:
print("Incorrect, try again!")
login()

#This section shows the splash logo
logo = theme.logo
if options.dologo == True:
Console().print(logo)

#This section checks if the setup has already been completed
stpcmplt = (os.path.isfile("./password.txt"))

#This section does the main setup
if stpcmplt == (bool(False)):
usrnm = input("Please assign a Username: ")
psswrd = input("Please assign a Password: ")
usrnm = (usrnm + "\n")
lgn = (usrnm + psswrd)
print("Writing Password...")
f = open("password.txt", "w")
f.write(lgn)
f.close()
print("Setup complete")
exec(open('prompt.py').read())
else:
#calls the login sequence
if options.dologin == True:
login()
exec(open('prompt.py').read())
#!/usr/bin/env python3

import os

#Changes Current directory to the OmniShell
os.chdir(os.path.dirname(os.path.abspath(__file__)))

#Creates .omnirc.py if it doesn't exist
if os.path.isfile("./.omnirc.py") == False:
f = open(".omnirc.py", "w")
f.close()

#This runs your .omnirc file
exec(open('.omnirc.py').read())

#This section imports the dependent libraries
import sys
import subprocess
import os
import options
from rich.console import Console
from rich.prompt import Prompt as prompt
import shutil
from importlib import reload
from pathlib import Path
import time
from prompt_toolkit import prompt
from prompt_toolkit import PromptSession
from prompt_toolkit.history import FileHistory
from prompt_toolkit.completion import NestedCompleter
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.validation import Validator
from prompt_toolkit.styles import Style
import theme

#Defines the login sequence
def login():
f = open("password.txt", "r")
lgin = (f.read())
usr = input("Please enter your Username: ")
usr = (usr + "\n")
pss = input("Now enter your Password: ")
usrpss = (usr + pss)
if str(lgin) == str(usrpss):
print("Correct!")
else:
print("Incorrect, try again!")
login()

#This section shows the splash logo
logo = theme.logo
if options.dologo == True:
Console().print(logo)

#This section checks if the setup has already been completed
stpcmplt = (os.path.isfile("./password.txt"))

#This section does the main setup
if stpcmplt == False and options.dologin == True:
usrnm = input("Please assign a Username: ")
psswrd = input("Please assign a Password: ")
usrnm = (usrnm + "\n")
lgn = (usrnm + psswrd)
print("Writing Password...")
f = open("password.txt", "w")
f.write(lgn)
f.close()
print("Setup complete")
else:
#Calls the login sequence
if options.dologin == True:
login()

#Runs the prompt file
exec(open('prompt.py').read())
4 changes: 2 additions & 2 deletions options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#This file is made as an easy way for addons and other scripts to add user configurable variables/options

dologin = False #this feature is just a proof of concept at the moment and provides basically no extra security
dologin = False #This feature is just a proof of concept at the moment and provides basically no extra security
dologo = True
unixmode = False
unixmode = False
202 changes: 138 additions & 64 deletions prompt.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,139 @@
#loads theme and asks for input
cmdno = Console().input(theme.prompt)
reload(theme)
prompt = (theme.prompt)

#exits omnishell
if cmdno.lower() == ("exit"):
print("Bye, Bye")

#show current directory
elif cmdno == ("cdir"):
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
exec(open('prompt.py').read())

#does nothing, just prints new prompt
elif cmdno == (""):
exec(open('prompt.py').read())

#runs different files and scripts
else:
#checks which file types exist in ./scripts/
filechkpy = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".py"))
filechkbat = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".bat"))
filechkexe = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".exe"))
filechklnk = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".lnk"))

#prioritises python files
if filechkpy == (bool(True)):
cmd = (cmdno + ".py")
Console().print(prompt + "executing: " + cmd)
exec(open("./scripts/" + cmdno + "/" + cmd).read())
exec(open('prompt.py').read())

#then .bat files
elif filechkbat == (bool(True)) and options.unixmode == (bool(False)):
cmd = (cmdno + ".bat")
dir_path = os.path.dirname(os.path.realpath(__file__))
bat_path = os.path.join(dir_path, "scripts", cmdno, cmd)
Console().print(prompt + "executing: " + cmd)
subprocess.call([bat_path])
exec(open('prompt.py').read())

#next .exe files
elif filechkexe == (bool(True)) and options.unixmode == (bool(False)):
cmd = (cmdno + ".exe")
dir_path = os.path.dirname(os.path.realpath(__file__))
exe_path = os.path.join(dir_path, "scripts", cmdno, cmd)
Console().print(prompt + "executing: " + cmd)
subprocess.call(exe_path)
exec(open('prompt.py').read())

#last .lnk files (shortcuts)
elif filechklnk == (bool(True)) and options.unixmode == (bool(False)):
cmd = (cmdno + ".lnk")
dir_path = os.path.dirname(os.path.realpath(__file__))
lnk_path = os.path.join(dir_path, "scripts", cmdno, cmd)
Console().print(prompt + "executing: " + cmd)
os.startfile(lnk_path)
exec(open('prompt.py').read())

#if no file exists, it will then try and run it as a shell command
#Define the prompt function
def main(cmdno, theme, os):
try:
#Loads theme
reload(theme)
prompt = (theme.prompt)

#Exits omnishell
if cmdno.lower() == ("exit"):
print("Bye, Bye")
exit()

#Show current directory
elif cmdno == ("cdir"):
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)

#Does nothing, just prints new prompt
elif cmdno == (""):
#Don't worry this isn't meant to do anything
0 == 0

#Runs different files and scripts
else:
#Checks which file types exist in ./scripts/
filechkpy = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".py" ))
altchkpy = (os.path.isfile("./scripts/" + cmdno + "/main.py" ))
filechkbat = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".bat"))
filechkexe = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".exe"))
filechklnk = (os.path.isfile("./scripts/" + cmdno + "/" + cmdno + ".lnk"))

#Gets the number of matching files
filematches = 0
if filechkpy == True:
filematches = filematches + 1
if altchkpy == True:
filematches = filematches + 1
if filechkbat == True:
filematches = filematches + 1
if filechkexe == True:
filematches = filematches + 1
if filechklnk == True:
filematches = filematches + 1

#Sets file conflict mode
if filematches > 1:
print("Looks like there is a file conflict")
print(os.listdir("./scripts/" + cmdno + "/"))
conflict = input("choose a file: ")
else:
conflict = False

#Runs python files
if filechkpy == (bool(True)) or altchkpy == (bool(True)) or conflict == (cmdno + ".py") or conflict == "main.py" :
cmd = (cmdno + ".py")
if altchkpy == (bool(True)) and conflict != (cmdno + ".py"):
cmd = ("main.py")
exec(open("./scripts/" + cmdno + "/" + cmd).read())

#Runs .bat files
elif filechkbat == (bool(True)) or conflict == (cmdno + ".bat") and options.unixmode == (bool(False)):
cmd = (cmdno + ".bat")
dir_path = os.path.dirname(os.path.realpath(__file__))
bat_path = os.path.join(dir_path, "scripts", cmdno, cmd)
subprocess.call([bat_path])

#Runs .exe files
elif filechkexe == (bool(True)) or conflict == (cmdno + ".exe") and options.unixmode == (bool(False)):
cmd = (cmdno + ".exe")
dir_path = os.path.dirname(os.path.realpath(__file__))
exe_path = os.path.join(dir_path, "scripts", cmdno, cmd)
subprocess.call(exe_path)

#Runs .lnk files (shortcuts)
elif filechklnk == (bool(True)) or conflict == (cmdno + ".lnk") and options.unixmode == (bool(False)):
cmd = (cmdno + ".lnk")
dir_path = os.path.dirname(os.path.realpath(__file__))
lnk_path = os.path.join(dir_path, "scripts", cmdno, cmd)
os.startfile(lnk_path)

#If input begins with "p! " run as python code
if cmdno[0:3] == ("p! "):
exec(cmdno[3:])

#If input begins with "b! " run as bash
elif cmdno[0:3] == ("b! "):
os.system("/bin/bash -c " + cmdno[3:])

#If input begins with "z! " run as zsh
elif cmdno[0:3] == ("z! "):
os.system("/bin/zsh -c " + cmdno[3:])

#If input begins with "f! " run as fish
elif cmdno[0:3] == ("f! "):
os.system("/bin/fish -c " + cmdno[3:])

#If no file exists, it will then try and run it as a shell command
else:
os.system(cmdno)

#If there is an error it prints it
except Exception as errmess:
print("Error", errmess)

#Sets the autocomplete list to include: $PATH, native commands, and scripts
completionlist = []
path = os.environ['PATH']
path = path.split(':')
for i in range(len(path)):
completionlist = completionlist + os.listdir(path[i])
completionlist = completionlist + os.listdir("./scripts/") + ['exit', 'cdir', 'p!', 'b!', 'z!', 'f!']
#Turns the list into a dictionary so that the completetion only happens on the first word, to reduce lag
completiondict = {item: None for item in completionlist}
completer = NestedCompleter.from_nested_dict(completiondict)

#Defines the rules for what counts as a valid command
def valcmd(text):
#Spaces are valid
if not text or text.isspace():
return True

#If the first word is on the completion list then it is valid
else:
os.system(cmdno)
exec(open('prompt.py').read())
text = text.split(maxsplit=1)
return text[0] in completionlist

#Sets the error for if a command is not valid
cmdvalidator = Validator.from_callable(
valcmd,
error_message="Invalid Command",
move_cursor_to_end=True,
)

#Sets the history file
session = PromptSession(history=FileHistory("./.omnihistory"))

#The prompt
while True:
main(session.prompt(theme.prompt, style=theme.style, validator=cmdvalidator, validate_while_typing=True, completer=completer, auto_suggest=AutoSuggestFromHistory()), theme, os)
14 changes: 8 additions & 6 deletions scripts/themetool/themetool.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#prints all installed themes and asks you to select one
#Prints all installed themes and asks you to select one
print("\nPick a theme", os.listdir("./themes/"))
selecttheme = input("Type the name of a theme: ")
themechk = os.path.isdir("./themes/" + selecttheme + "/")
themepath = ("./themes/" + selecttheme)

#if the theme you selected is valid it imports it
#If the theme you selected is valid it imports it
if themechk == True:
sys.path.insert(0, themepath)
import theme
sys.modules.pop('theme')
import theme

#if the theme has an info variable it will print it
#If the theme has an info variable it will print it
if hasattr(theme, 'info'):
Console().print(theme.info)

#it then checks if the theme has a logo and prompt
#It then checks if the theme has a logo and prompt
print("Features:")
if hasattr(theme, 'logo'):
print(u'\u2714' + " Custom Logo")
Expand All @@ -26,19 +26,21 @@
else:
print(u'\u2717' + " Custom Prompt")

#then it asks if you would like to install the theme and if yes then it will install it
#Then it asks if you would like to install the theme and if yes then it will install it
switch = input("\nDo you want to switch to this theme? y/n: ")
if switch.lower() == ("y"):
if hasattr(theme, 'logo') and hasattr(theme, 'prompt'):
path = (os.path.dirname(os.path.realpath(__file__)))
oldtheme = os.path.join(path, 'theme.py')
applytheme = os.path.join(path, 'themes', selecttheme, 'theme.py')
shutil.copyfile(applytheme, oldtheme)
exit()
else:
print("\nError: Missing variables in theme file")
else:
sys.path.insert(0, "./")
reload(theme)
#if theme doesn't exist it will exit
exit()
#If theme doesn't exist it will exit
elif themechk == False:
print("Theme does not exist")
Loading

0 comments on commit 3e1d88c

Please sign in to comment.