Truthtable update: now installed into bin instead of an alias

This commit is contained in:
Tiago Garcia 2023-02-20 22:26:49 +00:00 committed by GitHub
commit 2dfa9dfbef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 55 deletions

View File

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

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 > Variables: a-z
> Operators: +, *, ~, (, ); You can't use ~ before () > 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: truthtable usage:
Prints this message.""") Prints this message.""")
return 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]" function = sys.argv[1]
validOperators = ['+', '*', '(', ')', '~', ' ', '0', '1']
for char in function: validVariables = r"[a-z]"
if not (re.match(validVariables, char) or char in validOperators): validOperators = ['+', '*', '(', ')', '~', ' ', '0', '1']
print("Invalid function, use 'truthtable usage' to see the valid characters.")
return
variables = re.findall(validVariables, function) for char in function:
variables = list(dict.fromkeys(variables)) if not (re.match(validVariables, char) or char in validOperators):
variables.sort() print("Invalid function, use 'truthtable usage' to see the valid characters.")
return
truthTable = getTable(variables, function) variables = re.findall(validVariables, function)
printTable(variables, truthTable) variables = list(dict.fromkeys(variables))
variables.sort()
truthTable = getTable(variables, function)
printTable(variables, truthTable)
def getTable(variables, function): def getTable(variables, function):