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.

Parameters:
  • directory (str) – The root directory to start from (default: current directory)

  • exclude_dirs (list) – List of directories to exclude (default: None)

  • extensions (list) – List of file extensions to include (default: None)

Returns:

List of selected file paths

Return type:

list

FileSelector

class promptprep.tui.FileSelector(directory='.', exclude_dirs=None, extensions=None)[source]

Class for handling interactive file selection.

Parameters:
  • directory (str) – The root directory to start from (default: current directory)

  • exclude_dirs (list) – List of directories to exclude (default: None)

  • extensions (list) – List of file extensions to include (default: None)

run()[source]

Run the interactive selection interface.

Returns:

List of selected file paths

Return type:

list

scan_directory(directory)

Scan a directory for files and subdirectories.

Parameters:

directory (str) – Directory to scan

Returns:

Tuple of (files, subdirectories)

Return type:

tuple

should_include_file(file_path)

Determine if a file should be included based on extension filters.

Parameters:

file_path (str) – Path to the file

Returns:

Whether the file should be included

Return type:

bool

should_exclude_directory(directory)

Determine if a directory should be excluded.

Parameters:

directory (str) – Directory path

Returns:

Whether the directory should be excluded

Return type:

bool

build_file_tree()

Build the file tree data structure.

Returns:

Root node of the file tree

Return type:

TreeNode

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:

bool

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_hidden_files()

Toggle showing hidden files.

Returns:

None

get_selected_files()

Get the list of selected file paths.

Returns:

List of selected file paths

Return type:

list

TreeNode

class promptprep.tui.TreeNode(path, is_dir=False, parent=None)

Class representing a node in the file tree.

Parameters:
  • path (str) – Path to the file or directory

  • is_dir (bool) – Whether the node is a directory (default: False)

  • parent (TreeNode) – Parent node (default: None)

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.

add_child(child)

Add a child node.

Parameters:

child (TreeNode) – Child node to add

Returns:

None

toggle_selection()

Toggle selection of the node.

Returns:

None

toggle_expansion()

Toggle expansion of the directory.

Returns:

None

get_display_name()

Get the display name of the node.

Returns:

Display name

Return type:

str

get_indent_level()

Get the indent level of the node.

Returns:

Indent level

Return type:

int

is_hidden()

Check if the node is a hidden file or directory.

Returns:

Whether the node is hidden

Return type:

bool

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.

Parameters:
  • row (int) – Row position

  • col (int) – Column position

Returns:

None

get_terminal_size()

Get the size of the terminal.

Returns:

Tuple of (rows, columns)

Return type:

tuple

read_key()

Read a key press from the terminal.

Returns:

Key code

Return type:

str

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()