create a simple interactive prompt

This commit is contained in:
JJTech0130 2023-07-28 17:31:27 -04:00
parent 306bfd483f
commit ef9fd77bcb
No known key found for this signature in database
GPG key ID: 23C92EBCCF8F93D6
2 changed files with 38 additions and 1 deletions

View file

@ -50,7 +50,7 @@ class IncomingQueue:
with self.lock: with self.lock:
self.queue.append(item) self.queue.append(item)
def pop(self, index): def pop(self, index = -1):
with self.lock: with self.lock:
return self.queue.pop(index) return self.queue.pop(index)

37
demo.py
View file

@ -108,7 +108,44 @@ with open("config.json", "w") as f:
im = imessage.iMessageUser(conn, user) im = imessage.iMessageUser(conn, user)
# Create a thread to take user input
INPUT_QUEUE = apns.IncomingQueue()
def input_thread():
while True:
from prompt_toolkit import prompt
try:
msg = prompt('>> ')
except:
msg = 'quit'
INPUT_QUEUE.append(msg)
threading.Thread(target=input_thread, daemon=True).start()
while True: while True:
msg = im.receive() msg = im.receive()
if msg is not None: if msg is not None:
print(f"Got message {msg}") print(f"Got message {msg}")
if len(INPUT_QUEUE) > 0:
msg = INPUT_QUEUE.pop()
if msg == 'help' or msg == 'h':
print('help (h): show this message')
print('quit (q): quit')
print('send (s) [recipiant] [message]: send a message')
elif msg == 'quit' or msg == 'q':
break
elif msg.startswith('send') or msg.startswith('s'):
msg = msg.split(' ')
if len(msg) < 3:
print('send [recipiant] [message]')
else:
imsg = imessage.iMessage()
imsg.text = ' '.join(msg[2:])
imsg.participants = [msg[1], user.handles[0]]
imsg.sender = user.handles[0]
im.send(imsg)