Skip to content

Commit 37ba9d7

Browse files
committed
Simplify implementation and cleanup plain mode output
1 parent ab7cb42 commit 37ba9d7

File tree

3 files changed

+137
-72
lines changed

3 files changed

+137
-72
lines changed

src/fpm_backend.F90

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ subroutine build_package(targets,model,verbose)
6565
type(string_t), allocatable :: build_dirs(:)
6666
type(string_t) :: temp
6767

68-
type(console_t) :: console
69-
integer :: line, n_complete
68+
type(build_progress_t) :: progress
7069
logical :: plain_output
7170

7271
! Need to make output directory for include (mod) files
@@ -97,34 +96,31 @@ subroutine build_package(targets,model,verbose)
9796
allocate(stat(size(queue)))
9897
stat(:) = 0
9998
build_failed = .false.
100-
n_complete = 0
10199

102100
! Set output mode
103101
#ifndef FPM_BOOTSTRAP
104102
plain_output = (.not.(c_isatty()==1)) .or. verbose
105103
#else
106-
plain_output = verbose
104+
plain_output = .true.
107105
#endif
108-
call console%init(plain_output)
109-
call output_init(plain_output)
106+
107+
call progress%init(queue,plain_output)
110108

111109
! Loop over parallel schedule regions
112110
do i=1,size(schedule_ptr)-1
113111

114112
! Build targets in schedule region i
115-
!$omp parallel do default(shared) private(skip_current,line) schedule(dynamic,1)
113+
!$omp parallel do default(shared) private(skip_current) schedule(dynamic,1)
116114
do j=schedule_ptr(i),(schedule_ptr(i+1)-1)
117115

118-
! Update console output
119-
call output_status_compiling(console, line, queue(j)%ptr)
120-
call output_progress(n_complete, size(queue),plain_output)
121-
122116
! Check if build already failed
123117
!$omp atomic read
124118
skip_current = build_failed
125119

126120
if (.not.skip_current) then
121+
call progress%compiling_status(j)
127122
call build_target(model,queue(j)%ptr,verbose,stat(j))
123+
call progress%completed_status(j,stat(j))
128124
end if
129125

130126
! Set global flag if this target failed to build
@@ -133,10 +129,6 @@ subroutine build_package(targets,model,verbose)
133129
build_failed = .true.
134130
end if
135131

136-
! Update console output
137-
call output_status_complete(console, line, queue(j)%ptr,stat(j), n_complete)
138-
call output_progress(n_complete, size(queue),plain_output)
139-
140132
end do
141133

142134
! Check if this schedule region failed: exit with message if failed
@@ -157,7 +149,7 @@ subroutine build_package(targets,model,verbose)
157149

158150
end do
159151

160-
call output_progress_complete()
152+
call progress%success()
161153

162154
end subroutine build_package
163155

src/fpm_backend_console.f90

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,36 @@ subroutine console_init(console,plain_mode)
4141

4242
end subroutine console_init
4343

44-
function console_write_line(console,str) result(line)
44+
subroutine console_write_line(console,str,line,advance)
4545
class(console_t), intent(inout), target :: console
4646
character(*), intent(in) :: str
47-
integer :: line
47+
integer, intent(out), optional :: line
48+
logical, intent(in), optional :: advance
49+
50+
character(3) :: adv
51+
52+
adv = "yes"
53+
if (present(advance)) then
54+
if (.not.advance) then
55+
adv = "no"
56+
end if
57+
end if
4858

4959
!$omp critical
50-
line = console%n_line
5160

52-
write(stdout,*) console%LINE_RESET//str
61+
if (present(line)) then
62+
line = console%n_line
63+
end if
64+
65+
write(stdout,'(A)',advance=trim(adv)) console%LINE_RESET//str
66+
67+
if (adv=="yes") then
68+
console%n_line = console%n_line + 1
69+
end if
5370

54-
console%n_line = console%n_line + 1
5571
!$omp end critical
5672

57-
end function console_write_line
73+
end subroutine console_write_line
5874

5975
subroutine console_update_line(console,line_no,str)
6076
class(console_t), intent(in) :: console

src/fpm_backend_output.f90

Lines changed: 107 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
module fpm_backend_output
22
use iso_fortran_env, only: stdout=>output_unit
33
use fpm_filesystem, only: basename
4-
use fpm_targets, only: build_target_t
4+
use fpm_targets, only: build_target_ptr
55
use fpm_backend_console, only: console_t
66
use M_attr, only: attr, attr_mode
77
implicit none
88

9+
type build_progress_t
10+
11+
type(console_t) :: console
12+
13+
integer :: n_complete
14+
15+
integer :: n_target
16+
17+
logical :: plain_mode = .true.
18+
19+
integer, allocatable :: output_lines(:)
20+
21+
type(build_target_ptr), pointer :: target_queue(:)
22+
23+
contains
24+
procedure :: init => output_init
25+
procedure :: compiling_status => output_status_compiling
26+
procedure :: completed_status => output_status_complete
27+
procedure :: success => output_progress_success
28+
29+
end type build_progress_t
930

1031
contains
1132

12-
subroutine output_init(plain_mode)
33+
subroutine output_init(progress,target_queue,plain_mode)
34+
class(build_progress_t), intent(out) :: progress
35+
type(build_target_ptr), intent(in), target :: target_queue(:)
1336
logical, intent(in), optional :: plain_mode
1437

1538
if (plain_mode) then
@@ -18,80 +41,114 @@ subroutine output_init(plain_mode)
1841
call attr_mode('color')
1942
end if
2043

44+
call progress%console%init(plain_mode)
45+
46+
progress%n_target = size(target_queue,1)
47+
progress%target_queue => target_queue
48+
progress%plain_mode = plain_mode
49+
50+
allocate(progress%output_lines(progress%n_target))
51+
2152
end subroutine output_init
2253

23-
subroutine output_status_compiling(console, line, target)
24-
type(console_t), intent(inout), target :: console
25-
integer, intent(inout) :: line
26-
type(build_target_t), intent(in) :: target
54+
subroutine output_status_compiling(progress, queue_index)
55+
class(build_progress_t), intent(inout) :: progress
56+
integer, intent(in) :: queue_index
2757

2858
character(:), allocatable :: target_name
2959
character(100) :: output_string
60+
character(100) :: overall_progress
3061

31-
if (allocated(target%source)) then
32-
target_name = basename(target%source%file_name)
33-
else
34-
target_name = basename(target%output_file)
35-
end if
62+
associate(target=>progress%target_queue(queue_index)%ptr)
63+
64+
if (allocated(target%source)) then
65+
target_name = basename(target%source%file_name)
66+
else
67+
target_name = basename(target%output_file)
68+
end if
69+
70+
write(overall_progress,'(A,I4,A)') '[',100*progress%n_complete/progress%n_target,'%]'
71+
72+
if (progress%plain_mode) then
73+
74+
!$omp critical
75+
write(*,'(A8,A30)') trim(overall_progress),target_name
76+
!$omp end critical
3677

37-
write(output_string,'(A,T40,A,A)') target_name,attr('<yellow>compiling...</yellow>')
78+
else
3879

39-
line = console%write_line(trim(output_string))
80+
write(output_string,'(A,T40,A,A)') target_name,attr('<yellow>compiling...</yellow>')
81+
call progress%console%write_line(trim(output_string),progress%output_lines(queue_index))
82+
83+
call progress%console%write_line(trim(overall_progress)//'Compiling...',advance=.false.)
84+
85+
end if
86+
87+
end associate
4088

4189
end subroutine output_status_compiling
4290

43-
subroutine output_status_complete(console, line, target, build_stat, n_complete)
44-
type(console_t), intent(inout), target :: console
45-
integer, intent(in) :: line
46-
type(build_target_t), intent(in) :: target
91+
92+
subroutine output_status_complete(progress, queue_index, build_stat)
93+
class(build_progress_t), intent(inout) :: progress
94+
integer, intent(in) :: queue_index
4795
integer, intent(in) :: build_stat
48-
integer, intent(inout) :: n_complete
4996

5097
character(:), allocatable :: target_name
5198
character(100) :: output_string
52-
53-
if (allocated(target%source)) then
54-
target_name = basename(target%source%file_name)
55-
else
56-
target_name = basename(target%output_file)
57-
end if
58-
59-
if (build_stat == 0) then
60-
write(output_string,'(A,T40,A,A)') target_name,attr('<green>done.</green>')
61-
else
62-
write(output_string,'(A,T40,A,A)') target_name,attr('<red>failed.</red>')
63-
end if
64-
65-
call console%update_line(line,trim(output_string))
99+
character(100) :: overall_progress
66100

67101
!$omp critical
68-
n_complete = n_complete + 1
102+
progress%n_complete = progress%n_complete + 1
69103
!$omp end critical
70104

71-
end subroutine output_status_complete
105+
associate(target=>progress%target_queue(queue_index)%ptr)
72106

73-
subroutine output_progress(n_complete, total, plain_mode)
74-
integer, intent(in) :: n_complete, total
75-
logical :: plain_mode
107+
if (allocated(target%source)) then
108+
target_name = basename(target%source%file_name)
109+
else
110+
target_name = basename(target%output_file)
111+
end if
76112

77-
character(:), allocatable :: advance
113+
if (build_stat == 0) then
114+
write(output_string,'(A,T40,A,A)') target_name,attr('<green>done.</green>')
115+
else
116+
write(output_string,'(A,T40,A,A)') target_name,attr('<red>failed.</red>')
117+
end if
78118

79-
if (plain_mode) then
80-
advance = "yes"
81-
else
82-
advance = "no"
83-
end if
119+
write(overall_progress,'(A,I4,A)') '[',100*progress%n_complete/progress%n_target,'%] '
84120

85-
!$omp critical
86-
write(*,'(A,I4,A,A)',advance=advance) '[',100*n_complete/total,'%] Compiling project...'
87-
!$omp end critical
121+
if (progress%plain_mode) then
122+
123+
!$omp critical
124+
write(*,'(A8,A30,A7)') trim(overall_progress),target_name, 'done.'
125+
!$omp end critical
126+
127+
else
88128

89-
end subroutine output_progress
129+
call progress%console%update_line(progress%output_lines(queue_index),trim(output_string))
90130

91-
subroutine output_progress_complete()
131+
call progress%console%write_line(trim(overall_progress)//'Compiling...',advance=.false.)
92132

93-
write(*,'(A)') char(27)//"[2K"//char(27)//"[1G"//attr('<green>[100%] Project compiled successfully.</green>')
133+
end if
134+
135+
end associate
136+
137+
end subroutine output_status_complete
138+
139+
subroutine output_progress_success(progress)
140+
class(build_progress_t), intent(inout) :: progress
141+
142+
if (progress%plain_mode) then
143+
144+
write(*,'(A)') attr('<green>[100%] Project compiled successfully.</green>')
145+
146+
else
147+
148+
write(*,'(A)') progress%console%LINE_RESET//attr('<green>[100%] Project compiled successfully.</green>')
149+
150+
end if
94151

95-
end subroutine output_progress_complete
152+
end subroutine output_progress_success
96153

97154
end module fpm_backend_output

0 commit comments

Comments
 (0)