Skip to content

Commit

Permalink
Python 3.9 compatibility patch - disable mouse wheel in Python 3.9 an…
Browse files Browse the repository at this point in the history
…d lower, replaced glob.glob() call
  • Loading branch information
cmang committed Dec 5, 2023
1 parent c15f3b6 commit fd876fd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Durdraw
_| |__ __ _____ __| |_____ _____ __ __ __
/ _ | | | __| _ | __| _ | | | |\
/_____|_____|__|__|_____|__|___\____|________| |
\_____________________________________________\| v 0.22.2
\_____________________________________________\| v 0.22.3


![Durdraw-0 20-demo](https://github.com/cmang/durdraw/assets/261501/ce539865-2e84-4423-92af-cd9ddeeb02ce)
Expand Down
1 change: 1 addition & 0 deletions durdraw/durdraw_appstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self): # User friendly defeaults
#self.drawChar = b'\xE2\x96\x88'
self.colorPickChar = chr(9608) # unicode block character, for displaying colors in color pickers
self.hasMouse = True # replace with equivalent curses.has_mouse()
self.hasMouseScroll = True # Disable for compatibility with older Python versions <3.10
self.helpMov = None
self.helpMov_2 = None
self.hasHelpFile = False
Expand Down
146 changes: 92 additions & 54 deletions durdraw/durdraw_ui_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def __init__(self, app):
self.gui = durgui.Gui(guiType="curses", window=self.stdscr)
curses.start_color() # Yeayuhhh
self.ansi = AnsiArtStuff(self.appState) # obj for misc ansi-related stuff
# Disable mouse scrolling for Python versions below 3.10, as they don't have
# curses.BUTTON5_*
if sys.version_info.major == 3:
if sys.version_info.minor < 10:
self.appState.hasMouseScroll = False
if self.appState.colorMode == "256":
if self.ansi.initColorPairs_256color():
self.appState.theme = self.appState.theme_256
Expand Down Expand Up @@ -711,13 +716,13 @@ def showScrollingHelpScreen(self):
elif c in [10, 13, curses.KEY_ENTER, 27, ord('q')]: # 27 == escape key
self.playingHelpScreen = False


if mouseState & curses.BUTTON4_PRESSED: # wheel up
if self.appState.topLine > 0:
self.appState.topLine = self.appState.topLine - 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
if self.appState.topLine + self.realmaxY - 3 < helpMov.sizeY - 1: # wtf?
self.appState.topLine += 1
if self.appState.hasMouseScroll:
if mouseState & curses.BUTTON4_PRESSED: # wheel up
if self.appState.topLine > 0:
self.appState.topLine = self.appState.topLine - 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
if self.appState.topLine + self.realmaxY - 3 < helpMov.sizeY - 1: # wtf?
self.appState.topLine += 1

new_time = time.time()
frame_delay = helpMov.currentFrame.delay
Expand Down Expand Up @@ -938,12 +943,14 @@ def startPlaying(self):
pass
realmaxY,realmaxX = self.realstdscr.getmaxyx()

if mouseState & curses.BUTTON4_PRESSED: # wheel up
if self.appState.topLine > 0:
self.appState.topLine = self.appState.topLine - 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
if self.appState.topLine + self.realmaxY - 3 < self.mov.sizeY - 1: # wtf?
self.appState.topLine += 1
if self.appState.hasMouseScroll:
if mouseState & curses.BUTTON4_PRESSED: # wheel up
if self.appState.topLine > 0:
self.appState.topLine = self.appState.topLine - 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
if self.appState.topLine + self.realmaxY - 3 < self.mov.sizeY - 1: # wtf?
self.appState.topLine += 1

elif c in [339, curses.KEY_PPAGE, ord('u'), ord('b')]: # page up, and vim keys
self.appState.topLine = self.appState.topLine - self.realmaxY + 3
if self.appState.topLine < 0:
Expand Down Expand Up @@ -1713,10 +1720,13 @@ def mainLoop(self):
if self.pushingToClip:
self.pushingToClip = False
self.stdscr.redrawwin()
if mouseState & curses.BUTTON4_PRESSED: # wheel up
self.move_cursor_up()
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
self.move_cursor_down()

if self.appState.hasMouseScroll:
if mouseState & curses.BUTTON4_PRESSED: # wheel up
self.move_cursor_up()
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
self.move_cursor_down()

elif self.appState.cursorMode == "Move": # select mode/move the cursor
self.xy[1] = mouseX + 1 # set cursor position
self.xy[0] = mouseY + self.appState.topLine
Expand Down Expand Up @@ -2223,19 +2233,21 @@ def showCharSetPicker(self):
# mask_all = True
# masks = ['*.*']
# update file list
elif mouseState & curses.BUTTON4_PRESSED: # wheel up
# scroll up
# if the item isn't at the top of teh screen, move it up
if selected_item_number > top_line:
selected_item_number -= 1
elif top_line > 0:
top_line -= 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
# scroll down
if selected_item_number < len(block_list) - 1:
selected_item_number += 1
if selected_item_number == len(block_list) - top_line:
top_line += 1

elif self.appState.hasMouseScroll:
if mouseState & curses.BUTTON4_PRESSED: # wheel up
# scroll up
# if the item isn't at the top of teh screen, move it up
if selected_item_number > top_line:
selected_item_number -= 1
elif top_line > 0:
top_line -= 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
# scroll down
if selected_item_number < len(block_list) - 1:
selected_item_number += 1
if selected_item_number == len(block_list) - top_line:
top_line += 1
else: # add to search string
search_string += chr(c)
search_string = search_string.lower() # case insensitive search
Expand Down Expand Up @@ -2268,7 +2280,17 @@ def openFilePicker(self):
else:
current_directory = os.getcwd()
#folders += sorted(glob.glob(f"{current_directory}/*/"))
folders += sorted(glob.glob("*/", root_dir=current_directory))

#folders += sorted(glob.glob("*/", root_dir=current_directory)) # python 3.10+
# python 3.9 compatible block instead:
folders += sorted(filter(os.path.isdir, glob.glob(os.path.join(current_directory, "*/"))))
# remove leading paths
new_folders = []
for path_string in folders:
new_folders.append(os.path.sep.join(path_string.split(os.path.sep)[-2:]))
folders = new_folders


matched_files = []
file_list = []
for file in os.listdir(current_directory):
Expand Down Expand Up @@ -2357,9 +2379,9 @@ def openFilePicker(self):
# display file info - format data
file_info = f"File: {filename}"
if file_sauce.sauce_found:
file_info += f", Title: {file_title}, Artist: {file_author}, Width: {file_width}, Height: {file_height}"
file_info = f"Title: {file_title}, Artist: {file_author}, Width: {file_width}, Height: {file_height}"
# show it on screen
self.addstr(realmaxY - 1, 0, f"filename: {file_info}")
self.addstr(realmaxY - 1, 0, f"{file_info}")

self.stdscr.refresh()
# Read keyboard input
Expand Down Expand Up @@ -2425,7 +2447,14 @@ def openFilePicker(self):
current_directory = current_directory[:-1]
# get file list
folders = ["../"]
folders += sorted(glob.glob("*/", root_dir=current_directory))
#folders += sorted(glob.glob("*/", root_dir=current_directory))
folders += sorted(filter(os.path.isdir, glob.glob(os.path.join(current_directory, "*/"))))
# remove leading paths
new_folders = []
for path_string in folders:
new_folders.append(os.path.sep.join(path_string.split(os.path.sep)[-2:]))
folders = new_folders

if mask_all:
masks = ['*.*']
else:
Expand Down Expand Up @@ -2487,7 +2516,14 @@ def openFilePicker(self):
current_directory = current_directory[:-1]
# get file list
folders = ["../"]
folders += glob.glob("*/", root_dir=current_directory)
#folders += glob.glob("*/", root_dir=current_directory)
folders += sorted(filter(os.path.isdir, glob.glob(os.path.join(current_directory, "*/"))))
# remove leading paths
new_folders = []
for path_string in folders:
new_folders.append(os.path.sep.join(path_string.split(os.path.sep)[-2:]))
folders = new_folders

if mask_all:
masks = ['*.*']
else:
Expand Down Expand Up @@ -2530,19 +2566,20 @@ def openFilePicker(self):
# reset ui
selected_item_number = 0
search_string = ""
elif mouseState & curses.BUTTON4_PRESSED: # wheel up
# scroll up
# if the item isn't at the top of teh screen, move it up
if selected_item_number > top_line:
selected_item_number -= 1
elif top_line > 0:
top_line -= 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
# scroll down
if selected_item_number < len(file_list) - 1:
selected_item_number += 1
if selected_item_number == len(file_list) - top_line:
top_line += 1
elif self.appState.hasMouseScroll:
if mouseState & curses.BUTTON4_PRESSED: # wheel up
# scroll up
# if the item isn't at the top of teh screen, move it up
if selected_item_number > top_line:
selected_item_number -= 1
elif top_line > 0:
top_line -= 1
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
# scroll down
if selected_item_number < len(file_list) - 1:
selected_item_number += 1
if selected_item_number == len(file_list) - top_line:
top_line += 1
else: # add to search string
search_string += chr(c)
for filename in file_list: # search list for search_string
Expand Down Expand Up @@ -3600,12 +3637,13 @@ def startSelecting(self, firstkey=None, mouse=False): # firstkey is the key th
pass
realmaxY,realmaxX = self.realstdscr.getmaxyx()
# enable mouse tracking only when the button is pressed
if mouseState & curses.BUTTON4_PRESSED: # wheel up
if mouseY < self.mov.sizeY and mouseX < self.mov.sizeX: # in edit area
self.move_cursor_up()
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
if mouseY < self.mov.sizeY and mouseX < self.mov.sizeX: # in edit area
self.move_cursor_down()
if self.appState.hasMouseScroll:
if mouseState & curses.BUTTON4_PRESSED: # wheel up
if mouseY < self.mov.sizeY and mouseX < self.mov.sizeX: # in edit area
self.move_cursor_up()
elif mouseState & curses.BUTTON5_PRESSED: # wheel down
if mouseY < self.mov.sizeY and mouseX < self.mov.sizeX: # in edit area
self.move_cursor_down()
elif mouseState == curses.BUTTON1_CLICKED or mouseState & curses.BUTTON_SHIFT:
if mouseY < self.mov.sizeY and mouseX < self.mov.sizeX: # in edit area
self.xy[1] = mouseX + 1 # set cursor position
Expand Down
2 changes: 1 addition & 1 deletion durdraw/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def undosize(size_s):
raise argparse.ArgumentTypeError("Undo size must be between 1 and 1000.")

def main():
DUR_VER = '0.22.2'
DUR_VER = '0.22.3'
DUR_FILE_VER = 7
DEBUG_MODE = False # debug = makes debug_write available, sends verbose notifications
durlogo = '''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='durdraw',
version='0.22.2',
version='0.22.3',
author='Sam Foster',
author_email='samfoster@gmail.com',
description='Animated Color ASCII and Unicode Art Editor',
Expand Down

0 comments on commit fd876fd

Please sign in to comment.