Skip to content

Commit 46546e9

Browse files
committed
Update to Gren summer 2024.
1 parent 664dbd2 commit 46546e9

File tree

20 files changed

+1611
-1585
lines changed

20 files changed

+1611
-1585
lines changed

cat/gren.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"source-directories": [
55
"src"
66
],
7-
"gren-version": "0.3.0",
7+
"gren-version": "0.4.0",
88
"dependencies": {
99
"direct": {
10-
"gren-lang/core": "4.0.1",
11-
"gren-lang/node": "3.0.1"
10+
"gren-lang/core": "5.0.0",
11+
"gren-lang/node": "4.0.0"
1212
},
1313
"indirect": {
14-
"gren-lang/url": "3.0.0"
14+
"gren-lang/url": "4.0.0"
1515
}
1616
}
1717
}

cat/src/Main.gren

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,32 @@ module Main exposing (main)
33
import Node
44
import Bytes exposing (Bytes)
55
import Stream exposing (Stream)
6-
import Node exposing (Program, Environment)
6+
import Node exposing (Environment)
77
import FileSystem
8+
import FileSystem.Path as Path
89
import Init
910
import Task
1011

1112

12-
main : Program Model Msg
13+
main : Node.SimpleProgram a
1314
main =
14-
Node.defineProgram
15-
{ init = init
16-
, update = update
17-
, subscriptions = \_ -> Sub.none
18-
}
15+
Node.defineSimpleProgram init
1916

2017

21-
type alias Model =
22-
{ stdout : Stream
23-
, stderr : Stream
24-
}
25-
26-
27-
type Msg
28-
= OpenResult (Result FileSystem.AccessError (FileSystem.ReadableFileHandle Never))
29-
| ReadResult (Result FileSystem.UnknownFileSystemError Bytes)
30-
31-
init : Environment -> Init.Task { model : Model, command : Cmd Msg }
18+
init : Environment -> Init.Task (Cmd a)
3219
init env =
3320
Init.await FileSystem.initialize <| \fsPermission ->
34-
Node.startProgram
35-
{ model =
36-
{ stdout = env.stdout
37-
, stderr = env.stderr
38-
}
39-
, command =
40-
case env.args of
41-
[ _, _, file ] ->
42-
FileSystem.openForRead fsPermission file
43-
|> Task.attempt OpenResult
44-
45-
_ ->
46-
Stream.sendLine env.stderr <|
47-
"Exactly one argument is required: the file name to read"
48-
}
49-
50-
update : Msg -> Model -> { model : Model, command : Cmd Msg }
51-
update msg model =
52-
case msg of
53-
OpenResult (Ok fh) ->
54-
{ model = model
55-
, command =
56-
FileSystem.read fh
57-
|> Task.attempt ReadResult
58-
}
59-
60-
OpenResult (Err error) ->
61-
{ model = model
62-
, command =
63-
Stream.sendLine model.stderr <|
64-
"Failed to open file"
65-
}
66-
67-
ReadResult (Ok bytes) ->
68-
{ model = model
69-
, command =
70-
Stream.send model.stdout bytes
71-
}
72-
73-
ReadResult (Err error) ->
74-
{ model = model
75-
, command =
76-
Stream.sendLine model.stderr <|
77-
"Failed to read file"
78-
}
79-
21+
case env.args of
22+
[ _, _, file ] ->
23+
FileSystem.readFile fsPermission (Path.fromPosixString file)
24+
|> Task.map (Bytes.toString >> Maybe.withDefault "")
25+
|> Task.onError (Task.succeed << FileSystem.errorToString)
26+
|> Task.andThen (Stream.sendLine env.stdout)
27+
|> Task.execute
28+
|> Node.endWithCmd
29+
30+
_ ->
31+
"Exactly one argument is required: the file name to read"
32+
|> Stream.sendLine env.stderr
33+
|> Task.execute
34+
|> Node.endWithCmd

0 commit comments

Comments
 (0)