A Python module for text display using no library external import. Tinkered mainly for 90+e (fx-CG50) and 35+e II, works on any version of MicroPython/Python. https://tinyurl.com/locate2py
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.

locate2.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. class Screen:
  2. def _check(self, x, o, name):
  3. if not isinstance(x, int):
  4. raise TypeError("{} is not an int object".format(name))
  5. elif x < 1:
  6. raise ValueError("{} is lower than 1".format(name))
  7. if o is None:
  8. o = 0
  9. elif x > o and o:
  10. raise ValueError("{} is greater than the height of this object: {} > {}".
  11. format(name, x, o))
  12. def __init__(self, width=21, height=6, patern=" ", copy=None):
  13. if isinstance(copy, Screen):
  14. self._width = copy._width
  15. self._height = copy._height
  16. self._mat = copy._mat
  17. else:
  18. self._check(width, None, "width")
  19. self._check(height, None, "height")
  20. if not isinstance(patern, str):
  21. raise TypeError("patern is not a string")
  22. elif len(patern) > 1:
  23. raise ValueError("patern is too long (length = {})".format(len(patern)))
  24. self._width = width
  25. self._height = height
  26. self.fill(patern)
  27. def locate(self, x, y, string):
  28. self._check(x, self._width, "x")
  29. self._check(y, self._height, "y")
  30. string = str(string)
  31. i = -1
  32. for char in string:
  33. if i + x < self._width:
  34. self._mat[y - 1][x + i] = char
  35. i += 1
  36. def locate_v(self, x, y, string):
  37. self._check(x, self._width, "x")
  38. self._check(y, self._height, "y")
  39. string = str(string)
  40. i = -1
  41. for char in string:
  42. if i + y < self._height:
  43. self._mat[y + i][x - 1] = char
  44. i += 1
  45. def fill(self, patern=" "):
  46. self._mat = [[patern[0] for i in range(self._width)] for i in range(self._height)]
  47. def refresh(self, ask_for_input=True, endl="\n"):
  48. to_print = str()
  49. for line in self._mat:
  50. for cell in line:
  51. to_print += cell
  52. to_print += "\n"
  53. to_print = to_print[:-1]
  54. print(to_print)
  55. if ask_for_input:
  56. return input("> ")
  57. else:
  58. print("", end=endl)
  59. return None
  60. def get_cell(self, x, y):
  61. self._check(x, self._width, "x")
  62. self._check(y, self._width, "y")
  63. return self._mat[y - 1][x - 1]
  64. def get_dim(self):
  65. return self._width, self._height
  66. def export(self):
  67. result = str()
  68. for line in self._mat:
  69. for cell in line:
  70. result += cell
  71. return result
  72. def load(self, string):
  73. if type(string) is not str:
  74. raise TypeError("string is not a string type")
  75. if len(string) != self._width * self._height:
  76. raise ValueError("string lenght isn't equal to {}".format(self._width * self._height))
  77. i = 0
  78. s = 0
  79. while i != self._height:
  80. self._mat[i] = list(string[s:s + self._width])
  81. i += 1
  82. s += self._width
  83. get_cell_content = get_cell # For retro-compability
  84. class Pad(Screen):
  85. def refresh(self, x=1, y=1, width=21, height=6, ask_for_input=True, endl="\n"):
  86. if width > self._width:
  87. width = self._width
  88. if height > self._height:
  89. height = self._height
  90. if x < 1:
  91. x = 1
  92. if y < 1:
  93. y = 1
  94. x -= 1
  95. y -= 1
  96. if x + width > self._width:
  97. x = self._width - width
  98. if y + height > self._height:
  99. y = self._height - height
  100. to_print = str()
  101. for dy in range(y, height+y):
  102. dy = dy
  103. for dx in range(x, width+x):
  104. print(dx, dy)
  105. to_print += self._mat[dy][dx]
  106. to_print += "\n"
  107. to_print = to_print[:-1]
  108. print(to_print)
  109. if ask_for_input:
  110. return input("> ")
  111. else:
  112. print("", end=endl)
  113. return None