Created
September 7, 2020 18:25
-
-
Save 14NGiestas/6cb7522441dd81bd221776f70189d5f5 to your computer and use it in GitHub Desktop.
usage of isatty function to fix ansi escapes being written to output file throught redirection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ansi_colors | |
use iso_fortran_env | |
implicit none | |
character(len=1), parameter :: c_esc = achar(27) | |
character(len=2), parameter :: c_start = c_esc // '[' | |
character(len=1), parameter :: c_end = 'm' | |
character(len=*), parameter :: c_black = '30' | |
character(len=*), parameter :: c_red = '31' | |
character(len=*), parameter :: c_green = '32' | |
character(len=*), parameter :: c_yellow = '33' | |
character(len=*), parameter :: c_blue = '34' | |
character(len=*), parameter :: c_magenta = '35' | |
character(len=*), parameter :: c_cyan = '36' | |
character(len=*), parameter :: c_white = '37' | |
character(len=*), parameter :: c_clear = c_start // '0' // c_end | |
contains | |
function color(str, code) result(out) | |
character(len=*), intent(in) :: str | |
character(len=*), intent(in) :: code | |
character(len=:), allocatable :: out | |
if (isatty(output_unit)) then | |
out = c_start // code // c_end // str // c_clear | |
else | |
out = str | |
end if | |
end function color | |
end module ansi_colors | |
program ansi | |
use ansi_colors | |
use iso_fortran_env | |
implicit none | |
character(len=*), parameter :: endl = new_line('a') | |
write(output_unit, '(a)') & | |
color('Red', c_red) // endl // & | |
color('Green', c_green) // endl // & | |
color('Yellow', c_yellow) // endl // & | |
color('Blue', c_blue) // endl // & | |
color('Magenta', c_magenta) // endl // & | |
color('Cyan', c_cyan) // endl // & | |
color('White', c_white) | |
end program ansi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red | |
Green | |
Yellow | |
Blue | |
Magenta | |
Cyan | |
White |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment