Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved README and examples, added nix flake #13

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/tests-nix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
on:
pull_request:
workflow_dispatch:

# this cancels workflows currently in progress if you start a new one
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test-examples-nix:
runs-on: [ubuntu-22.04]
steps:
- uses: actions/checkout@v3

# install nix
- uses: cachix/install-nix-action@v23
with:
nix_path: nixpkgs=channel:nixos-unstable

# Run all tests
- name: Run all tests
run: nix develop -c sh -c 'export ROC=roc && ./ci/all_tests.sh'

- name: Run all tests and capture output to check for crash later
run: nix develop -c sh -c 'export ROC=roc && ./ci/all_tests.sh 2>&1 | tee all_tests_output.log'

# Workaround for https://github.com/roc-lang/roc/issues/6688
- name: Check if crash occurred
run: grep -qv "crashed" all_tests_output.log
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ concurrency:

jobs:

test-examples:
runs-on: [ubuntu-20.04]
test-examples-ubuntu:
runs-on: [ubuntu-22.04]
steps:
- uses: actions/checkout@v3

Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Ignore the example binaries
examples/simple
examples/visualWidth
examples/nrOfCodePoints
examples/getVisualWidth
example/splitGraphemes

# Ignore the generated files
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Work in progress!
Convenient functions for working with unicode.

Someday this will be a useful collection of Unicode operations, but for right now it's extremely WIP.
⚠️ This package has only gone through limited testing. [Make an issue](https://github.com/roc-lang/unicode/issues) when you hit a bug.

:eyes: [**examples**](https://github.com/roc-lang/unicode/tree/main/examples)

:book: **documentation**: TODO


## Learning about Unicode

The string/unicode rabbit hole goes deep, we have a [good overview](https://www.roc-lang.org/builtins/Str) (scroll to the unicode section).
11 changes: 6 additions & 5 deletions examples/visualWidth.roc → examples/getVisualWidth.roc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import unicode.CodePoint

word = "世界"

visualWidth : Result U32 CodePoint.Utf8ParseErr
visualWidth =
word
## Get the display width (in amount of characters) of a Str
getVisualWidth : Str -> Result U32 CodePoint.Utf8ParseErr
getVisualWidth = \str ->
str
|> Str.toUtf8
|> CodePoint.parseUtf8
|> Result.map (\lst -> List.map lst CodePoint.visualWidth)
|> Result.map List.sum

main =
when visualWidth is
when (getVisualWidth word) is
Ok width -> Stdout.line "\n\nThe word $(word) will be displayed with the width of $(Num.toStr width) characters on most UIs.\n\n"
Err _ -> crash "ERROR: Unable to parse $(word)!"

expect visualWidth == Ok 4
expect (getVisualWidth word) == Ok 4
24 changes: 24 additions & 0 deletions examples/nrOfCodePoints.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
unicode: "../package/main.roc", # use release URL (ends in tar.br) for local example, see github.com/roc/unicode/releases
}

import pf.Stdout
import pf.Task
import unicode.CodePoint exposing [Utf8ParseErr]

## Get the number of code points for a given Str
nrOfCodePoints : Str -> Result U64 Utf8ParseErr
nrOfCodePoints = \str ->
str |> Str.toUtf8 |> CodePoint.parseUtf8 |> Result.map List.len

main =
word = "ẇ͓̞͒͟͡ǫ̠̠̉̏͠͡ͅr̬̺͚̍͛̔͒͢d̠͎̗̳͇͆̋̊͂͐"

when nrOfCodePoints word is
Ok nr ->
Stdout.line "String \"$(word)\" consists of $(Num.toStr nr) code points."

Err _ ->
Task.err (Exit 1 "Failed to parse string $(word) as Utf8.")

18 changes: 0 additions & 18 deletions examples/simple.roc

This file was deleted.

186 changes: 186 additions & 0 deletions flake.lock

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

39 changes: 39 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
description = "Unicode package devShell flake";

inputs = {
roc.url = "github:roc-lang/roc";
nixpkgs.follows = "roc/nixpkgs";

# to easily make configs for multiple architectures
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, roc }:
let supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-darwin" "aarch64-linux" ];
in flake-utils.lib.eachSystem supportedSystems (system:
let
pkgs = import nixpkgs { inherit system; };
rocPkgs = roc.packages.${system};

linuxInputs = with pkgs;
lib.optionals stdenv.isLinux [
];

darwinInputs = with pkgs;
lib.optionals stdenv.isDarwin
(with pkgs.darwin.apple_sdk.frameworks; [
]);

sharedInputs = (with pkgs; [
rocPkgs.cli
]);
in {

devShell = pkgs.mkShell {
buildInputs = sharedInputs ++ darwinInputs ++ linuxInputs;
};

formatter = pkgs.nixpkgs-fmt;
});
}
Loading