Skip to content

Commit

Permalink
Support gleam_stdlib v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tynanbe committed Feb 12, 2024
1 parent 43a4c84 commit c085ba6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:

- uses: erlef/setup-beam@v1
with:
otp-version: "25"
otp-version: "26"
rebar3-version: "3"
gleam-version: "0.33"
gleam-version: "1.0.0-rc1"

- uses: denoland/setup-deno@v1
with:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Shellout now supports `gleam_stdlib` v1.0.
- Shellout now requires Gleam v0.34 or later.

## v1.5.0 - 2023-12-19

- Shellout now requires Gleam v0.33 or later.
Expand Down
6 changes: 3 additions & 3 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name = "shellout"
version = "1.5.0"
version = "1.6.0-dev"
description = "A Gleam library for cross-platform shell operations"
licences = ["Apache-2.0"]
gleam = ">= 0.33.0"
gleam = ">= 0.34.0"

[repository]
repo = "shellout"
Expand All @@ -14,7 +14,7 @@ href = "https://gleam.run/"
title = "Website"

[dependencies]
gleam_stdlib = "~> 0.34"
gleam_stdlib = "~> 0.34 or ~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
Expand Down
4 changes: 2 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

packages = [
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "4E75DCF846D653848094169304743DFFB386E3AECCCF611F99ADB735FF4D4DD9" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
]

[requirements]
gleam_stdlib = { version = "~> 0.34" }
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
30 changes: 8 additions & 22 deletions src/shellout.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub const colors: Lookup = [
/// with: display(["bold", "italic", "tubular"]),
/// custom: [],
/// )
/// // -> "\u{001b}[1;3mradical\u{001b}[0m\u{001b}[K"
/// // -> "\u{1b}[1;3mradical\u{1b}[0m\u{1b}[K"
/// ```
///
pub fn display(values: List(String)) -> StyleFlags {
Expand All @@ -130,7 +130,7 @@ pub fn display(values: List(String)) -> StyleFlags {
/// with: color(["yellow", "brightgreen", "gnarly"]),
/// custom: [],
/// )
/// // -> "\u{001b}[33;92muh...\u{001b}[0m\u{001b}[K"
/// // -> "\u{1b}[33;92muh...\u{1b}[0m\u{1b}[K"
/// ```
///
pub fn color(values: List(String)) -> StyleFlags {
Expand All @@ -148,7 +148,7 @@ pub fn color(values: List(String)) -> StyleFlags {
/// with: background(["yellow", "brightgreen", "bodacious"]),
/// custom: [],
/// )
/// // -> "\u{001b}[43;102mawesome\u{001b}[0m\u{001b}[K"
/// // -> "\u{1b}[43;102mawesome\u{1b}[0m\u{1b}[K"
/// ```
///
pub fn background(values: List(String)) -> StyleFlags {
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn background(values: List(String)) -> StyleFlags {
/// |> dict.merge(background(["brightblack", "excellent"])),
/// custom: lookups,
/// )
/// // -> "\u{001b}[1;3;38;2;255;175;243;100mcowabunga\u{001b}[0m\u{001b}[K"
/// // -> "\u{1b}[1;3;38;2;255;175;243;100mcowabunga\u{1b}[0m\u{1b}[K"
/// ```
///
pub fn style(
Expand Down Expand Up @@ -210,15 +210,10 @@ pub fn style(
|> escape(string)
}

@target(erlang)
fn escape(code: String, string: String) -> String {
string.concat(["\u{001b}[", code, "m", string, "\u{001b}[0m\u{001b}[K"])
"\u{1b}[" <> code <> "m" <> string <> "\u{1b}[0m\u{1b}[K"
}

@target(javascript)
@external(javascript, "./shellout_ffi.mjs", "escape")
fn escape(code: String, string: String) -> String

type Style {
Name(String)
Rgb(List(String))
Expand Down Expand Up @@ -261,8 +256,7 @@ fn do_style(lookup: Lookup, strings: List(String), flag: String) -> List(String)
let assert [Rgb(values), ..styles] = acc.styles
StyleAcc(
styles: [Rgb([item, ..values]), ..styles],
rgb_counter: rgb_counter
+ 1,
rgb_counter: rgb_counter + 1,
)
}
_ -> StyleAcc(styles: [Rgb([item]), ..acc.styles], rgb_counter: 1)
Expand Down Expand Up @@ -451,13 +445,9 @@ fn do_command(
/// // Ugh, shell shock ...
/// ```
///
pub fn exit(status: Int) -> Nil {
do_exit(status)
}

@external(erlang, "shellout_ffi", "os_exit")
@external(javascript, "./shellout_ffi.mjs", "os_exit")
fn do_exit(status status: Int) -> Nil
pub fn exit(status: Int) -> Nil

/// Results in a path to the given `executable` on success, or an `Error` when
/// no such path is found.
Expand All @@ -479,10 +469,6 @@ fn do_exit(status status: Int) -> Nil
/// // -> Error("command `dimension_x` not found")
/// ```
///
pub fn which(executable: String) -> Result(String, String) {
do_which(executable)
}

@external(erlang, "shellout_ffi", "os_which")
@external(javascript, "./shellout_ffi.mjs", "os_which")
fn do_which(command: String) -> Result(String, String)
pub fn which(executable: String) -> Result(String, String)
13 changes: 4 additions & 9 deletions src/shellout_ffi.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Error, Ok, toList } from "./gleam.mjs";
import { LetBeStderr, LetBeStdout, OverlappedStdio } from "./shellout.mjs";
import * as child_process from "node:child_process";
import * as fs from "node:fs";
import { spawnSync } from "node:child_process";
import { statSync } from "node:fs";
import * as path from "node:path";
import process from "node:process";

Expand Down Expand Up @@ -43,10 +43,6 @@ const Signals = {
SIGRTMIN: 34,
};

export function escape(code, string) {
return `\x1b[${code}m${string}\x1b[0m\x1b[K`;
}

export function start_arguments() {
return toList(globalThis.Deno?.args ?? process.argv.slice(1));
}
Expand Down Expand Up @@ -96,7 +92,7 @@ export function os_command(command, args, dir, opts) {
result.status = result.code ?? null;
} else {
spawnOpts.stdio = [stdin, stdout, stderr];
result = child_process.spawnSync(command, args, spawnOpts);
result = spawnSync(command, args, spawnOpts);
}
if (result.error) {
result = { status: null };
Expand Down Expand Up @@ -141,8 +137,7 @@ export function os_which(command) {
.concat([command])
.flatMap((item) => pathexts.map((ext) => item + ext));
let result = paths.map(
(item) =>
fs.statSync(item, { throwIfNoEntry: false })?.isFile() ? item : Nil,
(item) => statSync(item, { throwIfNoEntry: false })?.isFile() ? item : Nil,
).find((item) => item !== Nil);
return result !== Nil ? new Ok(result) : new Error(
`command \`${command}\` not found\n`,
Expand Down
10 changes: 5 additions & 5 deletions test/shellout_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ pub fn arguments_test() {
}

pub fn command_test() {
let echo = shellout.command(run: "echo", with: [message], in: ".", opt: _)
let print = shellout.command(run: "echo", with: [message], in: ".", opt: _)

let assert Ok(output) = echo([])
let assert Ok(output) = print([])
output
|> should.not_equal("")

let assert Ok(new_output) = echo([LetBeStderr])
let assert Ok(new_output) = print([LetBeStderr])
new_output
|> should.equal(output)

Expand Down Expand Up @@ -100,8 +100,8 @@ pub fn style_test() {
message
|> shellout.style(
with: shellout.display(["bold", "italic"])
|> dict.merge(shellout.color(["pink"]))
|> dict.merge(shellout.background(["brightblack"])),
|> dict.merge(shellout.color(["pink"]))
|> dict.merge(shellout.background(["brightblack"])),
custom: lookups,
)
styled
Expand Down

0 comments on commit c085ba6

Please sign in to comment.