Skip to content

Commit b61538c

Browse files
committed
docs: improve description and error handling of get_terminal_size
1 parent c1d7a16 commit b61538c

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

doc/specs/stdlib_system.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -533,17 +533,21 @@ The file is removed from the filesystem if the operation is successful. If the o
533533
{!example/system/example_delete_file.f90!}
534534
```
535535

536-
## `get_terminal_size` - Get the size of the terminal window
536+
## `get_terminal_size` - Get terminal window size in characters
537537

538538
### Status
539539

540540
Experimental
541541

542542
### Description
543543

544-
This subroutine returns the size of the terminal window in characters.
544+
Queries the terminal window size in characters (columns × lines).
545545

546-
Note: This routine performs a detailed runtime inspection, so it has non-negligible overhead.
546+
This routine performs the following checks:
547+
1. Verifies stdout is connected to a terminal (not redirected);
548+
2. Queries terminal dimensions via platform-specific APIs.
549+
550+
Typical execution time: <100μs on modern systems.
547551

548552
### Syntax
549553

@@ -555,13 +559,24 @@ Subroutine
555559

556560
### Arguments
557561

558-
`columns`: Shall be an `intent(out)` argument of type `integer` that will contain the number of columns in the terminal window.
562+
`columns`: `integer, intent(out)`.
563+
Number of columns in the terminal window. Set to `-1` on error.
564+
565+
`lines`: `integer, intent(out)`.
566+
Number of lines in the terminal window. Set to `-1` on error.
559567

560-
`lines`: Shall be an `intent(out)` argument of type `integer` that will contain the number of lines in the terminal window.
568+
`err`: `type(state_type), intent(out), optional`.
569+
Error state object. If absent, errors terminate execution.
561570

562-
`err`: Shall be an `intent(out)` and `optional` argument of type `type(state_type)` that will contain the error state. If not provided, the program stops execution on error.
571+
### Error Handling
563572

564-
Note: If the query fails, the values of `columns` and `lines` will be set to `-1`.
573+
- **Success**: `columns` and `lines` contain valid dimensions.
574+
- **Failure**:
575+
- Both arguments set to `-1`.
576+
- If `err` present, `stat` contains error code:
577+
- Unix: Contains `errno` (typically `ENOTTY` when redirected);
578+
- Windows: Contains `GetLastError()` code.
579+
- If `err` absent: Program stops with error message.
565580

566581
### Example
567582

example/system/example_get_terminal_size.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ program example_get_terminal_size
1111
call get_terminal_size(columns, lines, err)
1212

1313
print "(2(a,i0))", "Terminal size is ", columns, 'x', lines
14-
print "(a)", repeat("*", columns)
14+
if (err%ok()) print "(a)", repeat("*", columns)
1515

1616
end program example_get_terminal_size

src/stdlib_system.F90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ end function process_get_ID
554554

555555
contains
556556

557-
!! Returns the size of the terminal window.
557+
!! Returns terminal window size in characters.
558558
!!
559559
!! ### Returns:
560560
!! - **columns**: The number of columns in the terminal window.
@@ -575,7 +575,7 @@ end subroutine c_get_terminal_size
575575

576576
call c_get_terminal_size(columns, lines, stat)
577577
if (stat /= 0) then
578-
err0 = state_type('get_terminal_size',STDLIB_FS_ERROR,'Failed to get terminal size')
578+
err0 = state_type('get_terminal_size',STDLIB_FS_ERROR,'Failed to get terminal size,','stat =',stat)
579579
call err0%handle(err)
580580
end if
581581

0 commit comments

Comments
 (0)