Landin library reference discuss source

core/io/fake/terminals.ldn

1--  Terminals a test gives, the fake of `io.terminals`.
2
3import core/io
4
5--- One terminal of `terminals_over`'s fake: the file it is and its size.
6public screen: type = struct
7    stream: io.file
8    size: io.window
9end screen
10
11screens_value: type = struct
12    screens: []screen
13end screens_value
14
15--- Terminals a test gives: each `screen` is a terminal of its size, and
16--- every other file is no terminal.  A provider of `terminals`; copies
17--- share the screens.
18public terminals: type = screens_value
19
20--- Terminals over `screens`, which the handle lends; keep them alive while
21--- it is used.
22public terminals_over: (screens: []screen)
23                       -> (host: terminals from screens) =
24    host = (screens: screens)
25end terminals_over
26
27memory_is_terminal: (self: terminals, stream: io.file) -> (yes: bool) =
28    yes = false
29    for index in usize(0)..<lenof self.screens do
30        if self.screens[index].stream == stream then yes = true end if
31    end for
32end memory_is_terminal
33
34memory_window_size: (self: terminals, stream: io.file)
35                    -> (size: io.window) ! io.not_a_terminal | io.io_failed =
36    size = (rows: 0, columns: 0)
37    for index in usize(0)..<lenof self.screens do
38        if self.screens[index].stream == stream then
39            size = self.screens[index].size
40            return
41        end if
42    end for
43    fail io.not_a_terminal
44end memory_window_size
45
46terminals is io.terminals
47    (is_terminal: memory_is_terminal, window_size: memory_window_size)