|
8 | 8 | mod flags;
|
9 | 9 |
|
10 | 10 | use clap::{Arg, ArgAction, ArgMatches, Command};
|
11 |
| -use nix::libc::{O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ, c_ushort}; |
| 11 | +use nix::libc::{c_ushort, O_NONBLOCK, TIOCGWINSZ, TIOCSWINSZ}; |
12 | 12 | use nix::sys::termios::{
|
13 |
| - ControlFlags, InputFlags, LocalFlags, OutputFlags, SpecialCharacterIndices, Termios, |
14 |
| - cfgetospeed, cfsetospeed, tcgetattr, tcsetattr, |
| 13 | + cfgetospeed, cfsetospeed, tcgetattr, tcsetattr, ControlFlags, InputFlags, LocalFlags, |
| 14 | + OutputFlags, SpecialCharacterIndices, Termios, |
15 | 15 | };
|
16 | 16 | use nix::{ioctl_read_bad, ioctl_write_ptr_bad};
|
17 | 17 | use std::fs::File;
|
18 |
| -use std::io::{self, Stdout, stdout}; |
| 18 | +use std::io::{self, stdout, Stdout}; |
19 | 19 | use std::ops::ControlFlow;
|
20 | 20 | use std::os::fd::{AsFd, BorrowedFd};
|
21 | 21 | use std::os::unix::fs::OpenOptionsExt;
|
@@ -211,32 +211,24 @@ fn stty(opts: &Options) -> UResult<()> {
|
211 | 211 | if let Some(settings) = &opts.settings {
|
212 | 212 | let mut settings_iter = settings.iter();
|
213 | 213 | while let Some(setting) = settings_iter.next() {
|
214 |
| - if let Some(char_index) = is_control_char(setting) { |
| 214 | + if let Some(char_index) = cc_to_index(setting) { |
215 | 215 | let Some(new_cc) = settings_iter.next() else {
|
216 | 216 | return Err(USimpleError::new(
|
217 | 217 | 1,
|
218 | 218 | format!("missing argument to '{setting}'"),
|
219 | 219 | ));
|
220 | 220 | };
|
221 |
| - match apply_char_mapping(&mut termios, char_index, new_cc) { |
222 |
| - Ok(_) => {} |
223 |
| - Err(e) => match e { |
224 |
| - ControlCharMappingError::IntOutOfRange => { |
225 |
| - return Err(USimpleError::new( |
226 |
| - 1, |
227 |
| - format!( |
228 |
| - "invalid integer argument: '{new_cc}': Numerical result out of range" |
229 |
| - ), |
230 |
| - )); |
231 |
| - } |
| 221 | + apply_char_mapping(&mut termios, char_index, new_cc).map_err(|e| { |
| 222 | + let message = match e { |
| 223 | + ControlCharMappingError::IntOutOfRange => format!( |
| 224 | + "invalid integer argument: '{new_cc}': Numerical result out of range" |
| 225 | + ), |
232 | 226 | ControlCharMappingError::MultipleChars => {
|
233 |
| - return Err(USimpleError::new( |
234 |
| - 1, |
235 |
| - format!("invalid integer argument: '{new_cc}'"), |
236 |
| - )); |
| 227 | + format!("invalid integer argument: '{new_cc}'") |
237 | 228 | }
|
238 |
| - }, |
239 |
| - } |
| 229 | + }; |
| 230 | + USimpleError::new(1, message) |
| 231 | + })?; |
240 | 232 | } else if let ControlFlow::Break(false) = apply_setting(&mut termios, setting) {
|
241 | 233 | return Err(USimpleError::new(
|
242 | 234 | 1,
|
@@ -307,7 +299,7 @@ fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
307 | 299 | Ok(())
|
308 | 300 | }
|
309 | 301 |
|
310 |
| -fn is_control_char(option: &str) -> Option<SpecialCharacterIndices> { |
| 302 | +fn cc_to_index(option: &str) -> Option<SpecialCharacterIndices> { |
311 | 303 | for cc in CONTROL_CHARS {
|
312 | 304 | if option == cc.0 {
|
313 | 305 | return Some(cc.1);
|
|
0 commit comments