@@ -539,13 +539,17 @@ fn print_special_setting(setting: &PrintSetting, fd: i32) -> nix::Result<()> {
539539 Ok ( ( ) )
540540}
541541
542+ /// Handles line wrapping for stty output to fit within terminal width
542543struct WrappedPrinter {
543544 width : usize ,
544545 current : usize ,
545546 first_in_line : bool ,
546547}
547548
548549impl WrappedPrinter {
550+ /// Creates a new printer with the specified terminal width.
551+ /// If term_size is None (typically when output is piped), falls back to
552+ /// the COLUMNS environment variable or a default width of 80 columns.
549553 fn new ( term_size : Option < & TermSize > ) -> Self {
550554 let columns = match term_size {
551555 Some ( term_size) => term_size. columns ,
@@ -864,29 +868,29 @@ fn print_in_save_format(termios: &Termios) {
864868 println ! ( ) ;
865869}
866870
871+ /// Gets terminal size using the tiocgwinsz ioctl system call.
872+ /// This queries the kernel for the current terminal window dimensions.
873+ fn get_terminal_size ( fd : RawFd ) -> nix:: Result < TermSize > {
874+ let mut term_size = TermSize :: default ( ) ;
875+ unsafe { tiocgwinsz ( fd, & raw mut term_size) } . map ( |_| term_size)
876+ }
877+
867878fn print_settings ( termios : & Termios , opts : & Options ) -> nix:: Result < ( ) > {
868879 if opts. save {
869880 print_in_save_format ( termios) ;
870881 } else {
871882 let device_fd = opts. file . as_raw_fd ( ) ;
872- let term_size = {
873- let mut term_size = TermSize :: default ( ) ;
874- let term_size = unsafe { tiocgwinsz ( device_fd, & raw mut term_size) } . map ( |_| term_size) ;
875- if opts. all {
876- Some ( term_size?)
877- } else {
878- term_size. ok ( )
879- }
883+ let term_size = if opts. all {
884+ Some ( get_terminal_size ( device_fd) ?)
885+ } else {
886+ get_terminal_size ( device_fd) . ok ( )
880887 } ;
881888
882889 let stdout_fd = stdout ( ) . as_raw_fd ( ) ;
883890 let window_size = if device_fd == stdout_fd {
884891 & term_size
885892 } else {
886- let mut term_size = TermSize :: default ( ) ;
887- & unsafe { tiocgwinsz ( stdout_fd, & raw mut term_size) }
888- . map ( |_| term_size)
889- . ok ( )
893+ & get_terminal_size ( stdout_fd) . ok ( )
890894 } ;
891895
892896 print_terminal_size ( termios, opts, window_size. as_ref ( ) , term_size. as_ref ( ) ) ?;
0 commit comments