Skip to content

Commit

Permalink
Add paste exception warn
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Oct 21, 2024
1 parent aa8ef9c commit bcb1939
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pygame_menu/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ def __str__(self) -> str:
patch = property(lambda self: self[2])


vernum = Version(4, 4, 5)
vernum = Version(4, 4, 6)
ver = str(vernum)
rev = ''
42 changes: 28 additions & 14 deletions pygame_menu/widgets/widget/textinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
]

import math
import os
import platform
import pygame
import pygame_menu
import pygame_menu.controls as ctrl
Expand Down Expand Up @@ -503,7 +505,7 @@ def _render_selection_box(self, force: bool = False) -> None:
if not self._selection_enabled:
return

if force or self._selection_active and (
elif force or self._selection_active and (
self._last_selection_render[0] != self._selection_box[0] or
self._last_selection_render[1] != self._selection_box[1]
):
Expand Down Expand Up @@ -768,7 +770,7 @@ def _get_input_string(self, add_ellipsis: bool = True) -> str:
:param add_ellipsis: Adds ellipsis text
:return: String
"""
string = self._get_input_string_filtered()
string: str = self._get_input_string_filtered()
if self._maxwidth != 0 and len(string) > self._maxwidth:
text = string[self._renderbox[0]:self._renderbox[1]]
if add_ellipsis:
Expand Down Expand Up @@ -826,7 +828,7 @@ def _update_renderbox(
elif len_string <= self._maxwidth:
if right < 0 and self._renderbox[2] == len_string: # If del at the end of string
return
if left < 0 and self._renderbox[2] == 0: # If cursor is at beginning
elif left < 0 and self._renderbox[2] == 0: # If cursor is at beginning
return
self._renderbox[0] = 0 # To catch unexpected errors
if addition: # left/right are ignored
Expand Down Expand Up @@ -1034,6 +1036,7 @@ def _check_mouse_collide_input(self, pos: Tuple2NumberType) -> bool:
topleft, _ = rect.topleft
self._update_cursor_mouse(mouse_x - topleft)
return True # Prevents double click
return False

def _check_touch_collide_input(self, pos: Tuple2NumberType) -> bool:
"""
Expand All @@ -1053,6 +1056,7 @@ def _check_touch_collide_input(self, pos: Tuple2NumberType) -> bool:
topleft, _ = rect.topleft
self._update_cursor_mouse(touch_x - topleft)
return True # Prevents double click
return False

def set_value(self, text: Any) -> None:
"""
Expand Down Expand Up @@ -1107,7 +1111,7 @@ def _check_input_type(self, string: Any) -> bool:
"""
if string == '': # Empty is valid
return True
if self._input_type == INPUT_TEXT:
elif self._input_type == INPUT_TEXT:
return True

conv = None
Expand Down Expand Up @@ -1230,7 +1234,7 @@ def _copy(self) -> bool:
"""
if self._block_copy_paste: # Prevents multiple executions of event
return False
if self._password: # Password cannot be copied
elif self._password: # Password cannot be copied
return False

try:
Expand Down Expand Up @@ -1288,16 +1292,26 @@ def _paste(self) -> bool:

# Paste text in cursor
try:
text = paste()
except PyperclipException:
text: str = paste()
except PyperclipException as e:
if self._verbose:
paste_warn: str = 'Pasting from clipboard failed. '
if os.getenv('XDG_SESSION_TYPE') == 'wayland':
paste_warn += 'Please install package "wl-clipboard"'
elif platform.system() == 'Linux':
paste_warn += 'On Linux, install "xclip" or "xsel"'
else:
paste_warn += (f'An unrecognized error happened ({e}). '
f'Please create a new issue on {pygame_menu.__url_bug_tracker__}')
warn(paste_warn)
return False

text = text.strip()
for i in ['\n', '\r']:
text = text.replace(i, '')

# Delete escape chars
escapes = ''.join([chr(char) for char in range(1, 32)])
escapes: str = ''.join([chr(char) for char in range(1, 32)])
text = text.translate(escapes)
if text == '':
return False
Expand All @@ -1313,17 +1327,17 @@ def _paste(self) -> bool:
return False

# Cut string (if limit does exist)
text_end = len(text)
text_end: int = len(text)
if self._maxchar != 0:
char_limit = self._maxchar - len(self._input_string)
text_end = min(char_limit, text_end)
if text_end <= 0: # If there's no more space, returns
self._sound.play_event_error()
return False

new_string = self._input_string[0:self._cursor_position] \
+ text[0:text_end] \
+ self._input_string[self._cursor_position:len(self._input_string)]
new_string: str = self._input_string[0:self._cursor_position] \
+ text[0:text_end] \
+ self._input_string[self._cursor_position:len(self._input_string)]

# If string is valid
if self._check_input_type(new_string):
Expand Down Expand Up @@ -1367,7 +1381,7 @@ def _undo(self) -> bool:
"""
if self._history_index == 0: # There's no back history
return False
if self._history_index == len(self._history): # If the actual is the last
elif self._history_index == len(self._history): # If the actual is the last
self._history_index -= 1
self._history_index = max(0, self._history_index - 1)
self._update_from_history()
Expand All @@ -1390,7 +1404,7 @@ def _remove_selection(self) -> None:
Remove text from selection.
"""
removed = self._selection_box[1] - self._selection_box[0]
left = False
left: bool = False
if self._selection_box[0] == self._cursor_position:
left = True

Expand Down

0 comments on commit bcb1939

Please sign in to comment.