aboutsummaryrefslogtreecommitdiff
path: root/src/fpm_backend_console.f90
diff options
context:
space:
mode:
authorLaurence Kedward <laurence.kedward@bristol.ac.uk>2021-11-22 16:13:01 +0000
committerLaurence Kedward <laurence.kedward@bristol.ac.uk>2021-11-23 10:07:56 +0000
commitbfd9b06249814ad7c4bc47c0f065d6337f87076c (patch)
tree1b1a857fa0017397b7432f7e2860baf315aeb590 /src/fpm_backend_console.f90
parent995fb2e834e4e555e8b6bc32eadb57983ef5b298 (diff)
downloadfpm-bfd9b06249814ad7c4bc47c0f065d6337f87076c.tar.gz
fpm-bfd9b06249814ad7c4bc47c0f065d6337f87076c.zip
Add: backend_output to manage pretty printing of build progress
Diffstat (limited to 'src/fpm_backend_console.f90')
-rw-r--r--src/fpm_backend_console.f9082
1 files changed, 82 insertions, 0 deletions
diff --git a/src/fpm_backend_console.f90 b/src/fpm_backend_console.f90
new file mode 100644
index 0000000..4db0cdc
--- /dev/null
+++ b/src/fpm_backend_console.f90
@@ -0,0 +1,82 @@
+module fpm_backend_console
+use iso_fortran_env, only: stdout=>output_unit
+implicit none
+
+private
+public :: console_t
+
+character(len=*), parameter :: ESC = char(27)
+
+type console_t
+ integer :: n_line = 1
+ logical :: plain_mode = .false.
+ character(:), allocatable :: LINE_RESET
+ character(:), allocatable :: LINE_UP
+ character(:), allocatable :: LINE_DOWN
+contains
+ procedure :: init => console_init
+ procedure :: write_line => console_write_line
+ procedure :: update_line => console_update_line
+end type console_t
+
+contains
+
+subroutine console_init(console,plain_mode)
+ class(console_t), intent(out), target :: console
+ logical, intent(in), optional :: plain_mode
+
+ if (present(plain_mode)) then
+ console%plain_mode = plain_mode
+ end if
+
+ if (console%plain_mode) then
+ console%LINE_RESET = ""
+ console%LINE_UP = ""
+ console%LINE_DOWN = ""
+ else
+ console%LINE_RESET = ESC//"[2K"//ESC//"[1G"
+ console%LINE_UP = ESC//"[1A"
+ console%LINE_DOWN = ESC//"[1B"
+ end if
+
+end subroutine console_init
+
+function console_write_line(console,str) result(line)
+ class(console_t), intent(inout), target :: console
+ character(*), intent(in) :: str
+ integer :: line
+
+ !$omp critical
+ line = console%n_line
+
+ write(stdout,*) console%LINE_RESET//str
+
+ console%n_line = console%n_line + 1
+ !$omp end critical
+
+end function console_write_line
+
+subroutine console_update_line(console,line_no,str)
+ class(console_t), intent(in) :: console
+ integer, intent(in) :: line_no
+ character(*), intent(in) :: str
+
+ integer :: n
+
+ !$omp critical
+
+ n = console%n_line - line_no !+ 1 !+ 1
+
+ ! Step back to line
+ write(stdout,'(A)',advance="no") repeat(console%LINE_UP,n)//console%LINE_RESET
+
+ write(stdout,*) str
+
+ ! Step forward to end
+ write(stdout,'(A)',advance="no") repeat(console%LINE_DOWN,n)//console%LINE_RESET
+
+ !$omp end critical
+
+end subroutine console_update_line
+
+end module fpm_backend_console \ No newline at end of file