refactor: Modularize pty.rs for better maintainability#1
Merged
Conversation
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.
cfcd2d2 to
3e41833
Compare
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
pty/client.rspty/socket.rspty/terminal.rspty/io_handler.rspty/session_switcher.rspty/spawn.rspty/mod.rs2. Main Handler Refactoring (main.rs → handlers/ directory)
Extracted all business logic from 621-line main.rs into organized handler modules:
main.rshandlers/session.rshandlers/info.rshandlers/mod.rsImpact
Before
pty.rs(1139 lines),main.rs(621 lines)After
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
Migration Notes
Future Improvements
spawn.rs(865 lines) into smaller modules