diff --git a/v4/Controller.py b/v4/Controller.py index c0310a1..1c99ef9 100644 --- a/v4/Controller.py +++ b/v4/Controller.py @@ -29,6 +29,12 @@ class Controller: def registerCallback(self, cb): self.callbacks.append(cb) + def registerFieldCallback(self, fieldName, cb): + def execCB(c): + if c.recentField == fieldName: + cb(c) + self.callbacks.append(execCB) + def registerFunction(self, f): self.functions.append(f) diff --git a/v4/cli.py b/v4/cli.py index 72b585d..c71790e 100644 --- a/v4/cli.py +++ b/v4/cli.py @@ -1,6 +1,31 @@ from memory_Context import * from llm import * +# Exit +# +# Conditions: +# 1. `e`, `exit`, `q`, or `quit` was entered +@llm_by_value +def cli_exit( + c: memory_Context +) -> memory_Context: + if ( + c.recentField == "input" and + ( + c.input == "e" or + c.input == "exit" or + c.input == "q" or + c.input == "quit" + ) + ): + c.exit = True + c.recentField = "exit" + return c + #} + c.recentField = "none" + return c +#} + # Greet the user # # Conditions: diff --git a/v4/cli_test.py b/v4/cli_test.py index 52aa0dd..c3e1541 100644 --- a/v4/cli_test.py +++ b/v4/cli_test.py @@ -2,6 +2,62 @@ from cli import * from memory import * from memory_Context import * +def cli_test_exit_e( +) -> str: + c = memory_createContext() + c.input = "e" + c.recentField = "input" + c = cli_exit(c) + if ( + c.recentField == "exit" + ): + return "OK: cli_exit_e" + #} + return "ERR: cli_exit_e" +#} + +def cli_test_exit_exit( +) -> str: + c = memory_createContext() + c.input = "exit" + c.recentField = "input" + c = cli_exit(c) + if ( + c.recentField == "exit" + ): + return "OK: cli_exit_exit" + #} + return "ERR: cli_exit_e" +#} + +def cli_test_exit_q( +) -> str: + c = memory_createContext() + c.input = "q" + c.recentField = "input" + c = cli_exit(c) + if ( + c.recentField == "exit" + ): + return "OK: cli_exit_q" + #} + return "ERR: cli_exit_q" +#} + +def cli_test_exit_quit( +) -> str: + c = memory_createContext() + c.input = "quit" + c.recentField = "input" + c = cli_exit(c) + if ( + c.recentField == "exit" + ): + return "OK: cli_exit_quit" + #} + return "ERR: cli_exit_quit" +#} + def cli_test_greetUser( ) -> str: c = memory_createContext() @@ -16,6 +72,34 @@ def cli_test_greetUser( return "ERR: cli_greetUser" #} +def cli_test_showHelp_h( +) -> str: + c = memory_createContext() + c.input = "h" + c.recentField = "input" + c = cli_showHelp(c) + if ( + c.recentField == "outputHelp" + ): + return "OK: cli_showHelp_h" + #} + return "ERR: cli_showHelp_h" +#} + +def cli_test_showHelp_help( +) -> str: + c = memory_createContext() + c.input = "help" + c.recentField = "input" + c = cli_showHelp(c) + if ( + c.recentField == "outputHelp" + ): + return "OK: cli_showHelp_help" + #} + return "ERR: cli_showHelp_help" +#} + #def cli_test_selectItem( #) -> str: # c = cli_createContext() @@ -61,28 +145,3 @@ def cli_test_greetUser( # return "ERR: cli_shouldReportInvalidItemSelection" ##} # -#def cli_test_showHelp_h( -#) -> str: -# c = cli_createContext() -# c.input = "h" -# c = cli_showHelp(c) -# if ( -# c.recentField == "outputHelp" -# ): -# return "OK: cli_showHelp_h" -# #} -# return "ERR: cli_showHelp_h" -##} -# -#def cli_test_showHelp_help( -#) -> str: -# c = cli_createContext() -# c.input = "help" -# c = cli_showHelp(c) -# if ( -# c.recentField == "outputHelp" -# ): -# return "OK: cli_showHelp_help" -# #} -# return "ERR: cli_showHelp_help" -##} diff --git a/v4/main.py b/v4/main.py index 88aa5de..c156f6b 100644 --- a/v4/main.py +++ b/v4/main.py @@ -2,8 +2,7 @@ from cli import * from cli_test import * from memory_test import * from Controller import * -#from shell import * -#import sys +import sys print(memory_test_deselectMismatchedItems()) print(memory_test_deselectMismatchedItems_itemTwice()) @@ -14,24 +13,29 @@ print(memory_test_selectItem_1x()) print(memory_test_selectItem_2x()) print(memory_test_selectItem_3x()) +print(cli_test_exit_e()) +print(cli_test_exit_exit()) +print(cli_test_exit_q()) +print(cli_test_exit_quit()) print(cli_test_greetUser()) +print(cli_test_showHelp_h()) +print(cli_test_showHelp_help()) #print(cli_test_selectItem()) #print(cli_test_shouldPromptSelection()) ##print(cli_test_shouldReportIvalidItemSelection_outOfBoundsMin()) -#print(cli_test_showHelp_h()) -#print(cli_test_showHelp_help()) ctrl = Controller(memory_createContext()) +ctrl.registerFunction(cli_exit) ctrl.registerFunction(cli_greetUser) ctrl.registerFunction(cli_showHelp) -ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'")) def printOutput(c): if c.recentField.startswith("output"): print(getattr(c, c.recentField)) ctrl.registerCallback(printOutput) - +#ctrl.registerCallback(lambda c: print(f"ИГР App.dbg ctx: '{c}'")) +ctrl.registerFieldCallback("exit", lambda c: sys.exit(0)) # #c = shell_createContext() @@ -46,43 +50,6 @@ ctrl.registerCallback(printOutput) ctrl.set("didLaunch", True) -#for line in sys.stdin: -# c.input = line.rstrip() -# c = shell_processInput(c) -# if c.exit: -# break -# print(c.output) - - - - - - -import threading -import time - -class KeyboardThread(threading.Thread): - - def __init__(self, input_cbk = None, name='keyboard-input-thread'): - self.input_cbk = input_cbk - super(KeyboardThread, self).__init__(name=name, daemon=True) - self.start() - - def run(self): - while True: - self.input_cbk(input()) #waits to get input + Return - -showcounter = 0 #something to demonstrate the change - -def my_callback(inp): - #evaluate the keyboard input - print('You Entered:', inp, ' Counter is at:', showcounter) - -#start the Keyboard thread -kthread = KeyboardThread(my_callback) - -while True: - #the normal program executes without blocking. here just counting up - showcounter += 1 - time.sleep(0.5) - +for line in sys.stdin: + ln = line.rstrip() + ctrl.set("input", ln)