aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/console.lux
blob: 80e8eaebb65f474f4cbc8e7d809acb2984befb39 (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
(.module:
  [library
   [lux #*
    [ffi (#+ import:)]
    ["@" target]
    [abstract
     [monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]
     ["." io (#+ IO io)]
     [concurrency
      ["." async (#+ Async)]
      ["." atom]]]
    [data
     ["." text (#+ Char)
      ["%" format (#+ format)]]]]])

(interface: .public (Console !)
  {#.doc (example "An interface to console/terminal I/O.")}
  
  (: (-> [] (! (Try Char)))
     read)
  (: (-> [] (! (Try Text)))
     read_line)
  (: (-> Text (! (Try Any)))
     write)
  (: (-> [] (! (Try Any)))
     close))

(def: .public (async console)
  (-> (Console IO) (Console Async))
  (`` (implementation
       (~~ (template [<capability>]
             [(def: <capability>
                (|>> (\ console <capability>) async.future))]

             [read]
             [read_line]
             [write]
             [close])))))

(with_expansions [<jvm> (as_is (import: java/lang/String)

                               (import: java/io/Console
                                 ["#::."
                                  (readLine [] #io #try java/lang/String)])

                               (import: java/io/InputStream
                                 ["#::."
                                  (read [] #io #try int)])

                               (import: java/io/PrintStream
                                 ["#::."
                                  (print [java/lang/String] #io #try void)])

                               (import: java/lang/System
                                 ["#::."
                                  (#static console [] #io #? java/io/Console)
                                  (#static in java/io/InputStream)
                                  (#static out java/io/PrintStream)])

                               (template [<name>]
                                 [(exception: .public (<name>)
                                    "")]

                                 [cannot_open]
                                 [cannot_close]
                                 )

                               (def: .public default
                                 (IO (Try (Console IO)))
                                 (do io.monad
                                   [?jvm_console (java/lang/System::console)]
                                   (case ?jvm_console
                                     #.None
                                     (in (exception.except ..cannot_open []))

                                     (#.Some jvm_console)
                                     (let [jvm_input (java/lang/System::in)
                                           jvm_output (java/lang/System::out)]
                                       (<| in
                                           #try.Success
                                           (: (Console IO)) ... TODO: Remove ASAP
                                           (implementation
                                            (def: (read _)
                                              (|> jvm_input
                                                  java/io/InputStream::read
                                                  (\ (try.with io.monad) map .nat)))
                                            
                                            (def: (read_line _)
                                              (java/io/Console::readLine jvm_console))
                                            
                                            (def: (write message)
                                              (java/io/PrintStream::print message jvm_output))
                                            
                                            (def: close
                                              (|>> (exception.except ..cannot_close) in)))))))))]
  (for {@.old (as_is <jvm>)
        @.jvm (as_is <jvm>)}
       (as_is)))

(def: .public (write_line message console)
  {#.doc (example "Writes the message on the console and appends a new-line/line-feed at the end.")}
  (All [!] (-> Text (Console !) (! (Try Any))))
  (\ console write (format message text.new_line)))

(interface: .public (Mock s)
  {#.doc (example "A mock/simulation of a console."
                  "Useful for testing.")}
  
  (: (-> s (Try [s Char]))
     on_read)
  (: (-> s (Try [s Text]))
     on_read_line)
  (: (-> Text s (Try s))
     on_write)
  (: (-> s (Try s))
     on_close))

(def: .public (mock mock init)
  (All [s] (-> (Mock s) s (Console IO)))
  (let [state (atom.atom init)]
    (`` (implementation
         (~~ (template [<method> <mock>]
               [(def: (<method> _)
                  (do {! io.monad}
                    [|state| (atom.read! state)]
                    (case (\ mock <mock> |state|)
                      (#try.Success [|state| output])
                      (do !
                        [_ (atom.write! |state| state)]
                        (in (#try.Success output)))
                      
                      (#try.Failure error)
                      (in (#try.Failure error)))))]

               [read on_read]
               [read_line on_read_line]
               ))

         (def: (write input)
           (do {! io.monad}
             [|state| (atom.read! state)]
             (case (\ mock on_write input |state|)
               (#try.Success |state|)
               (do !
                 [_ (atom.write! |state| state)]
                 (in (#try.Success [])))
               
               (#try.Failure error)
               (in (#try.Failure error)))))

         (def: (close _)
           (do {! io.monad}
             [|state| (atom.read! state)]
             (case (\ mock on_close |state|)
               (#try.Success |state|)
               (do !
                 [_ (atom.write! |state| state)]
                 (in (#try.Success [])))
               
               (#try.Failure error)
               (in (#try.Failure error)))))
         ))))