MQTT Home Emulation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_interface.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import readline
  2. import sys
  3. import time
  4. class user_interface(dict):
  5. def __init__(self):
  6. super().__init__(self)
  7. self.add_command('quit', None)
  8. def run(self):
  9. readline.parse_and_bind("tab: complete")
  10. readline.set_completer(self.completer)
  11. print("\nEnter command: ")
  12. while (True):
  13. userfeedback = input('')
  14. command = userfeedback.split(' ')[0]
  15. if userfeedback == 'quit':
  16. break
  17. else:
  18. self.call_it(userfeedback)
  19. def call_it(self, userfeedback):
  20. args = userfeedback.split(" ")
  21. cmd = args.pop(0)
  22. cb = self.get(cmd)
  23. if callable(cb):
  24. cb(*args)
  25. else:
  26. print("Unknown command \"%s\"" % cmd)
  27. def add_command(self, name, callback):
  28. self[name] = callback
  29. def reduced_list(self, text):
  30. """
  31. Create reduced completation list
  32. """
  33. reduced_list = {}
  34. for cmd in self.keys():
  35. next_dot = cmd[len(text):].find('.')
  36. if next_dot >= 0:
  37. reduced_list[cmd[:len(text) + next_dot + 1]] = None
  38. else:
  39. reduced_list[cmd] = None
  40. return reduced_list.keys()
  41. def completer(self, text, state):
  42. """
  43. Our custom completer function
  44. """
  45. options = [x for x in self.reduced_list(text) if x.startswith(text)]
  46. return options[state]