Skip to content

Commit

Permalink
adapte the program to TUI
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDorschner committed Apr 6, 2024
1 parent f3dce46 commit c23844d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
7 changes: 3 additions & 4 deletions Part 2 - Pake/client/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .utils import Login, SignUp
from .tui import main

SignUp()
print("entering login phase :")
shared_key = Login()
if __name__ == "__main__":
main()
56 changes: 43 additions & 13 deletions Part 2 - Pake/client/tui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytermgui as ptg
from .utils import SignUp, Login

CONFIG = """
config:
Expand Down Expand Up @@ -37,7 +38,22 @@ def showDialog(manager, title, content):
manager.focus(modal)


def display_result(manager, result):
content_lines = []

title = f"{result.get('operation')} {'SUCCESSFUL' if result.get('success') else 'FAILED'}"

for key, value in result.items():
content_lines.append(f"[bold]{key}: [/]{value}")

content = "\n".join(content_lines)

showDialog(manager, title, content)


def login(manager):
for window in manager._windows:
window.close()
fields = {
"Username": ptg.InputField("Username: ", placeholder="Username"),
"Password": ptg.InputField(
Expand All @@ -46,17 +62,18 @@ def login(manager):
}

def handle_login(*_):
username = fields["Username"].value
password = fields["Password"].value
try:
username = fields["Username"].value
password = fields["Password"].value

# Code for login
if not username or not password:
raise ValueError("Username and password cannot be empty.")

if username == "user" and password == "pass":
showDialog(
manager, "Login Success", "C: ValueXYZ\nEnvelope Contents: {...}\n"
)
else:
showDialog(manager, "Login Failed", "Invalid username or password.")
data = Login(username, password)

display_result(manager, data)
except Exception as e:
showDialog(manager, "Login Failed", "error: " + str(e) + " occurred.")

login_window = ptg.Window(
"[bold]Login[/bold]",
Expand All @@ -66,12 +83,14 @@ def handle_login(*_):
box=ptg.boxes.SINGLE,
width=40,
)

manager.layout.add_slot("Body")
manager.add(login_window.center())
manager.focus(login_window)


def signup(manager):
for window in manager._windows:
window.close()
fields = {
"Username": ptg.InputField("Username: ", placeholder="Username"),
"Password": ptg.InputField(
Expand All @@ -80,9 +99,18 @@ def signup(manager):
}

def handle_signup(*_):
# Code for signup
try:
username = fields["Username"].value
password = fields["Password"].value

showDialog(manager, "Signup Success", "Shared Key: KeyXYZ")
data = SignUp()

if not username or not password:
raise ValueError("Username and password cannot be empty.")

display_result(manager, data)
except Exception as e:
showDialog(manager, "Signup Failed", "error: " + str(e) + " occurred.")

signup_window = ptg.Window(
"[bold]Signup[/bold]",
Expand All @@ -92,7 +120,7 @@ def handle_signup(*_):
box=ptg.boxes.SINGLE,
width=40,
)

manager.layout.add_slot("Body")
manager.add(signup_window.center())
manager.focus(signup_window)

Expand All @@ -101,6 +129,8 @@ def main_menu(manager):
for window in manager._windows:
window.close()

manager.layout.add_slot("Body")

menu = ptg.Window(
ptg.Button("Login", onclick=lambda *_: login(manager)),
ptg.Button("Sign Up", onclick=lambda *_: signup(manager)),
Expand Down

0 comments on commit c23844d

Please sign in to comment.