1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type Tty
17
18///|
19pub impl 
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
  loop_(Self) -> Loop
}
ToHandle
for
type Tty
Tty
with to_handle(tty :
type Tty
Tty
) ->
type Handle
Handle
= "%identity"
20 21///| 22pub impl
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
  loop_(Self) -> Loop
}
ToHandle
for
type Tty
Tty
with of_handle(handle :
type Handle
Handle
) ->
type Tty
Tty
= "%identity"
23 24///| 25pub impl
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
for
type Tty
Tty
with to_stream(tty :
type Tty
Tty
) ->
type Stream
Stream
= "%identity"
26 27///| 28pub impl
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
for
type Tty
Tty
with of_stream(stream :
type Stream
Stream
) ->
type Tty
Tty
= "%identity"
29 30///| 31extern "c" fn uv_tty_make() ->
type Tty
Tty
= "moonbit_uv_tty_make"
32 33///| 34extern "c" fn uv_tty_init(uv :
type Loop Bytes
Loop
, tty :
type Tty
Tty
, fd :
Int
Int
) ->
Int
Int
= "moonbit_uv_tty_init"
35 36///| 37pub fn
type Tty
Tty
::
(self : Loop, file : File) -> Tty raise Errno
new
(
Loop
self
:
type Loop Bytes
Loop
,
File
file
:
type File Int
File
) ->
type Tty
Tty
raise
type! Errno Status
Errno
{
38 let
Tty
tty
=
() -> Tty
uv_tty_make
()
39 let
Int
result
=
(uv : Loop, tty : Tty, fd : Int) -> Int
uv_tty_init
(
Loop
self
,
Tty
tty
,
File
file
.
(self : File) -> Int

Convert newtype to its underlying type, automatically derived.

inner
())
40 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
41 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
42 } 43
Tty
tty
44} 45 46///| 47enum TtyMode { 48
TtyMode
Normal
49
TtyMode
Raw
50
TtyMode
Io
51
TtyMode
RawVT
52} 53 54///| 55pub fn
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
::
() -> TtyMode
normal
() ->
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
{
56 TtyMode::
TtyMode
Normal
57} 58 59///| 60pub fn
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
::
() -> TtyMode
raw
() ->
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
{
61 TtyMode::
TtyMode
Raw
62} 63 64///| 65pub fn
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
::
() -> TtyMode
io
() ->
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
{
66 TtyMode::
TtyMode
Io
67} 68 69///| 70pub fn
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
::
() -> TtyMode
raw_vt
() ->
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
{
71 TtyMode::
TtyMode
RawVT
72} 73 74///| 75extern "c" fn uv_tty_set_mode(tty :
type Tty
Tty
, mode :
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
) ->
Int
Int
= "moonbit_uv_tty_set_mode"
76 77///| 78pub fn
type Tty
Tty
::
(self : Tty, mode : TtyMode) -> Unit raise Errno
set_mode
(
Tty
self
:
type Tty
Tty
,
TtyMode
mode
:
enum TtyMode {
  Normal
  Raw
  Io
  RawVT
}
TtyMode
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
79 let
Int
result
=
(tty : Tty, mode : TtyMode) -> Int
uv_tty_set_mode
(
Tty
self
,
TtyMode
mode
)
80 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
81 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
82 } 83} 84 85///| 86extern "c" fn uv_tty_reset_mode() ->
Int
Int
= "moonbit_uv_tty_reset_mode"
87 88///| 89pub fn
type Tty
Tty
::
() -> Unit raise Errno
reset_mode
() ->
Unit
Unit
raise
type! Errno Status
Errno
{
90 let
Int
result
=
() -> Int
uv_tty_reset_mode
()
91 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
92 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
93 } 94} 95 96///| 97extern "c" fn uv_tty_get_winsize( 98 tty :
type Tty
Tty
,
99 width :
type FixedArray[A]
FixedArray
[
Int
Int
],
100 height :
type FixedArray[A]
FixedArray
[
Int
Int
]
101) ->
Int
Int
= "moonbit_uv_tty_get_winsize"
102 103///| 104pub fn
type Tty
Tty
::
(self : Tty) -> (Int, Int) raise Errno
get_winsize
(
Tty
self
:
type Tty
Tty
) -> (
Int
Int
,
Int
Int
) raise
type! Errno Status
Errno
{
105 let
FixedArray[Int]
width
:
type FixedArray[A]
FixedArray
[
Int
Int
] = [0]
106 let
FixedArray[Int]
height
:
type FixedArray[A]
FixedArray
[
Int
Int
] = [0]
107 let
Int
result
=
(tty : Tty, width : FixedArray[Int], height : FixedArray[Int]) -> Int
uv_tty_get_winsize
(
Tty
self
,
FixedArray[Int]
width
,
FixedArray[Int]
height
)
108 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
109 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
110 } 111 (
FixedArray[Int]
width
[0],
FixedArray[Int]
height
[0])
112} 113