Skip to content

Conversation

@K-NRS
Copy link
Member

@K-NRS K-NRS commented Sep 13, 2025

Summary

Complete refactoring of monolithic code files into organized, maintainable modules for better code organization and faster development.

Changes

1. PTY Module Refactoring (pty.rs → pty/ directory)

Broke down the 1139-line monolithic pty.rs into 7 focused modules:

Module Lines Responsibility
pty/client.rs 38 Client connection and terminal size management
pty/socket.rs 61 Unix socket operations and command parsing
pty/terminal.rs 119 Terminal state save/restore and raw mode operations
pty/io_handler.rs 172 PTY I/O operations and scrollback buffer management
pty/session_switcher.rs 155 Interactive session switching logic (~s command)
pty/spawn.rs 865 Core PTY process spawning and session attachment
pty/mod.rs 14 Module exports maintaining backward compatibility

2. Main Handler Refactoring (main.rs → handlers/ directory)

Extracted all business logic from 621-line main.rs into organized handler modules:

Module Lines Responsibility
main.rs 125 CLI parsing and command routing only
handlers/session.rs 229 Session management (new, attach, kill, rename, clean)
handlers/info.rs 313 Information display (list, info, history)
handlers/mod.rs 17 Module exports

Impact

Before

  • 2 large monolithic files: pty.rs (1139 lines), main.rs (621 lines)
  • Difficult to navigate and maintain
  • Slow incremental compilation
  • High cognitive load

After

  • 11 focused modules with clear responsibilities
  • Average module size: ~180 lines
  • 80% reduction in main.rs file size
  • Clean separation of concerns

Benefits

Better Code Organization - Related functionality grouped together
Improved Maintainability - Easy to locate and modify specific features
Faster Compilation - Better incremental compilation with smaller modules
Enhanced Testability - Components can be tested in isolation
Reduced Complexity - Each module has a single, clear responsibility
Easier Onboarding - New developers can understand the codebase faster

Testing

  • All existing unit tests pass
  • All integration tests pass
  • Manual testing of all commands (new, attach, kill, list, info, history)
  • Backward compatibility verified - no breaking changes
  • Build completes without errors

Migration Notes

  • No breaking changes - all existing code continues to work
  • Module structure follows Rust best practices
  • All public APIs remain unchanged

Future Improvements

  • Further decompose spawn.rs (865 lines) into smaller modules
  • Add module-specific error types for better error handling
  • Consider async I/O migration with tokio for better scalability
  • Add more granular unit tests for individual modules

Session Management Enhancements:
- Add session name support to all commands (attach, kill, info, rename, history)
- Implement partial name matching for session identification
- Fix Ctrl+D to only detach current client, not terminate session
- Fix detach process hanging after showing detach message
- Add terminal resize support for multi-client sessions
- Implement session switching within attached sessions (~s command)

UI/UX Improvements:
- Complete rewrite of interactive picker using ratatui for better TUI
- Add current session highlighting in both list and interactive views
- Implement sleek, modern UI design with minimal borders
- Show comprehensive session info (PID, uptime, created time, working dir, client count)
- Add special time format for sessions older than 24h (e.g., "2d, 14:30")
- Fix cursor shape preservation after client detachment

Technical Improvements:
- Add ClientInfo struct for per-client terminal tracking
- Implement environment variables (NDS_SESSION_ID, NDS_SESSION_NAME) for session identification
- Add process tree walking for current session detection
- Fix terminal mode switching for user input during session switching
- Implement non-blocking I/O for proper input handling
- Add tcflush() for input buffer clearing during detach

Documentation:
- Update README with new environment variables
- Document session name support in all relevant commands
- Add keyboard shortcuts documentation
- Update examples with name-based operations
Breaking down the monolithic 1139-line pty.rs file into focused modules:

Module Structure:
- pty/client.rs (38 lines) - Client connection and terminal size management
- pty/socket.rs (61 lines) - Unix socket operations and command parsing
- pty/terminal.rs (119 lines) - Terminal state save/restore and raw mode
- pty/io_handler.rs (172 lines) - PTY I/O operations and scrollback buffer
- pty/session_switcher.rs (155 lines) - Interactive session switching logic
- pty/spawn.rs (865 lines) - Core PTY process spawning and attachment
- pty/mod.rs (14 lines) - Module exports for backward compatibility

Benefits:
- Improved code organization and maintainability
- Better separation of concerns
- Faster incremental compilation
- Easier testing of individual components
- Reduced cognitive load for developers

All tests pass and backward compatibility is maintained.
@K-NRS K-NRS force-pushed the refactor/modularize-pty branch from cfcd2d2 to 3e41833 Compare September 13, 2025 09:18
@K-NRS K-NRS closed this Sep 13, 2025
@K-NRS K-NRS reopened this Sep 13, 2025
Reduced main.rs from 621 to 125 lines by extracting handlers:

- handlers/session.rs (229 lines) - Session management operations
  - handle_new_session, handle_attach_session, handle_kill_sessions
  - handle_rename_session, handle_clean_sessions

- handlers/info.rs (313 lines) - Information display operations
  - handle_list_sessions, handle_session_info, handle_session_history

Benefits:
- 80% reduction in main.rs file size
- Clear separation of concerns
- Better code organization
- Easier testing and maintenance
- Removed duplicate handler functions from main.rs (already in handlers module)
- Removed old pty.rs file (replaced by modular structure in pty/ directory)
- Fixed all rustfmt warnings for CI compliance
- Consistent code formatting across the codebase
- No functional changes, only formatting
@K-NRS K-NRS merged commit a0b4c1b into master Sep 13, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants