TUI Module
The tui module provides the Terminal User Interface for interactive file selection in promptprep.
Module Overview
The TUI module provides functionality for:
Displaying a terminal-based file browser
Allowing users to navigate through directories
Selecting and deselecting files
Handling keyboard input
Managing the visual representation of the file tree
Key Classes and Functions
run_interactive_selection
- promptprep.tui.run_interactive_selection(directory='.', exclude_dirs=None, extensions=None)
Run the interactive file selection interface.
FileSelector
- class promptprep.tui.FileSelector(directory='.', exclude_dirs=None, extensions=None)[source]
Class for handling interactive file selection.
- Parameters:
- run()[source]
Run the interactive selection interface.
- Returns:
List of selected file paths
- Return type:
- scan_directory(directory)
Scan a directory for files and subdirectories.
- should_include_file(file_path)
Determine if a file should be included based on extension filters.
- should_exclude_directory(directory)
Determine if a directory should be excluded.
- build_file_tree()
Build the file tree data structure.
- Returns:
Root node of the file tree
- Return type:
- render_tree()
Render the file tree to the terminal.
- Returns:
None
- handle_input()
Handle keyboard input.
- Returns:
Whether to continue running the interface
- Return type:
- move_cursor_up()
Move the cursor up in the file tree.
- Returns:
None
- move_cursor_down()
Move the cursor down in the file tree.
- Returns:
None
- toggle_current_node()
Toggle selection of the current node.
- Returns:
None
- toggle_directory_expansion()
Toggle expansion of the current directory.
- Returns:
None
- select_all_in_current_directory()
Select all files in the current directory.
- Returns:
None
- select_all()
Select all files in all directories.
- Returns:
None
- deselect_all_in_current_directory()
Deselect all files in the current directory.
- Returns:
None
- deselect_all()
Deselect all files in all directories.
- Returns:
None
Toggle showing hidden files.
- Returns:
None
TreeNode
- class promptprep.tui.TreeNode(path, is_dir=False, parent=None)
Class representing a node in the file tree.
- Parameters:
- path
Path to the file or directory.
- is_dir
Whether the node is a directory.
- parent
Parent node.
- children
List of child nodes.
- is_expanded
Whether the directory is expanded.
- is_selected
Whether the file is selected.
- toggle_selection()
Toggle selection of the node.
- Returns:
None
- toggle_expansion()
Toggle expansion of the directory.
- Returns:
None
Check if the node is a hidden file or directory.
- Returns:
Whether the node is hidden
- Return type:
TerminalUI
- class promptprep.tui.TerminalUI
Class for handling terminal UI operations.
- init_terminal()
Initialize the terminal for UI operations.
- Returns:
None
- restore_terminal()
Restore the terminal to its original state.
- Returns:
None
- clear_screen()
Clear the terminal screen.
- Returns:
None
- move_cursor(row, col)
Move the cursor to a specific position.
- get_terminal_size()
Get the size of the terminal.
- Returns:
Tuple of (rows, columns)
- Return type:
- hide_cursor()
Hide the terminal cursor.
- Returns:
None
- show_cursor()
Show the terminal cursor.
- Returns:
None
Usage Examples
Basic Usage
from promptprep.tui import run_interactive_selection
# Run interactive selection
selected_files = run_interactive_selection(
directory='./my_project',
exclude_dirs=['node_modules', 'venv'],
extensions=['.py', '.js']
)
# Print selected files
print("Selected files:")
for file in selected_files:
print(f"- {file}")
Custom File Selector
from promptprep.tui import FileSelector
# Create a custom file selector
selector = FileSelector(
directory='./my_project',
exclude_dirs=['node_modules', 'venv'],
extensions=['.py', '.js']
)
# Run the selector
selected_files = selector.run()
# Process selected files
print(f"Selected {len(selected_files)} files")
Working with Tree Nodes
from promptprep.tui import TreeNode
# Create a file tree manually
root = TreeNode('project', is_dir=True)
src = TreeNode('project/src', is_dir=True, parent=root)
main = TreeNode('project/src/main.py', parent=src)
utils = TreeNode('project/src/utils.py', parent=src)
# Add children
root.add_child(src)
src.add_child(main)
src.add_child(utils)
# Expand directories
root.toggle_expansion() # Expand root
src.toggle_expansion() # Expand src
# Select files
main.toggle_selection() # Select main.py
# Print tree structure
def print_tree(node, indent=0):
prefix = ' ' * indent
expanded = '[+]' if node.is_dir and node.is_expanded else '[>]' if node.is_dir else ' '
selected = '[x]' if node.is_selected else '[ ]'
print(f"{prefix}{expanded} {selected} {node.get_display_name()}")
if node.is_dir and node.is_expanded:
for child in node.children:
print_tree(child, indent + 1)
print_tree(root)
Custom Terminal UI
from promptprep.tui import TerminalUI
# Create a terminal UI
ui = TerminalUI()
try:
# Initialize terminal
ui.init_terminal()
ui.hide_cursor()
# Clear screen
ui.clear_screen()
# Display some text
ui.move_cursor(0, 0)
print("Welcome to the file selector!")
ui.move_cursor(2, 0)
print("Press any key to continue...")
# Wait for key press
key = ui.read_key()
# Display pressed key
ui.move_cursor(4, 0)
print(f"You pressed: {key}")
# Get terminal size
rows, cols = ui.get_terminal_size()
ui.move_cursor(6, 0)
print(f"Terminal size: {rows} rows x {cols} columns")
# Wait for another key press
ui.move_cursor(8, 0)
print("Press any key to exit...")
ui.read_key()
finally:
# Restore terminal
ui.show_cursor()
ui.restore_terminal()