Skip to content

Commit

Permalink
Merge pull request #10 from imclerran/main
Browse files Browse the repository at this point in the history
Remove backpassing from package + examples and update examples to basic-cli v0.15.0
  • Loading branch information
lukewilliamboswell authored Sep 5, 2024
2 parents 90a73f9 + 8e3817d commit 83a0524
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 59 deletions.
3 changes: 1 addition & 2 deletions examples/animals.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br",
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
ansi: "../package/main.roc",
}

import cli.Task
import cli.Stdout
import ansi.Core

Expand Down
3 changes: 1 addition & 2 deletions examples/colors.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br",
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
ansi: "../package/main.roc",
}

import cli.Task
import cli.Stdout
import ansi.Core

Expand Down
3 changes: 1 addition & 2 deletions examples/styles.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br",
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
ansi: "../package/main.roc",
}

import cli.Task
import cli.Stdout
import ansi.Core

Expand Down
51 changes: 24 additions & 27 deletions examples/text-editor.roc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.13.0/nW9yMRtZuCYf1Oa9vbE5XoirMwzLbtoSgv7NGhUlqYA.tar.br",
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
ansi: "../package/main.roc",

# TODO use unicode when https://github.com/roc-lang/roc/issues/5477 resolved
Expand All @@ -12,7 +12,6 @@ import cli.Stdin
import cli.Tty
import cli.Arg
import cli.Path
import cli.Task exposing [Task]
# Package with helpers for working with the terminal
import ansi.Core
import ansi.PieceTable
Expand Down Expand Up @@ -146,29 +145,28 @@ drawViewPort = \{ lines, lineOffset, width, height, position } -> \_, { row, col
main =

# Read path of file to edit from argument
path <- readArgFilePath |> Task.await
path = readArgFilePath!

# Check if the file exists
fileExists <-
fileExists =
Path.isFile path
|> Task.onErr \err ->
if err == PathDoesNotExist then
Task.ok Bool.false
else
Task.err UnableToCheckFile
|> Task.await
|> Task.onErr! \err ->
if err == PathDoesNotExist then
Task.ok Bool.false
else
Task.err UnableToCheckFile

# Read the file
original <- getFileContentsTask { fileExists, path } |> Task.await
original = getFileContentsTask! { fileExists, path }

# Loop UI command->update->render
{} <- Tty.enableRawMode |> Task.await
model <- Task.loop (init original path) runUILoop |> Task.await
Tty.enableRawMode! {}
model = Task.loop! (init original path) runUILoop

# Restore terminal
{} <- Stdout.write (Core.toStr Reset) |> Task.await
{} <- Tty.disableRawMode |> Task.await
{} <- Stdout.write (Core.toStr (Control (Cursor (Abs { row: 0, col: 0 })))) |> Task.await
Stdout.write! (Core.toStr Reset)
Tty.disableRawMode! {}
Stdout.write! (Core.toStr (Control (Cursor (Abs { row: 0, col: 0 }))))

Stdout.line "Finished editing $(Path.display model.filePath)"

Expand Down Expand Up @@ -199,7 +197,7 @@ runUILoop : Model -> Task.Task [Step Model, Done Model] []_
runUILoop = \prevModel ->

# Update screen size (in case it was resized since the last draw)
terminalSize <- getTerminalSize |> Task.await
terminalSize = getTerminalSize!

# Update the model with screen size and time of this draw
model = { prevModel & screen: terminalSize }
Expand All @@ -222,10 +220,10 @@ runUILoop = \prevModel ->

# Draw the screen
drawFns = render model lines
{} <- Core.drawScreen model drawFns |> Stdout.write |> Task.await
Core.drawScreen model drawFns |> Stdout.write!

# Get user input
input <- Stdin.bytes |> Task.map Core.parseRawStdin |> Task.await
input = Stdin.bytes {} |> Task.map! Core.parseRawStdin

# Parse input into a command
command =
Expand All @@ -239,6 +237,8 @@ runUILoop = \prevModel ->
Action Space -> InsertCharacter " "
Action Enter -> InsertCharacter "\n"
Symbol symbol -> InsertCharacter (Core.symbolToStr symbol)
Upper key -> InsertCharacter (Core.upperToStr key)
Lower key -> InsertCharacter (Core.lowerToStr key)
Ctrl C -> Exit
Ctrl S -> SaveChanges
Ctrl Y -> RedoChanges
Expand Down Expand Up @@ -319,9 +319,8 @@ runUILoop = \prevModel ->
|> List.join

# Write changes to file
{} <- Path.writeBytes model.filePath fileBytes
|> Task.mapErr UnableToSaveFile
|> Task.await
Path.writeBytes model.filePath fileBytes
|> Task.mapErr! UnableToSaveFile

# Reset the editor state, cleanup history and rebuild buffers
{ model2 &
Expand Down Expand Up @@ -377,16 +376,14 @@ getTerminalSize : Task.Task Core.ScreenSize []_
getTerminalSize =

# Move the cursor to bottom right corner of terminal
{} <-
[Cursor (Abs { row: 999, col: 999 }), Cursor (Position (Get))]
[Cursor (Abs { row: 999, col: 999 }), Cursor (Position (Get))]
|> List.map Control
|> List.map Core.toStr
|> Str.joinWith ""
|> Stdout.write
|> Task.await
|> Stdout.write!

# Read the cursor position
Stdin.bytes
Stdin.bytes {}
|> Task.map Core.parseCursor
|> Task.map \{ row, col } -> { width: col, height: row }

Expand Down
19 changes: 9 additions & 10 deletions examples/tui-menu.roc
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br",
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
ansi: "../package/main.roc",
}

import cli.Stdout
import cli.Stdin
import cli.Tty
import cli.Task exposing [Task]
import cli.Utc
import ansi.Core

Expand Down Expand Up @@ -54,11 +53,11 @@ render = \model ->
main =

# TUI Dashboard
Tty.enableRawMode!
Tty.enableRawMode! {}
model = Task.loop! init runUILoop
# Restore terminal
Stdout.write! (Core.toStr Reset)
Tty.disableRawMode!
Tty.disableRawMode! {}

# EXIT or RUN selected solution
when model.state is
Expand All @@ -72,7 +71,7 @@ runUILoop : Model -> Task.Task [Step Model, Done Model] _
runUILoop = \prevModel ->

# Get the time of this draw
now = Utc.now!
now = Utc.now! {}

# Update screen size (in case it was resized since the last draw)
terminalSize = getTerminalSize!
Expand All @@ -85,7 +84,7 @@ runUILoop = \prevModel ->
Core.drawScreen model drawFns |> Stdout.write!

# Get user input
input = Stdin.bytes |> Task.map! Core.parseRawStdin
input = Stdin.bytes {} |> Task.map! Core.parseRawStdin

# Parse user input into a command
command =
Expand Down Expand Up @@ -126,9 +125,9 @@ runUILoop = \prevModel ->

mapSelected : Model -> List { selected : Bool, s : Str, row : U16 }
mapSelected = \model ->
s, idx <- List.mapWithIndex model.things
row = 3 + (Num.toU16 idx)
{ selected: model.cursor.row == row, s, row }
List.mapWithIndex model.things \s, idx ->
row = 3 + (Num.toU16 idx)
{ selected: model.cursor.row == row, s, row }

getSelected : Model -> Result Str [NothingSelected]
getSelected = \model ->
Expand All @@ -145,7 +144,7 @@ getTerminalSize =
Stdout.write! cmd

# Read the cursor position
Stdin.bytes
Stdin.bytes {}
|> Task.map Core.parseCursor
|> Task.map! \{ row, col } -> { width: col, height: row }

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions package/Core.roc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module [
drawBox,
drawCursor,
symbolToStr,
lowerToStr,
upperToStr,
]

import Color exposing [Color]
Expand Down Expand Up @@ -502,16 +504,15 @@ updateCursor = \state, direction ->
drawScreen : { cursor : CursorPosition, screen : ScreenSize }*, List DrawFn -> Str
drawScreen = \{ cursor, screen }, drawFns ->
pixels =
row <- List.range { start: At 0, end: Before screen.height } |> List.map
col <- List.range { start: At 0, end: Before screen.width } |> List.map

List.walkUntil
drawFns
{ char: " ", fg: Default, bg: Default, styles: [] }
\defaultPixel, drawFn ->
when drawFn cursor { row, col } is
Ok pixel -> Break pixel
Err _ -> Continue defaultPixel
List.map (List.range { start: At 0, end: Before screen.height }) \row ->
List.map (List.range { start: At 0, end: Before screen.width }) \col ->
List.walkUntil
drawFns
{ char: " ", fg: Default, bg: Default, styles: [] }
\defaultPixel, drawFn ->
when drawFn cursor { row, col } is
Ok pixel -> Break pixel
Err _ -> Continue defaultPixel

pixels
|> joinAllPixels
Expand Down

0 comments on commit 83a0524

Please sign in to comment.