Skip to content

Commit 93b629e

Browse files
committed
Add: developer documentation to new files
1 parent 37ba9d7 commit 93b629e

File tree

4 files changed

+81
-22
lines changed

4 files changed

+81
-22
lines changed

src/fpm_backend_console.f90

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
!># Build Backend Console
2+
!> This module provides a lightweight implementation for printing to the console
3+
!> and updating previously-printed console lines. It used by `[[fpm_backend_output]]`
4+
!> for pretty-printing build status and progress.
5+
!>
6+
!> @note The implementation for updating previous lines relies on no other output
7+
!> going to `stdout`/`stderr` except through the `console_t` object provided.
8+
!>
9+
!> @note All write statements to `stdout` are enclosed within OpenMP `critical` regions
10+
!>
111
module fpm_backend_console
212
use iso_fortran_env, only: stdout=>output_unit
313
implicit none
@@ -7,22 +17,34 @@ module fpm_backend_console
717

818
character(len=*), parameter :: ESC = char(27)
919

20+
!> Console object
1021
type console_t
22+
!> Number of lines printed
1123
integer :: n_line = 1
24+
!> 'Plain' output (no escape codes)
1225
logical :: plain_mode = .false.
26+
!> Escape code for erasing current line
1327
character(:), allocatable :: LINE_RESET
28+
!> Escape code for moving up one line
1429
character(:), allocatable :: LINE_UP
30+
!> Escape code for moving down one line
1531
character(:), allocatable :: LINE_DOWN
1632
contains
33+
!> Initialise the console object
1734
procedure :: init => console_init
35+
!> Write a single line to the console
1836
procedure :: write_line => console_write_line
37+
!> Update a previously-written console line
1938
procedure :: update_line => console_update_line
2039
end type console_t
2140

2241
contains
2342

43+
!> Initialise the console object
2444
subroutine console_init(console,plain_mode)
45+
!> Console object to initialise
2546
class(console_t), intent(out), target :: console
47+
!> 'Plain' output (no escape codes)
2648
logical, intent(in), optional :: plain_mode
2749

2850
if (present(plain_mode)) then
@@ -41,10 +63,15 @@ subroutine console_init(console,plain_mode)
4163

4264
end subroutine console_init
4365

66+
!> Write a single line to the standard output
4467
subroutine console_write_line(console,str,line,advance)
68+
!> Console object
4569
class(console_t), intent(inout), target :: console
70+
!> String to write
4671
character(*), intent(in) :: str
72+
!> Integer needed to later update console line
4773
integer, intent(out), optional :: line
74+
!> Advancing output (print newline?)
4875
logical, intent(in), optional :: advance
4976

5077
character(3) :: adv
@@ -72,9 +99,13 @@ subroutine console_write_line(console,str,line,advance)
7299

73100
end subroutine console_write_line
74101

102+
!> Overwrite a previously-written line in standard output
75103
subroutine console_update_line(console,line_no,str)
104+
!> Console object
76105
class(console_t), intent(in) :: console
106+
!> Integer output from `[[console_write_line]]`
77107
integer, intent(in) :: line_no
108+
!> New string to overwrite line
78109
character(*), intent(in) :: str
79110

80111
integer :: n

src/fpm_backend_output.f90

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
!># Build Backend Progress Output
2+
!> This module provides a derived type `build_progress_t` for printing build status
3+
!> and progress messages to the console while the backend is building the package.
4+
!>
5+
!> The `build_progress_t` type supports two modes: `normal` and `plain`
6+
!> where the former does 'pretty' output and the latter does not.
7+
!> The `normal` mode is intended for typical interactive usage whereas
8+
!> 'plain' mode is used with the `--verbose` flag or when `stdout` is not attached
9+
!> to a terminal (e.g. when piping or redirecting `stdout`). In these cases,
10+
!> the pretty output must be suppressed to avoid control codes being output.
11+
112
module fpm_backend_output
213
use iso_fortran_env, only: stdout=>output_unit
314
use fpm_filesystem, only: basename
@@ -6,33 +17,43 @@ module fpm_backend_output
617
use M_attr, only: attr, attr_mode
718
implicit none
819

9-
type build_progress_t
20+
private
21+
public build_progress_t
1022

23+
!> Build progress object
24+
type build_progress_t
25+
!> Console object for updating console lines
1126
type(console_t) :: console
12-
27+
!> Number of completed targets
1328
integer :: n_complete
14-
29+
!> Total number of targets scheduled
1530
integer :: n_target
16-
31+
!> 'Plain' output (no colors or updating)
1732
logical :: plain_mode = .true.
18-
33+
!> Store needed when updating previous console lines
1934
integer, allocatable :: output_lines(:)
20-
35+
!> Queue of scheduled build targets
2136
type(build_target_ptr), pointer :: target_queue(:)
22-
2337
contains
38+
!> Initialise build progress object
2439
procedure :: init => output_init
40+
!> Output 'compiling' status for build target
2541
procedure :: compiling_status => output_status_compiling
42+
!> Output 'complete' status for build target
2643
procedure :: completed_status => output_status_complete
44+
!> Output finished status for whole package
2745
procedure :: success => output_progress_success
28-
2946
end type build_progress_t
3047

3148
contains
3249

50+
!> Initialise build progress object
3351
subroutine output_init(progress,target_queue,plain_mode)
52+
!> Progress object to initialise
3453
class(build_progress_t), intent(out) :: progress
54+
!> The queue of scheduled targets
3555
type(build_target_ptr), intent(in), target :: target_queue(:)
56+
!> Enable 'plain' output for progress object
3657
logical, intent(in), optional :: plain_mode
3758

3859
if (plain_mode) then
@@ -51,8 +72,11 @@ subroutine output_init(progress,target_queue,plain_mode)
5172

5273
end subroutine output_init
5374

75+
!> Output 'compiling' status for build target and overall percentage progress
5476
subroutine output_status_compiling(progress, queue_index)
77+
!> Progress object
5578
class(build_progress_t), intent(inout) :: progress
79+
!> Index of build target in the target queue
5680
integer, intent(in) :: queue_index
5781

5882
character(:), allocatable :: target_name
@@ -69,13 +93,13 @@ subroutine output_status_compiling(progress, queue_index)
6993

7094
write(overall_progress,'(A,I4,A)') '[',100*progress%n_complete/progress%n_target,'%]'
7195

72-
if (progress%plain_mode) then
96+
if (progress%plain_mode) then ! Plain output
7397

7498
!$omp critical
7599
write(*,'(A8,A30)') trim(overall_progress),target_name
76100
!$omp end critical
77101

78-
else
102+
else ! Pretty output
79103

80104
write(output_string,'(A,T40,A,A)') target_name,attr('<yellow>compiling...</yellow>')
81105
call progress%console%write_line(trim(output_string),progress%output_lines(queue_index))
@@ -88,10 +112,13 @@ subroutine output_status_compiling(progress, queue_index)
88112

89113
end subroutine output_status_compiling
90114

91-
115+
!> Output 'complete' status for build target and update overall percentage progress
92116
subroutine output_status_complete(progress, queue_index, build_stat)
117+
!> Progress object
93118
class(build_progress_t), intent(inout) :: progress
119+
!> Index of build target in the target queue
94120
integer, intent(in) :: queue_index
121+
!> Build status flag
95122
integer, intent(in) :: build_stat
96123

97124
character(:), allocatable :: target_name
@@ -118,13 +145,13 @@ subroutine output_status_complete(progress, queue_index, build_stat)
118145

119146
write(overall_progress,'(A,I4,A)') '[',100*progress%n_complete/progress%n_target,'%] '
120147

121-
if (progress%plain_mode) then
148+
if (progress%plain_mode) then ! Plain output
122149

123150
!$omp critical
124151
write(*,'(A8,A30,A7)') trim(overall_progress),target_name, 'done.'
125152
!$omp end critical
126153

127-
else
154+
else ! Pretty output
128155

129156
call progress%console%update_line(progress%output_lines(queue_index),trim(output_string))
130157

@@ -136,14 +163,15 @@ subroutine output_status_complete(progress, queue_index, build_stat)
136163

137164
end subroutine output_status_complete
138165

166+
!> Output finished status for whole package
139167
subroutine output_progress_success(progress)
140168
class(build_progress_t), intent(inout) :: progress
141169

142-
if (progress%plain_mode) then
170+
if (progress%plain_mode) then ! Plain output
143171

144172
write(*,'(A)') attr('<green>[100%] Project compiled successfully.</green>')
145173

146-
else
174+
else ! Pretty output
147175

148176
write(*,'(A)') progress%console%LINE_RESET//attr('<green>[100%] Project compiled successfully.</green>')
149177

src/fpm_environment.f90

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,6 @@ subroutine run(cmd,echo,exitstat,verbose,redirect)
198198
end if
199199
end if
200200
end if
201-
202-
203-
if(present(redirect))then
204-
verbose_local=verbose
205-
else
206-
verbose_local=.true.
207-
end if
208201

209202
if(echo_local) print *, '+ ', cmd
210203

src/ptycheck/isatty.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
// This file provides a `c_isatty` wrapper function to check if `stdout` is connected
2+
// to a terminal or not. This wrapper is required for better portability, specifically
3+
// for supporting the MS Windows command prompt and the MinTTY terminal used by MSYS2.
4+
15
#include <unistd.h> //for isatty()
26
#include <stdio.h> //for fileno()
37

48
#ifdef __MINGW64__
9+
// ptycheck/iscygpty allows us to check if connected to MinTTY in MSYS2 on Windows
510
#include "iscygpty.h"
611
#endif
712

13+
// Check if `stdout` is connected to a terminal
14+
// Returns 1 if is a terminal, and 0 otherwise
815
int c_isatty(void)
916
{
1017

0 commit comments

Comments
 (0)