demo_prompt.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. '''
  2. demo_prompt.py
  3. Provides a short demo on how to write prompts and input handlers in Python, as
  4. well as how the default prompt can be overwritten in Python.
  5. '''
  6. import mud, mudsys, socket
  7. def new_prompt(sock):
  8. '''this prompt will be used to override the default NM prompt'''
  9. sock.send_raw("\r\nnew prompt> ")
  10. def example_prompt(sock):
  11. '''used as an example of how to interact with the input handler stack'''
  12. sock.send_raw("\r\nexample prompt> ")
  13. def example_handler(sock, arg):
  14. '''used as an example of how to interact with the input handler stack'''
  15. if arg.upper() == 'Q':
  16. sock.pop_ih()
  17. else:
  18. sock.send("You typed " + arg + ". Q to quit.")
  19. def cmd_pyih(ch, cmd, arg):
  20. '''used as an example of how to interact with the input handler stack'''
  21. ch.socket.push_ih(example_handler, example_prompt)
  22. ch.send("New input handler set.")
  23. # add the command that allows us to interact with the input handler stack
  24. mudsys.add_cmd("pyih", None, cmd_pyih, "admin", False)
  25. # override our default prompt with a new one
  26. mudsys.show_prompt = new_prompt