Truthtable update: now installed into bin instead of an alias

This commit is contained in:
TiagoRG 2023-02-20 22:21:57 +00:00
parent 5bcadd9282
commit 228c1c8222
4 changed files with 40 additions and 55 deletions

View File

@ -1,15 +1,17 @@
# Truth Table Builder
### Script to build the truth table for a given function.
---
## Installing
#### 1. Head over to [Release](https://github.com/TiagoRG/uaveiro-leci/releases/tag/ttb) and download the python file.
#### 2. Save it on any directory and then open the directory.
#### 1. Head over to [Release](https://github.com/TiagoRG/uaveiro-leci/releases/tag/ttb) and download the zip folder.
#### 2. Extract it into any location and then open that location.
#### 3. Inside the directory, right click and choose "Open in terminal"
![- Unable to load image -](https://github.com/TiagoRG/uaveiro-leci/blob/master/tools/truthtable/openInTerminal.png)
#### 4. Run the following command:
`python3 truthtable.py bash`
#### 5. Relaunch the terminal and you will have the `truthtable` command
#### 4. Run the following commands:
1. Make sure the setup.sh is executable: `sudo chmod +x setup.sh`<br>
2. Install the script: `./setup.sh install`
#### 5. Done! You can now use the tool by running `truthtable` in terminal.
---
#### Use `truthtable usage` to know better how to use this tool.
#### If you want to change the location of the `truthtable.py` file, move it to another directory, open that directory in terminal and run
`python3 truthtable.py bash --reset`
#### If you want to remove the tool, run `./setup.sh uninstall`, delete setup.sh and it will be removed.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 0 B

18
tools/truthtable/setup.sh Executable file
View File

@ -0,0 +1,18 @@
if [ "$1" = "install" ]; then
echo "Installing truthtable..."
sudo mv truthtable.py /usr/bin/
sudo touch /usr/bin/truthtable
sudo chmod o+w /usr/bin/truthtable
echo "python3 /usr/bin/truthtable.py \"\$1\"" > /usr/bin/truthtable
sudo chmod o-w /usr/bin/truthtable
sudo chmod +x /usr/bin/truthtable
echo "Done. You can now delete this file or keep it to uninstall truthtable."
echo "You can now run truthtable by typing 'truthtable' in the terminal."
elif [ "$1" = "uninstall" ]; then
echo "Uninstalling truthtable..."
sudo rm /usr/bin/truthtable.py
sudo rm /usr/bin/truthtable
echo "Done."
else
echo "Usage: ./setup.sh [install|uninstall]"
fi

View File

@ -14,61 +14,26 @@ truthtable '<function>':
> Variables: a-z
> Operators: +, *, ~, (, ); You can't use ~ before ()
truthtable bash:
Adds the command truthtable to bash, so you can use it from anywhere.
truthtable bash --reset:
Resets the command truthtable in bash in case it's broken, so you can use it from anywhere.
truthtable usage:
Prints this message.""")
return
if sys.argv[1] == "bash":
username = os.getlogin()
if len(sys.argv) == 2:
with open(f'/home/{username}/.bashrc', 'r') as bashrc:
for line in bashrc:
if line.startswith('alias truthtable='):
print('Command already exists, try using, after opening a new terminal, truthtable \'<function>\'')
return
os.system(f'echo "\n\nalias truthtable=\'python3 {pathlib.Path(__file__).parent.absolute()}/truthtable.py\'\n" >> \'/home/{username}/.bashrc\'')
print('Command added to bash, try using truthtable <function>.\nIf it doesn\'t work, do truthtable bash --reset.')
elif sys.argv[2] == '--reset':
with open(f'/home/{username}/.bashrc', 'r') as bashrc:
lines = bashrc.readlines()
linesDict = {}
index = 1
for line in lines:
linesDict[index] = line
index += 1
for key in linesDict:
if linesDict[key].startswith('alias truthtable='):
del linesDict[key]
break
string = ''
for key in linesDict:
string += linesDict[key]
string += f'\n\nalias truthtable=\'python3 {pathlib.Path(__file__).parent.absolute()}/truthtable.py\'\n'
with open(f'/home/{username}/.bashrc', 'w') as bashrc:
bashrc.write(string)
print('Command readded to bash, try using, after opening a new terminal, truthtable <function>.')
else:
function = sys.argv[1]
validVariables = r"[a-z]"
validOperators = ['+', '*', '(', ')', '~', ' ', '0', '1']
function = sys.argv[1]
for char in function:
if not (re.match(validVariables, char) or char in validOperators):
print("Invalid function, use 'truthtable usage' to see the valid characters.")
return
validVariables = r"[a-z]"
validOperators = ['+', '*', '(', ')', '~', ' ', '0', '1']
variables = re.findall(validVariables, function)
variables = list(dict.fromkeys(variables))
variables.sort()
for char in function:
if not (re.match(validVariables, char) or char in validOperators):
print("Invalid function, use 'truthtable usage' to see the valid characters.")
return
truthTable = getTable(variables, function)
printTable(variables, truthTable)
variables = re.findall(validVariables, function)
variables = list(dict.fromkeys(variables))
variables.sort()
truthTable = getTable(variables, function)
printTable(variables, truthTable)
def getTable(variables, function):