Skip to content

Commit b0115d1

Browse files
committed
Apply suggestion: don't use M_attr, simplify implementation
1 parent 6aba40d commit b0115d1

File tree

3 files changed

+27
-59
lines changed

3 files changed

+27
-59
lines changed

fpm.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ rev = "2f5eaba864ff630ba0c3791126a3f811b6e437f3"
1414
git = "https://github.com/urbanjost/M_CLI2.git"
1515
rev = "ea6bbffc1c2fb0885e994d37ccf0029c99b19f24"
1616

17-
[dependencies.M_attr]
18-
git = "https://github.com/urbanjost/M_attr.git"
19-
rev = "608b9d3b40be9ff2590c23d2089781fd4da76344"
20-
21-
2217
[[test]]
2318
name = "cli-test"
2419
source-dir = "test/cli_test"

src/fpm_backend_console.f90

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,39 @@ module fpm_backend_console
1414

1515
private
1616
public :: console_t
17+
public :: LINE_RESET
18+
public :: COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_RESET
1719

1820
character(len=*), parameter :: ESC = char(27)
21+
!> Escape code for erasing current line
22+
character(len=*), parameter :: LINE_RESET = ESC//"[2K"//ESC//"[1G"
23+
!> Escape code for moving up one line
24+
character(len=*), parameter :: LINE_UP = ESC//"[1A"
25+
!> Escape code for moving down one line
26+
character(len=*), parameter :: LINE_DOWN = ESC//"[1B"
27+
!> Escape code for red foreground color
28+
character(len=*), parameter :: COLOR_RED = ESC//"[31m"
29+
!> Escape code for green foreground color
30+
character(len=*), parameter :: COLOR_GREEN = ESC//"[32m"
31+
!> Escape code for yellow foreground color
32+
character(len=*), parameter :: COLOR_YELLOW = ESC//"[93m"
33+
!> Escape code to reset foreground color
34+
character(len=*), parameter :: COLOR_RESET = ESC//"[0m"
1935

2036
!> Console object
2137
type console_t
2238
!> Number of lines printed
2339
integer :: n_line = 1
24-
!> 'Plain' output (no escape codes)
25-
logical :: plain_mode = .false.
26-
!> Escape code for erasing current line
27-
character(:), allocatable :: LINE_RESET
28-
!> Escape code for moving up one line
29-
character(:), allocatable :: LINE_UP
30-
!> Escape code for moving down one line
31-
character(:), allocatable :: LINE_DOWN
40+
3241
contains
3342
!> Write a single line to the console
3443
procedure :: write_line => console_write_line
3544
!> Update a previously-written console line
3645
procedure :: update_line => console_update_line
3746
end type console_t
3847

39-
!> Constructor for console_t
40-
interface console_t
41-
procedure :: new_console
42-
end interface console_t
43-
4448
contains
4549

46-
!> Initialise a new console object
47-
function new_console(plain_mode) result(console)
48-
!> 'Plain' output (no escape codes)
49-
logical, intent(in), optional :: plain_mode
50-
!> Console object to initialise
51-
type(console_t) :: console
52-
53-
if (present(plain_mode)) then
54-
console%plain_mode = plain_mode
55-
end if
56-
57-
if (console%plain_mode) then
58-
console%LINE_RESET = ""
59-
console%LINE_UP = ""
60-
console%LINE_DOWN = ""
61-
else
62-
console%LINE_RESET = ESC//"[2K"//ESC//"[1G"
63-
console%LINE_UP = ESC//"[1A"
64-
console%LINE_DOWN = ESC//"[1B"
65-
end if
66-
67-
end function new_console
68-
6950
!> Write a single line to the standard output
7051
subroutine console_write_line(console,str,line,advance)
7152
!> Console object
@@ -92,7 +73,7 @@ subroutine console_write_line(console,str,line,advance)
9273
line = console%n_line
9374
end if
9475

95-
write(stdout,'(A)',advance=trim(adv)) console%LINE_RESET//str
76+
write(stdout,'(A)',advance=trim(adv)) LINE_RESET//str
9677

9778
if (adv=="yes") then
9879
console%n_line = console%n_line + 1
@@ -118,12 +99,12 @@ subroutine console_update_line(console,line_no,str)
11899
n = console%n_line - line_no !+ 1 !+ 1
119100

120101
! Step back to line
121-
write(stdout,'(A)',advance="no") repeat(console%LINE_UP,n)//console%LINE_RESET
102+
write(stdout,'(A)',advance="no") repeat(LINE_UP,n)//LINE_RESET
122103

123104
write(stdout,*) str
124105

125106
! Step forward to end
126-
write(stdout,'(A)',advance="no") repeat(console%LINE_DOWN,n)//console%LINE_RESET
107+
write(stdout,'(A)',advance="no") repeat(LINE_DOWN,n)//LINE_RESET
127108

128109
!$omp end critical
129110

src/fpm_backend_output.f90

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ module fpm_backend_output
1313
use iso_fortran_env, only: stdout=>output_unit
1414
use fpm_filesystem, only: basename
1515
use fpm_targets, only: build_target_ptr
16-
use fpm_backend_console, only: console_t
17-
use M_attr, only: attr, attr_mode
16+
use fpm_backend_console, only: console_t, LINE_RESET, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_RESET
1817
implicit none
1918

2019
private
@@ -58,14 +57,6 @@ function new_build_progress(target_queue,plain_mode) result(progress)
5857
logical, intent(in), optional :: plain_mode
5958
!> Progress object to initialise
6059
type(build_progress_t) :: progress
61-
62-
if (plain_mode) then
63-
call attr_mode('plain')
64-
else
65-
call attr_mode('color')
66-
end if
67-
68-
progress%console = console_t(plain_mode)
6960

7061
progress%n_target = size(target_queue,1)
7162
progress%target_queue => target_queue
@@ -105,7 +96,8 @@ subroutine output_status_compiling(progress, queue_index)
10596

10697
else ! Pretty output
10798

108-
write(output_string,'(A,T40,A,A)') target_name,attr('<yellow>compiling...</yellow>')
99+
write(output_string,'(A,T40,A,A)') target_name, COLOR_YELLOW//'compiling...'//COLOR_RESET
100+
109101
call progress%console%write_line(trim(output_string),progress%output_lines(queue_index))
110102

111103
call progress%console%write_line(trim(overall_progress)//'Compiling...',advance=.false.)
@@ -142,9 +134,9 @@ subroutine output_status_complete(progress, queue_index, build_stat)
142134
end if
143135

144136
if (build_stat == 0) then
145-
write(output_string,'(A,T40,A,A)') target_name,attr('<green>done.</green>')
137+
write(output_string,'(A,T40,A,A)') target_name,COLOR_GREEN//'done.'//COLOR_RESET
146138
else
147-
write(output_string,'(A,T40,A,A)') target_name,attr('<red>failed.</red>')
139+
write(output_string,'(A,T40,A,A)') target_name,COLOR_RED//'failed.'//COLOR_RESET
148140
end if
149141

150142
write(overall_progress,'(A,I4,A)') '[',100*progress%n_complete/progress%n_target,'%] '
@@ -173,11 +165,11 @@ subroutine output_progress_success(progress)
173165

174166
if (progress%plain_mode) then ! Plain output
175167

176-
write(*,'(A)') attr('<green>[100%] Project compiled successfully.</green>')
168+
write(*,'(A)') '[100%] Project compiled successfully.'
177169

178170
else ! Pretty output
179171

180-
write(*,'(A)') progress%console%LINE_RESET//attr('<green>[100%] Project compiled successfully.</green>')
172+
write(*,'(A)') LINE_RESET//COLOR_GREEN//'[100%] Project compiled successfully.'//COLOR_RESET
181173

182174
end if
183175

0 commit comments

Comments
 (0)