diff --git a/tools/truthtable/README.md b/tools/truthtable/README.md index 8e5c06d..413ad61 100644 --- a/tools/truthtable/README.md +++ b/tools/truthtable/README.md @@ -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`
+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. diff --git a/tools/truthtable/openInTerminal.png b/tools/truthtable/openInTerminal.png index 9928b5f..e69de29 100644 Binary files a/tools/truthtable/openInTerminal.png and b/tools/truthtable/openInTerminal.png differ diff --git a/tools/truthtable/setup.sh b/tools/truthtable/setup.sh new file mode 100755 index 0000000..43abec4 --- /dev/null +++ b/tools/truthtable/setup.sh @@ -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 \ No newline at end of file diff --git a/tools/truthtable/truthtable.py b/tools/truthtable/truthtable.py index 9fde1b4..f723e2e 100644 --- a/tools/truthtable/truthtable.py +++ b/tools/truthtable/truthtable.py @@ -14,61 +14,26 @@ truthtable '': > 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 \'\'') - 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 .\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 .') - 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):