Appearance
6. A polished report
The last chapter prints the report on the terminal: a title, aligned columns, the notes of the cheapest car wrapped in a paragraph.
A title, in colour
f90
title = 'cars report'
title = title%startcase()
print '(A)', title%colorize(color_fg='cyan', style='bold_on')
print '(A)', repeat('=', title%len())colorize returns the characters of the string wrapped in the ANSI codes of a colour and of a style. repeat is the Fortran intrinsic.
Aligned columns
f90
lowest = huge(1._R8P)
do r = 2, size(rows)
call rows(r)%split(tokens=cells, sep=',')
cells = cells%strip()
price = cells(4)%to_number(kind=1._R8P)
if (price < lowest) then
lowest = price
cheapest = rows(r)
endif
cells(4) = nint(price) ! a number into a string: +3000
cells(4) = cells(4)%slice(first=2) ! without the sign
print '(A)', cells(1)//' '//cells(2)%ljust(6)//cells(3)%ljust(8)//cells(4)%rjust(6, '.')
enddoljustandrjustpad a string to a width, with blanks or with a fill character:ljustadds them on the right,rjuston the left,centeron both sides.fillpads with zeros by default.- The price becomes a rounded integer assigned back to the cell;
slice(first=2)drops its sign.slicetakes a first and a last position and a stride, all optional, and returns acharacter.
A justified paragraph
f90
call cheapest%split(tokens=cells, sep=',')
cells = cells%strip()
print '(A)', ''
print '(A)', 'The cheapest is the '//glue%join(array=cells(2:3), sep=' ')//':'
call cells(5)%justify(lines=lines, width=30)
do l = 1, size(lines)
print '(A)', ' '//lines(l)%colorize(style='italics_on')
enddojustify wraps the words of a string in lines of a given width, spreading the blanks so that every line but the last one is as wide as the others. Like split, it is a subroutine that allocates the lines.
Running it
The whole program:
f90
program report
!< Tutorial, chapter 6: a polished report.
use stringifor
implicit none
type(string) :: glue, title, cheapest
type(string), allocatable :: rows(:), cells(:), lines(:)
character(256) :: argument
real(R8P) :: price, lowest
integer :: r, l
call get_command_argument(1, argument)
call read_file(file=trim(argument), lines=rows)
title = 'cars report'
title = title%startcase()
print '(A)', title%colorize(color_fg='cyan', style='bold_on')
print '(A)', repeat('=', title%len())
lowest = huge(1._R8P)
do r = 2, size(rows)
call rows(r)%split(tokens=cells, sep=',')
cells = cells%strip()
price = cells(4)%to_number(kind=1._R8P)
if (price < lowest) then
lowest = price
cheapest = rows(r)
endif
cells(4) = nint(price) ! a number into a string: +3000
cells(4) = cells(4)%slice(first=2) ! without the sign
print '(A)', cells(1)//' '//cells(2)%ljust(6)//cells(3)%ljust(8)//cells(4)%rjust(6, '.')
enddo
call cheapest%split(tokens=cells, sep=',')
cells = cells%strip()
print '(A)', ''
print '(A)', 'The cheapest is the '//glue%join(array=cells(2:3), sep=' ')//':'
call cells(5)%justify(lines=lines, width=30)
do l = 1, size(lines)
print '(A)', ' '//lines(l)%colorize(style='italics_on')
enddo
endprogram reportWhat you learned
colorize, ljust and rjust, slice, justify: a report in a short program. Reference: String Manipulation, Files and Paths.
Next: the cookbook, short recipes for everyday tasks.