Skip to content

Instantly share code, notes, and snippets.

@mpentler
Last active July 21, 2025 20:54
Show Gist options
  • Select an option

  • Save mpentler/0c851d9f632e33f93e1521439c353718 to your computer and use it in GitHub Desktop.

Select an option

Save mpentler/0c851d9f632e33f93e1521439c353718 to your computer and use it in GitHub Desktop.
CLI Video player file list panel
# Horrorbox v1.4
# File panel class, containing navigation and display functions
import os
import urwid
from ui.safe_listbox import SafeListBox
class FileListPanel:
def __init__(self, root_path, video_extensions):
self.root_path = root_path
self.current_path = root_path
self.video_extensions = video_extensions
self.walker = urwid.SimpleFocusListWalker([])
self.listbox = SafeListBox(self.walker)
self.populate()
def populate(self):
items = []
back_widget = urwid.SelectableIcon("Previous Folder", 0)
items.append(urwid.AttrMap(back_widget, None, focus_map='reversed'))
try:
entries = sorted(os.listdir(self.current_path))
entries = [e for e in entries if not e.startswith('.')]
except Exception as e:
entries = []
items.append(urwid.Text(f"(Error accessing {self.current_path}: {e})"))
for entry in entries:
full_path = os.path.join(self.current_path, entry)
if os.path.isdir(full_path):
if entry != "System Volume Information":
label = f"[{entry}/]"
else:
continue
elif entry.lower().endswith(self.video_extensions):
label = entry
else:
continue
if label != "":
widget = urwid.SelectableIcon(label, 0)
items.append(urwid.AttrMap(widget, None, focus_map='reversed'))
if len(items) == 1:
empty_widget = urwid.SelectableIcon("(Empty folder)", 0)
items.append(urwid.AttrMap(empty_widget, None, focus_map='reversed'))
self.walker.clear()
self.walker.extend(items)
if self.walker:
self.walker.set_focus(0)
def move_focus_up(self):
if self.walker:
pos = self.listbox.focus_position
if pos > 0:
self.walker.set_focus(pos - 1)
def move_focus_down(self):
if self.walker:
pos = self.listbox.focus_position
if pos < len(self.walker) - 1:
self.walker.set_focus(pos + 1)
def get_focused_label(self):
focus_widget, idx = self.walker.get_focus()
if focus_widget:
return focus_widget.original_widget.text.strip()
return None
def go_to_previous_folder(self):
if not os.path.samefile(self.current_path, self.root_path):
self.current_path = os.path.dirname(self.current_path)
self.populate()
return True
return False
def enter_folder(self, folder_name):
new_path = os.path.join(self.current_path, folder_name)
if os.path.isdir(new_path):
self.current_path = new_path
self.populate()
return True
return False
def get_current_path(self):
return self.current_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment