aboutsummaryrefslogtreecommitdiff
path: root/dumb_render.f90
blob: 23b991b375d6a05d2eaf8a0d6d2093f42dd431c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
! Copyright (c) 2020 Jeffrey Armstrong <jeff@rainbow-100.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module dumb_render
use render
implicit none

    integer, parameter::width = 79
    integer, parameter::height = 23

    character, parameter::bullet_character = '-'

    type, extends(renderer) :: dumb_renderer

        integer::link_index
        character(len=1024), dimension(height)::link_urls

        integer::first_line

    contains

        procedure :: initialize => dumb_initialize
        procedure :: new_page => dumb_new_page
        procedure :: prepare_for_layout => prepare_for_dumb_layout
    
        procedure :: text_width => dumb_text_width
        procedure :: text_height => dumb_text_height
        procedure :: is_text_visible => dumb_text_visible
        
        procedure :: draw_porportional => dumb_text_draw

        procedure :: preformatted_width => dumb_simple_width
        procedure :: preformatted_height => dumb_simple_height
        procedure :: is_preformatted_visible => dumb_text_visible
        
        procedure :: draw_preformatted => dumb_simple_draw
    
        procedure :: link_width => dumb_link_width
        procedure :: link_height => dumb_simple_height
        procedure :: is_link_visible => dumb_text_visible
        
        procedure :: draw_link => dumb_link_draw
        
        procedure :: request_input => dumb_request_input
        
        procedure :: draw_error => dumb_draw_error
        
        procedure :: report_status => dumb_draw_status
        procedure :: status_ready => dumb_ready_status
        
        procedure :: request_action => dumb_action
    
        procedure :: request_save_filename => dumb_request_save_filename
    
        procedure :: report_displayed_page => dumb_displayed_page
    
    end type dumb_renderer
    
contains

    subroutine dumb_initialize(self)
    implicit none
    
        class(dumb_renderer)::self
        
        self%max_width = width
        self%y = 0
        self%first_line = 1
        
        self%link_index = 0
        self%link_urls = " "
        
    end subroutine dumb_initialize
    
    subroutine dumb_new_page(self)
    implicit none
    
        class(dumb_renderer)::self
        
        self%y = 0
        self%first_line = 1
        
    end subroutine dumb_new_page

    subroutine prepare_for_dumb_layout(self)
    implicit none
    
        class(dumb_renderer)::self
        
        self%link_index = 0
        self%link_urls = " "
        
        self%y = 0

    end subroutine prepare_for_dumb_layout
    
    function store_link(self, url)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::url
        integer::store_link
        
        self%link_index = self%link_index + 1
        self%link_urls(self%link_index) = url
        
        store_link = self%link_index
        
    end function store_link

    function dumb_text_width(self, text, heading, list_item)
    implicit none
        
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer, intent(in), optional::heading
        logical, intent(in), optional::list_item
        integer::dumb_text_width
        
        dumb_text_width = len_trim(text)
        
        if(present(list_item)) then
            if(list_item) then
                dumb_text_width = dumb_text_width + 3
            end if
        end if
        
    end function dumb_text_width
        
    function dumb_text_height(self, text, heading, list_item)
    implicit none
        
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer, intent(in), optional::heading
        logical, intent(in), optional::list_item
        integer::dumb_text_height
        
        dumb_text_height = 1
        
        if(present(heading)) then
            if(heading > 0) then
                dumb_text_height = 2
            end if
        end if
        
    end function dumb_text_height
    
    function dumb_simple_width(self, text)
    implicit none
        
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer::dumb_simple_width
        
        dumb_simple_width = len_trim(text)
        
    end function dumb_simple_width
    
    function dumb_simple_height(self, text)
    implicit none
        
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer::dumb_simple_height
        
        dumb_simple_height = 1
        
    end function dumb_simple_height
    
    function dumb_text_visible(self, text)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text
        logical::dumb_text_visible
        integer::onscreen
        
        onscreen = self%y-self%first_line+1
        dumb_text_visible = (onscreen >= 0 .AND. onscreen < height)
    
    end function dumb_text_visible
    
    subroutine dumb_simple_draw(self, text)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer::limit_x
        character(5)::formatting
        
        limit_x = min(len_trim(text), self%max_width)
        if(limit_x == 0) then
            write(*,*) " "
        else
            if(limit_x < 10) then
                write(formatting, '(A2, I1, A1)') '(A', limit_x, ')'
            else
                write(formatting, '(A2, I2, A1)') '(A', limit_x, ')'
            end if
            
            write(*, formatting) text(1:limit_x)
        end if
    end subroutine dumb_simple_draw
    
    subroutine dumb_text_draw(self, text, heading, list_item)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer, intent(in), optional::heading
        logical, intent(in), optional::list_item
        integer::limit_x
        character(5)::formatting
        character(self%max_width)::heading_line
        
        ! Put a bullet for list items
        if(present(list_item)) then
            if(list_item) then
                write(*, '(1X, A1, 1X)', advance='no') bullet_character
            end if
        end if
        
        limit_x = min(len_trim(text), self%max_width)
        if(limit_x == 0) then
            write(*,*) " "
        else
            if(limit_x < 10) then
                write(formatting, '(A2, I1, A1)') '(A', limit_x, ')'
            else
                write(formatting, '(A2, I2, A1)') '(A', limit_x, ')'
            end if
            
            write(*, formatting) text(1:limit_x)
        end if
        
        ! For headings, plop a second line as an underline
        if(present(heading)) then
            if(heading > 0) then
                if(limit_x > 0) then
                    if(heading == 1) then
                        heading_line = repeat("=", limit_x)
                    else
                        heading_line = repeat("-", limit_x)
                    end if
                    write(*, formatting) heading_line(1:limit_x)
                else
                    write(*,*) " "
                end if
            end if
        end if
    
    end subroutine dumb_text_draw
    
    function build_link_text(self, text, idnum) result(res)
    implicit none
        
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer, intent(in)::idnum
        character(width)::res
        
        write(res, '(A1, I3, A2)') '[', idnum,']['
        
        if(len_trim(text) < (width - 7)) then
            res = trim(res)//trim(text)//']'
        else
            res = trim(res)//text(1:(width-7))//']'
        end if
    
    end function build_link_text
    
    function dumb_link_width(self, text)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text
        integer::dumb_link_width
        
        character(width)::link
    
        link = build_link_text(self, text, 0)
        
        dumb_link_width = dumb_text_width(self, link)
    
    end function dumb_link_width
    
    subroutine dumb_link_draw(self, text, url)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text, url
        integer::i
        character(width)::link
    
        i = store_link(self, url)    
        link = build_link_text(self, text, i)
        
        call dumb_text_draw(self, link)
        
    end subroutine dumb_link_draw
    
    function dumb_request_input(self, question, answer)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::question
        character(*), intent(out)::answer
        logical::dumb_request_input
        
        ! Line Feed
        Print *, " "
        
        answer = " "
        
        Print *, question
        Write(*, '(1X, A3)', advance="no") "=> "
        Read *, answer
        
        ! Line Feed
        Print *, " "
        
        dumb_request_input = (len_trim(answer) > 0)
        
    end function dumb_request_input
    
    subroutine dumb_draw_error(self, text)
    implicit none
        
        class(dumb_renderer)::self
        character(*), intent(in)::text
        
        ! Line Feed
        Print *, " "
        
        Print *, "ERROR: "//trim(text)

        ! Line Feed
        Print *, " "
    
    end subroutine dumb_draw_error
    
    subroutine dumb_draw_status(self, text)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text
        
        Print *, "*** "//trim(text)//" ***"
        
    end subroutine dumb_draw_status
    
    subroutine dumb_ready_status(self)
    implicit none
    
        class(dumb_renderer)::self
        
        ! Override to do nothing
        
    end subroutine dumb_ready_status
    
    subroutine prompt_user(input)
    implicit none
    
        character(*), intent(out)::input
    
        write(*, '(A68)', advance='no') "*** [A]/[Z] PgUp/PgDn | [#] Link | [B] Back | [U] URL | [Q] Quit => "
        read(*, *) input
        
    end subroutine prompt_user
    
    function dumb_action(self, text)
    use render, only: render_action_none, render_action_layout, &
                      render_action_goto, render_action_quit
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(out)::text
        integer::dumb_action
        
        character(256)::input
        integer::link_id, iostatus
        
        call prompt_user(input)
        
        if(len_trim(input) == 0) then
            dumb_action = render_action_none
        else if(trim(input) == "a" .or. trim(input) == "A") then
            self%first_line = max(self%first_line - height, 0)
            dumb_action = render_action_layout
        else if(trim(input) == "z" .or. trim(input) == "Z") then
            self%first_line = self%first_line + height
            dumb_action = render_action_layout
        else if(trim(input) == "b" .or. trim(input) == "B") then
            dumb_action = render_action_back
        else if(trim(input) == "q" .or. trim(input) == "Q") then
            dumb_action = render_action_quit
        else if(trim(input) == "u" .or. trim(input) == "U") then
            write(*, '(A5)', advance='no') "URL: "
            read(*,'(A75)') text
            if(len_trim(text) == 0) then
                dumb_action = render_action_layout
            else
                dumb_action = render_action_goto
            end if
        else
            read(input, '(I3)', iostat=iostatus) link_id
            if(iostatus == 0 .and. link_id >= 1 .and. link_id <= self%link_index) then
                text = self%link_urls(link_id)
                dumb_action = render_action_goto
            else
                Print *, "Error in command: "//trim(input)
                dumb_action = render_action_none
            end if
        end if
        
    end function dumb_action
    
    function dumb_request_save_filename(self, url, mimetype, filename)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::url
        character(*), intent(in)::mimetype
        character(*), intent(out)::filename
        logical::dumb_request_save_filename
    
        Print *, "*** Response type is "//trim(mimetype)
        Write(*, '(1X, A26)', advance='no') "*** Save file as: "
    
        Read(*, *) filename
    
        dumb_request_save_filename = (len_trim(filename) > 0)
    
    end function dumb_request_save_filename
    
    subroutine dumb_displayed_page(self, text)
    implicit none
    
        class(dumb_renderer)::self
        character(*), intent(in)::text
        
        ! Nothing to do in this renderer
        
    end subroutine dumb_displayed_page
    
end module dumb_render