Skip to content

Commit 8d6fcad

Browse files
committed
feat(stdlib_system): rename get_console_width to get_terminal_size and update its functionality
1 parent 8b2e55f commit 8d6fcad

File tree

8 files changed

+52
-47
lines changed

8 files changed

+52
-47
lines changed

doc/specs/stdlib_system.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,39 +533,36 @@ 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_console_width` - Get the width of the console
536+
## `get_terminal_size` - Get the size of the terminal window
537537

538538
### Status
539539

540540
Experimental
541541

542542
### Description
543543

544-
This function returns the width of the console window in characters.
545-
It is designed to work across multiple platforms. On Windows, the width is determined by the console window's size.
546-
On UNIX-like systems (Linux, macOS), the width is determined by the terminal's size.
544+
This subroutine returns the size of the terminal window in characters.
547545

548546
Note: This routine performs a detailed runtime inspection, so it has non-negligible overhead.
549547

550548
### Syntax
551549

552-
`width = [[stdlib_system(module):get_console_width(function)]]()`
550+
`call [[stdlib_system(module):get_terminal_size(subroutine)]](columns, lines)`
553551

554552
### Class
555553

556-
Function
554+
Subroutine
557555

558556
### Arguments
559557

560-
None.
558+
`columns`: Shall be an `intent(out)` argument of type `integer` that will contain the number of columns in the terminal window.
561559

562-
### Return Value
560+
`lines`: Shall be an `intent(out)` argument of type `integer` that will contain the number of lines in the terminal window.
563561

564-
- **Type:** `integer`
565-
- Returns the width of the console window in characters.
562+
Note: If the query fails, the values of `columns` and `lines` will be set to `-1`.
566563

567564
### Example
568565

569566
```fortran
570-
{!./example/system/example_get_console_width.f90}!
567+
{!./example/system/example_get_terminal_size.f90}!
571568
```

example/system/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ADD_EXAMPLE(get_runtime_os)
2-
ADD_EXAMPLE(get_console_width)
2+
ADD_EXAMPLE(get_terminal_size)
33
ADD_EXAMPLE(delete_file)
44
ADD_EXAMPLE(is_directory)
55
ADD_EXAMPLE(null_device)

example/system/example_get_console_width.f90

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
program example_get_terminal_size
2+
3+
use stdlib_system, only: get_terminal_size
4+
implicit none
5+
6+
integer :: columns, lines
7+
8+
!> Get terminal size
9+
call get_terminal_size(columns, lines)
10+
11+
print "(2(a,i0))", "Terminal size is ", columns, 'x', lines
12+
print "(a)", repeat("*", columns)
13+
14+
end program example_get_terminal_size

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ set(SRC
112112
stdlib_hashmap_open.f90
113113
stdlib_logger.f90
114114
stdlib_sorting_radix_sort.f90
115-
stdlib_system_get_console_width.c
115+
stdlib_system_get_terminal_size.c
116116
stdlib_system_subprocess.c
117117
stdlib_system_subprocess.F90
118118
stdlib_system.F90

src/stdlib_system.F90

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module stdlib_system
8484
public :: elapsed
8585
public :: is_windows
8686

87-
public :: get_console_width
87+
public :: get_terminal_size
8888

8989
!! version: experimental
9090
!!
@@ -550,14 +550,16 @@ module function process_get_ID(process) result(ID)
550550
integer(process_ID) :: ID
551551
end function process_get_ID
552552

553-
!! Returns the width of the console window.
553+
!! Returns the size of the terminal window.
554554
!!
555555
!! ### Returns:
556-
!! - **integer**: The width of the console window.
556+
!! - **columns**: The number of columns in the terminal window.
557+
!! - **lines**: The number of lines in the terminal window.
557558
!!
558559
!! Note: This function performs a detailed runtime inspection, so it has non-negligible overhead.
559-
integer function get_console_width() bind(C)
560-
end function get_console_width
560+
subroutine get_terminal_size(columns, lines) bind(C)
561+
integer, intent(out) :: columns, lines
562+
end subroutine get_terminal_size
561563

562564
end interface
563565

src/stdlib_system_get_console_width.c renamed to src/stdlib_system_get_terminal_size.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77
#include <unistd.h>
88
#endif
99

10-
int get_console_width()
10+
void get_terminal_size(int *columns, int *lines)
1111
{
12-
int width = 80; // default value
12+
*columns = -1;
13+
*lines = -1;
14+
1315
#ifdef _WIN32
14-
CONSOLE_SCREEN_BUFFER_INFO csbi;
1516
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
17+
CONSOLE_SCREEN_BUFFER_INFO csbi;
1618
if (GetConsoleScreenBufferInfo(hConsole, &csbi))
1719
{
18-
width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
20+
*columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
21+
*lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1922
}
2023
#else
2124
struct winsize w;
2225
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0)
2326
{
24-
width = w.ws_col;
27+
*columns = w.ws_col;
28+
*lines = w.ws_row;
2529
}
2630
#endif
27-
return width;
28-
}
31+
}

test/system/test_os.f90

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module test_os
22
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
33
use stdlib_system, only: get_runtime_os, OS_WINDOWS, OS_UNKNOWN, OS_TYPE, is_windows, null_device, &
4-
get_console_width
4+
get_terminal_size
55
implicit none
66

77
contains
@@ -12,23 +12,26 @@ subroutine collect_suite(testsuite)
1212
type(unittest_type), allocatable, intent(out) :: testsuite(:)
1313

1414
testsuite = [ &
15-
new_unittest('test_get_console_width', test_get_console_width), &
15+
new_unittest('test_get_terminal_size', test_get_terminal_size), &
1616
new_unittest('test_get_runtime_os', test_get_runtime_os), &
1717
new_unittest('test_is_windows', test_is_windows), &
1818
new_unittest('test_null_device', test_null_device) &
1919
]
2020
end subroutine collect_suite
2121

22-
subroutine test_get_console_width(error)
22+
subroutine test_get_terminal_size(error)
2323
type(error_type), allocatable, intent(out) :: error
24-
integer :: width
24+
integer :: columns, lines
2525

26-
!> Get console width
27-
width = get_console_width()
26+
!> Get terminal size
27+
call get_terminal_size(columns, lines)
2828

29-
call check(error, width > 0, "Console width is not positive")
29+
call check(error, columns > 0, "Terminal width is not positive")
30+
if (allocated(error)) return
31+
32+
call check(error, lines > 0, "Terminal height is not positive")
3033

31-
end subroutine test_get_console_width
34+
end subroutine test_get_terminal_size
3235

3336
subroutine test_get_runtime_os(error)
3437
type(error_type), allocatable, intent(out) :: error

0 commit comments

Comments
 (0)