mirror of
https://github.com/uutils/coreutils.git
synced 2025-07-07 21:45:01 +00:00
stty: add option to print terminal size
This commit is contained in:
parent
93ac655936
commit
10f8d77560
2 changed files with 35 additions and 6 deletions
|
@ -108,15 +108,20 @@ enum ControlCharMappingError {
|
|||
MultipleChars,
|
||||
}
|
||||
|
||||
enum SpecialSettings {
|
||||
enum SpecialSetting {
|
||||
Rows(u16),
|
||||
Cols(u16),
|
||||
}
|
||||
|
||||
enum PrintSetting {
|
||||
Size,
|
||||
}
|
||||
|
||||
enum ArgOptions<'a> {
|
||||
Flags(AllFlags<'a>),
|
||||
Mapping((SpecialCharacterIndices, u8)),
|
||||
Special(SpecialSettings),
|
||||
Special(SpecialSetting),
|
||||
Print(PrintSetting),
|
||||
}
|
||||
|
||||
impl<'a> From<AllFlags<'a>> for ArgOptions<'a> {
|
||||
|
@ -289,7 +294,7 @@ fn stty(opts: &Options) -> UResult<()> {
|
|||
} else if *arg == "rows" {
|
||||
if let Some(rows) = args_iter.next() {
|
||||
if let Some(n) = parse_rows_cols(rows) {
|
||||
valid_args.push(ArgOptions::Special(SpecialSettings::Rows(n)));
|
||||
valid_args.push(ArgOptions::Special(SpecialSetting::Rows(n)));
|
||||
} else {
|
||||
return Err(USimpleError::new(
|
||||
1,
|
||||
|
@ -302,7 +307,7 @@ fn stty(opts: &Options) -> UResult<()> {
|
|||
} else if *arg == "columns" || *arg == "cols" {
|
||||
if let Some(cols) = args_iter.next() {
|
||||
if let Some(n) = parse_rows_cols(cols) {
|
||||
valid_args.push(ArgOptions::Special(SpecialSettings::Cols(n)));
|
||||
valid_args.push(ArgOptions::Special(SpecialSetting::Cols(n)));
|
||||
} else {
|
||||
return Err(USimpleError::new(
|
||||
1,
|
||||
|
@ -312,7 +317,9 @@ fn stty(opts: &Options) -> UResult<()> {
|
|||
} else {
|
||||
return Err(USimpleError::new(1, format!("missing argument to '{arg}'")));
|
||||
}
|
||||
// not a valid control char or flag
|
||||
} else if *arg == "size" {
|
||||
valid_args.push(ArgOptions::Print(PrintSetting::Size));
|
||||
// not a valid option
|
||||
} else {
|
||||
return Err(USimpleError::new(1, format!("invalid argument '{arg}'")));
|
||||
}
|
||||
|
@ -329,6 +336,9 @@ fn stty(opts: &Options) -> UResult<()> {
|
|||
ArgOptions::Special(setting) => {
|
||||
apply_special_setting(setting, opts.file.as_raw_fd())?;
|
||||
}
|
||||
ArgOptions::Print(setting) => {
|
||||
print_special_setting(setting, opts.file.as_raw_fd())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
tcsetattr(
|
||||
|
@ -358,6 +368,17 @@ fn check_flag_group<T>(flag: &Flag<T>, remove: bool) -> bool {
|
|||
remove && flag.group.is_some()
|
||||
}
|
||||
|
||||
fn print_special_setting(setting: &PrintSetting, fd: i32) -> nix::Result<()> {
|
||||
match setting {
|
||||
PrintSetting::Size => {
|
||||
let mut size = TermSize::default();
|
||||
unsafe { tiocgwinsz(fd, &raw mut size)? };
|
||||
println!("{} {}", size.rows, size.columns);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_terminal_size(termios: &Termios, opts: &Options) -> nix::Result<()> {
|
||||
let speed = cfgetospeed(termios);
|
||||
|
||||
|
@ -632,7 +653,7 @@ fn apply_char_mapping(termios: &mut Termios, mapping: &(SpecialCharacterIndices,
|
|||
termios.control_chars[mapping.0 as usize] = mapping.1;
|
||||
}
|
||||
|
||||
fn apply_special_setting(setting: &SpecialSettings, fd: i32) -> nix::Result<()> {
|
||||
fn apply_special_setting(setting: &SpecialSetting, fd: i32) -> nix::Result<()> {
|
||||
let mut size = TermSize::default();
|
||||
unsafe { tiocgwinsz(fd, &raw mut size)? };
|
||||
match setting {
|
||||
|
|
|
@ -48,6 +48,14 @@ fn all_and_setting() {
|
|||
.stderr_contains("when specifying an output style, modes may not be set");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_and_print_setting() {
|
||||
new_ucmd!()
|
||||
.args(&["--all", "size"])
|
||||
.fails()
|
||||
.stderr_contains("when specifying an output style, modes may not be set");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_and_all() {
|
||||
new_ucmd!()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue