Stream: beginners

Topic: append to a file


view this post on Zulip Artur Swiderski (Oct 03 2023 at 08:14):

is there possibility to append text to a file, it would be extremely useful for now I am always replacing content

view this post on Zulip Luke Boswell (Oct 03 2023 at 09:39):

app "append"
    packages {
        cli: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br",
    }
    imports [
        cli.File,
        cli.Path.{ Path },
        cli.Task.{ Task },
        cli.Stdout,
    ]
    provides [main] to cli

main =
    result <- appendTask (Path.fromStr "filename.txt") (Str.toUtf8 "This is some text to append\n") |> Task.attempt

    when result is
        Ok {} -> Stdout.line "Success"
        Err msg -> Stdout.line "Failed with \(msg)"

appendTask : Path, List U8 -> Task {} Str
appendTask = \path, bytes ->
    path
    |> File.readBytes
    |> Task.await \contents ->
        File.writeBytes path (List.concat contents bytes)
    |> Task.mapErr \err ->
        when err is
            FileReadErr _ _ -> "file read error"
            FileWriteErr _ _ -> "file write error"

view this post on Zulip Luke Boswell (Oct 03 2023 at 09:41):

I tried using Cmd and doing something like echo "This is some text to append" >> filename.txt but couldn't get that working.

view this post on Zulip Luke Boswell (Oct 03 2023 at 09:41):

Maybe worth adding another function to basic-cli file module for just appending some content.

view this post on Zulip Anton (Oct 03 2023 at 09:43):

Yes, I've made an issue for it. I want to take some time to think about the best API, so I recommend downloading the basic-cli files and adding append functions yourself if you need highly performant appending, Luke's solution will be slower but will likely be fast enough for the majority of cases.

view this post on Zulip Luke Boswell (Oct 03 2023 at 09:47):

Sounds good, when you have a good idea I'm happy to take a stab at it. I would like to remove the duplication of IOError types in the InternalX.roc files and remove the redundant glue code.

view this post on Zulip Anton (Oct 03 2023 at 14:04):

@Luke Boswell for the API, I would go with separate functions:

append
appendBytes
appendUtf8

Writing to a file is so common, so I don't want to add visual or performance clutter to the existing write functions.

view this post on Zulip Luke Boswell (Oct 04 2023 at 21:14):

Thank you Anton, I've added this to my todo list, I should get to it sometime this week. I'm getting a bit distracted with another attempt at a graphics platform using zig, I feel like I'm really close this time :fingers_crossed: .

view this post on Zulip James Sully (Oct 05 2023 at 10:26):

Hiya!

⮞ roc dev
Downloading https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br
    into /home/james/.cache/roc/packages

How big is this tarball? I'm trying to figure out whether it's just taking a while or it's hanging

view this post on Zulip James Sully (Oct 05 2023 at 10:28):

ok it's medium sized, it downloads pretty quick when I fetch it in my browser. So I think this is a bug?

view this post on Zulip James Sully (Oct 05 2023 at 10:34):

I opened an issue: https://github.com/roc-lang/roc/issues/5887

view this post on Zulip Richard Feldman (Oct 06 2023 at 05:40):

:thinking: I wonder if the download is succeeding, but then something after that is hanging...can you check if the file it says it was downloading made it into that directory?

view this post on Zulip Richard Feldman (Oct 06 2023 at 05:43):

my guess is that it made it there (and that it's got the same contents as if you curl it)


Last updated: Jul 06 2025 at 12:14 UTC