The XeFM Navigation System handles directory traversal, cursor positioning, and navigation state management. This document covers the implementation details of navigation behaviors and optimizations.
When navigating from a child directory to its parent directory using the Backspace key, the system implements intelligent cursor positioning to improve user experience.
xefm/app.pyelif key == curses.KEY_BACKSPACE or key == KEY_BACKSPACE_2 or key == KEY_BACKSPACE_1: # Backspace - go to parent directory
if current_pane['path'] != current_pane['path'].parent:
try:
# Save current cursor position before changing directory
self.save_cursor_position(current_pane)
# Remember the child directory name we're leaving
child_directory_name = current_pane['path'].name
current_pane['path'] = current_pane['path'].parent
current_pane['selected_index'] = 0
current_pane['scroll_offset'] = 0
current_pane['selected_files'].clear() # Clear selections when changing directory
self.refresh_files(current_pane)
# Try to set cursor to the child directory we just came from
cursor_set = False
for i, file_path in enumerate(current_pane['files']):
if file_path.name == child_directory_name and file_path.is_dir():
current_pane['selected_index'] = i
# Adjust scroll offset to keep selection visible
self.adjust_scroll_for_selection(current_pane)
cursor_set = True
break
# If we couldn't find the child directory, try to restore cursor position from history
if not cursor_set and not self.restore_cursor_position(current_pane):
# If no history found, default to first item
current_pane['selected_index'] = 0
current_pane['scroll_offset'] = 0
self.needs_full_redraw = True
except PermissionError:
self.show_error("Permission denied")
self.needs_full_redraw = True
child_directory_name)The implementation includes robust fallback mechanisms:
If the child directory is deleted while the user is in it, the fallback mechanisms ensure graceful handling:
When already at the root directory, the condition current_pane['path'] != current_pane['path'].parent prevents unnecessary processing.
Permission errors during navigation are caught and displayed to the user with appropriate error messages.
The adjust_scroll_for_selection() method ensures that when the cursor is positioned on the child directory, it remains visible even if it’s outside the current scroll view.
save_cursor_position(current_pane) stores cursor state before navigationrestore_cursor_position(current_pane) retrieves saved positionsMAX_HISTORY_ENTRIES configuration for memory managementadjust_scroll_for_selection(current_pane) keeps selections visiblecurrent_pane['path'].name for cross-platform directory name extractioncurrent_pane['path'].parent for reliable parent directory accessfile_path.is_dir() for consistent directory identificationThe navigation system includes comprehensive unit tests in test/test_parent_directory_navigation.py:
MAX_HISTORY_ENTRIES for fallback cursor history