base.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Clipboard for command line interface.
  3. """
  4. from __future__ import unicode_literals
  5. from abc import ABCMeta, abstractmethod
  6. from six import with_metaclass
  7. import six
  8. from prompt_toolkit.selection import SelectionType
  9. __all__ = (
  10. 'Clipboard',
  11. 'ClipboardData',
  12. )
  13. class ClipboardData(object):
  14. """
  15. Text on the clipboard.
  16. :param text: string
  17. :param type: :class:`~prompt_toolkit.selection.SelectionType`
  18. """
  19. def __init__(self, text='', type=SelectionType.CHARACTERS):
  20. assert isinstance(text, six.string_types)
  21. assert type in (SelectionType.CHARACTERS, SelectionType.LINES, SelectionType.BLOCK)
  22. self.text = text
  23. self.type = type
  24. class Clipboard(with_metaclass(ABCMeta, object)):
  25. """
  26. Abstract baseclass for clipboards.
  27. (An implementation can be in memory, it can share the X11 or Windows
  28. keyboard, or can be persistent.)
  29. """
  30. @abstractmethod
  31. def set_data(self, data):
  32. """
  33. Set data to the clipboard.
  34. :param data: :class:`~.ClipboardData` instance.
  35. """
  36. def set_text(self, text): # Not abstract.
  37. """
  38. Shortcut for setting plain text on clipboard.
  39. """
  40. assert isinstance(text, six.string_types)
  41. self.set_data(ClipboardData(text))
  42. def rotate(self):
  43. """
  44. For Emacs mode, rotate the kill ring.
  45. """
  46. @abstractmethod
  47. def get_data(self):
  48. """
  49. Return clipboard data.
  50. """