Skip to content

Commit 097d10c

Browse files
committed
fixed typo, added specs for stdlib_ansi_cursor, added helpful error messages to tests, small fixes
1 parent 26b010d commit 097d10c

File tree

3 files changed

+305
-15
lines changed

3 files changed

+305
-15
lines changed

doc/specs/stdlib_ansi_cursor.md

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
title: ansi_cursor
3+
---
4+
5+
# The `stdlib_ansi_cursor` module
6+
7+
[TOC]
8+
9+
## Introduction
10+
11+
Module for cursor control using ansi terminal escape sequences
12+
13+
## Constants provided by `stdlib_ascii`
14+
15+
### ``esc``
16+
17+
The ESC character
18+
19+
20+
### ``home``
21+
22+
ansi escape code to move the cursor to it's home coordinates `(0,0)`
23+
24+
25+
### ``clear_till_screen_start``
26+
27+
ansi escape code to clear the screen till the start of the terminal
28+
29+
30+
### ``clear_till_screen_end``
31+
32+
ansi escape code to clear the screen till the end of the terminal
33+
34+
35+
### ``clear_completetely``
36+
37+
ansi escape code to clear the terminal screen completely
38+
39+
40+
### ``clear_till_line_end``
41+
42+
ansi escape code to clear till the current line end
43+
44+
45+
### ``clear_till_line_start``
46+
47+
ansi escape code to clear till the current line start
48+
49+
50+
### ``clear_entire_line``
51+
52+
ansi escape code to clear the entire line
53+
54+
55+
56+
## Procedures and methods provided
57+
58+
59+
### `move_to`
60+
61+
#### Status
62+
63+
Experimental
64+
65+
#### Description
66+
67+
moves the cursor to the specified `line` and `column`
68+
69+
#### Syntax
70+
71+
`code =` [[stdlib_ansi_cursor(module):move_to(function)]] `(line, col)`
72+
73+
#### Class
74+
75+
Pure function.
76+
77+
#### Arguments
78+
79+
`line`: line (row) number to move it to
80+
81+
`col`: col (column) number to move it to
82+
83+
#### Return value
84+
85+
a default character string
86+
87+
#### Examples
88+
89+
```fortran
90+
program test
91+
use stdlib_ansi_cursor, only: move_to
92+
implicit none
93+
94+
character(len=1) :: input
95+
96+
print *, move_to(0, 0) ! Same as printing the constant `home`
97+
read (*,*), input ! Waiting for input to actually see the effect of the `move_to` function
98+
end program test
99+
```
100+
101+
102+
### `move_to_column`
103+
104+
#### Status
105+
106+
Experimental
107+
108+
#### Description
109+
110+
moves the cursor to the specified `column`
111+
112+
#### Syntax
113+
114+
`code =` [[stdlib_ansi_cursor(module):move_to_column(function)]] `(col)`
115+
116+
#### Class
117+
118+
Pure function.
119+
120+
#### Arguments
121+
122+
`col`: col (column) number to move it to
123+
124+
#### Return value
125+
126+
a default character string
127+
128+
129+
### `move_up`
130+
131+
#### Status
132+
133+
Experimental
134+
135+
#### Description
136+
137+
moves the cursor up by `line` lines
138+
139+
#### Syntax
140+
141+
`code =` [[stdlib_ansi_cursor(module):move_up(function)]] `(line)`
142+
143+
#### Class
144+
145+
Pure function.
146+
147+
#### Arguments
148+
149+
`line`: number of lines to move it above by
150+
151+
#### Return value
152+
153+
a default character string
154+
155+
156+
### `move_down`
157+
158+
#### Status
159+
160+
Experimental
161+
162+
#### Description
163+
164+
moves the cursor down by `line` lines
165+
166+
#### Syntax
167+
168+
`code =` [[stdlib_ansi_cursor(module):move_down(function)]] `(line)`
169+
170+
#### Class
171+
172+
Pure function.
173+
174+
#### Arguments
175+
176+
`line`: number of lines to move it below by
177+
178+
#### Return value
179+
180+
a default character string
181+
182+
183+
### `move_left`
184+
185+
#### Status
186+
187+
Experimental
188+
189+
#### Description
190+
191+
moves the cursor to the left by `col` columns
192+
193+
#### Syntax
194+
195+
`code =` [[stdlib_ansi_cursor(module):move_left(function)]] `(col)`
196+
197+
#### Class
198+
199+
Pure function.
200+
201+
#### Arguments
202+
203+
`col`: number of columns to move the cursor to the left by
204+
205+
#### Return value
206+
207+
a default character string
208+
209+
210+
### `move_right`
211+
212+
#### Status
213+
214+
Experimental
215+
216+
#### Description
217+
218+
moves the cursor to the right by `col` columns
219+
220+
#### Syntax
221+
222+
`code =` [[stdlib_ansi_cursor(module):move_right(function)]] `(col)`
223+
224+
#### Class
225+
226+
Pure function.
227+
228+
#### Arguments
229+
230+
`col`: number of columns to move the cursor to the right by
231+
232+
#### Return value
233+
234+
a default character string
235+

src/stdlib_ansi_cursor.f90

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ module stdlib_ansi_cursor
22
use stdlib_strings, only: to_string
33
implicit none
44

5+
private
6+
7+
public :: move_to, move_up, move_down, move_left, move_right, move_to_column
8+
public :: esc, home, clear_till_screen_end, clear_till_screen_start, clear_completely, &
9+
& clear_till_line_end, clear_till_line_start, clear_entire_line
10+
11+
!> the ESC character
512
character(len=*), parameter :: esc = achar(27)
613
!> moves the cursor to home => `(0,0)`
714
character(len=*), parameter :: home = esc//"[H"
815
!> erases from the cursor till the end of the screen
9-
character(len=*), parameter :: clear_till_screen_end = esc//"[OJ"
16+
character(len=*), parameter :: clear_till_screen_end = esc//"[0J"
1017
!> erases from the cursor to the beginning of the screen
1118
character(len=*), parameter :: clear_till_screen_start = esc//"[1J"
1219
!> erases the entire screen
@@ -19,8 +26,11 @@ module stdlib_ansi_cursor
1926
character(len=*), parameter :: clear_entire_line = esc//"[2K"
2027

2128
contains
29+
!> Version: Experimental
30+
!>
2231
!> moves the cursor to `(line, column)`
2332
!> returns an empty string if any of them is negative
33+
!> [Specification](../page/specs/stdlib_ansi_cursor.html#move_to)
2434
pure function move_to(line, col) result(str)
2535
integer, intent(in) :: line
2636
integer, intent(in) :: col
@@ -34,8 +44,11 @@ pure function move_to(line, col) result(str)
3444

3545
end function move_to
3646

47+
!> Version: Experimental
48+
!>
3749
!> moves the cursor to column `col`
3850
!> returns an empty string if `col` is negative
51+
!> [Specification](../page/specs/stdlib_ansi_cursor.html#move_to_column)
3952
pure function move_to_column(col) result(str)
4053
integer, intent(in) :: col
4154
character(:), allocatable :: str
@@ -48,8 +61,11 @@ pure function move_to_column(col) result(str)
4861

4962
end function move_to_column
5063

64+
!> Version: Experimental
65+
!>
5166
!> moves the cursor up by `line` lines
5267
!> returns an empty string if `line` is negative
68+
!> [Specification](../page/specs/stdlib_ansi_cursor.html#move_up)
5369
pure function move_up(line) result(str)
5470
integer, intent(in) :: line
5571
character(:), allocatable :: str
@@ -62,8 +78,11 @@ pure function move_up(line) result(str)
6278

6379
end function move_up
6480

81+
!> Version: Experimental
82+
!>
6583
!> moves the cursor down by `line` lines
6684
!> returns an empty string if `line` is negative
85+
!> [Specification](../page/specs/stdlib_ansi_cursor.html#move_down)
6786
pure function move_down(line) result(str)
6887
integer, intent(in) :: line
6988
character(:), allocatable :: str
@@ -76,30 +95,36 @@ pure function move_down(line) result(str)
7695

7796
end function move_down
7897

98+
!> Version: Experimental
99+
!>
79100
!> moves the cursor right by `line` lines
80101
!> returns an empty string if `line` is negative
81-
pure function move_right(line) result(str)
82-
integer, intent(in) :: line
102+
!> [Specification](../page/specs/stdlib_ansi_cursor.html#move_right)
103+
pure function move_right(col) result(str)
104+
integer, intent(in) :: col
83105
character(:), allocatable :: str
84106

85-
if (line <= 0) then
107+
if (col <= 0) then
86108
str = ""
87109
else
88-
str = esc//"["//to_string(line)//"C"
110+
str = esc//"["//to_string(col)//"C"
89111
end if
90112

91113
end function move_right
92114

115+
!> Version: Experimental
116+
!>
93117
!> moves the cursor left by `line` lines
94118
!> returns an empty string if `line` is negative
95-
pure function move_left(line) result(str)
96-
integer, intent(in) :: line
119+
!> [Specification](../page/specs/stdlib_ansi_cursor.html#move_left)
120+
pure function move_left(col) result(str)
121+
integer, intent(in) :: col
97122
character(:), allocatable :: str
98123

99-
if (line <= 0) then
124+
if (col <= 0) then
100125
str = ""
101126
else
102-
str = esc//"["//to_string(line)//"D"
127+
str = esc//"["//to_string(col)//"D"
103128
end if
104129

105130
end function move_left

0 commit comments

Comments
 (0)