shellout

Types

Options for controlling the behavior of command.

pub type CommandOpt {
  LetBeStderr
  LetBeStdout
  OverlappedStdio
}

Constructors

  • LetBeStderr

    Don’t capture the standard error stream, let it behave as usual.

  • LetBeStdout

    Don’t capture the standard output stream, let it behave as usual.

    When targeting Erlang, this option also implies LetBeStderr.

    When targeting JavaScript, this option also enables SIGINT (Ctrl+C) to pass through to the spawned process.

  • OverlappedStdio

    Overlap the standard input and output streams.

    This option is specific to the Windows platform and otherwise ignored; however, when targeting JavaScript, this option prevents the standard input stream from behaving as usual.

A list of tuples in which the first element of each tuple is a label and the second element is a list of one or more number strings representing a singular ANSI style.

ANSI styles are split into three categories, labeled "display", "color", and "background", primarily so a single color Lookup can work with both foreground and background.

Examples

See the displays and colors constants, and the Lookups type.

pub type Lookup =
  List(#(String, List(String)))

A list of tuples in which the first element of each tuple is a list of Lookup labels and the second element is a Lookup.

Lookups allow for customization, adding new styles to the specified Lookup categories.

Examples

pub const lookups: Lookups = [
  #(
    ["color", "background"],
    [
      #("buttercup", ["252", "226", "174"]),
      #("mint", ["182", "255", "234"]),
      #("pink", ["255", "175", "243"]),
    ],
  ),
]
pub type Lookups =
  List(#(List(String), Lookup))

A map in which the keys are style categories, "display", "color", or "background", and the values are lists of style labels found within a Lookup.

Examples

See the display, color, and background functions.

pub type StyleFlags =
  Dict(String, List(String))

Constants

pub const colors: List(#(String, List(String))) = [
  #("black", ["30"]),
  #("red", ["31"]),
  #("green", ["32"]),
  #("yellow", ["33"]),
  #("blue", ["34"]),
  #("magenta", ["35"]),
  #("cyan", ["36"]),
  #("white", ["37"]),
  #("default", ["39"]),
  #("brightblack", ["90"]),
  #("brightred", ["91"]),
  #("brightgreen", ["92"]),
  #("brightyellow", ["93"]),
  #("brightblue", ["94"]),
  #("brightmagenta", ["95"]),
  #("brightcyan", ["96"]),
  #("brightwhite", ["97"]),
]

A list of ANSI styles representing the basic 16 terminal colors, 8 standard and 8 bright.

pub const displays: List(#(String, List(String))) = [
  #("reset", ["0"]),
  #("bold", ["1"]),
  #("dim", ["2"]),
  #("italic", ["3"]),
  #("underline", ["4"]),
  #("blink", ["5"]),
  #("fastblink", ["6"]),
  #("reverse", ["7"]),
  #("hide", ["8"]),
  #("strike", ["9"]),
  #("normal", ["22"]),
  #("noitalic", ["23"]),
  #("nounderline", ["24"]),
  #("noblink", ["25"]),
  #("noreverse", ["27"]),
  #("nohide", ["28"]),
  #("nostrike", ["29"]),
]

A list of ANSI styles representing non-color display effects.

Functions

pub fn arguments() -> List(String)

Retrieves a list of strings corresponding to any extra arguments passed when invoking a runtime—via gleam run, for example.

Examples

// $ gleam run -- pizza --order=5 --anchovies=false
arguments()
// -> ["pizza", "--order=5", "--anchovies=false"]
// $ gleam run --target=javascript
arguments()
// -> []
pub fn background(
  values: List(String),
) -> Dict(String, List(String))

Converts a list of "background" style labels into a StyleFlags.

Examples

style(
  "awesome",
  with: background(["yellow", "brightgreen", "bodacious"]),
  custom: [],
)
// -> "\u{1b}[43;102mawesome\u{1b}[0m\u{1b}[K"
pub fn color(values: List(String)) -> Dict(String, List(String))

Converts a list of "color" style labels into a StyleFlags.

Examples

style(
  "uh...",
  with: color(["yellow", "brightgreen", "gnarly"]),
  custom: [],
)
// -> "\u{1b}[33;92muh...\u{1b}[0m\u{1b}[K"
pub fn command(
  run executable: String,
  with arguments: List(String),
  in directory: String,
  opt options: List(CommandOpt),
) -> Result(String, #(Int, String))

Results in any output captured from the given executable on success, or an Error on failure.

An Error result wraps a tuple in which the first element is an OS error status code and the second is a message about what went wrong (or an empty string).

The executable is given arguments and run from within the given directory.

Any number of CommandOpt options can be given to alter the behavior of this function.

The standard error stream is by default redirected to the standard output stream, and both are captured. When targeting JavaScript, anything captured from the standard error stream is appended to anything captured from the standard output stream.

The standard input stream is by default handled in raw mode when targeting JavaScript, allowing full interaction with the spawned process. When targeting Erlang, however, it’s always handled in cooked mode.

Note that while shellout aims for near feature parity between runtimes, some discrepancies exist and are documented herein.

Examples

command(run: "echo", with: ["-n", "Cool!"], in: ".", opt: [])
// -> Ok("Cool!")
command(run: "echo", with: ["Cool!"], in: ".", opt: [LetBeStdout])
// Cool!
// -> Ok("")
// $ stat -c '%a %U %n' /tmp/dimension_x
// 700 root /tmp/dimension_x
command(run: "ls", with: ["dimension_x"], in: "/tmp", opt: [])
// -> Error(#(2, "ls: cannot open directory 'dimension_x': Permission denied\n"))
command(run: "dimension_x", with: [], in: ".", opt: [])
// -> Error(#(1, "command `dimension_x` not found\n"))
// $ ls -p
// gleam.toml  manifest.toml  src/  test/
command(run: "echo", with: [], in: "dimension_x", opt: [])
// -> Error(#(2, "The directory \"dimension_x\" does not exist\n"))
pub fn display(
  values: List(String),
) -> Dict(String, List(String))

Converts a list of "display" style labels into a StyleFlags.

Examples

style(
  "radical",
  with: display(["bold", "italic", "tubular"]),
  custom: [],
)
// -> "\u{1b}[1;3mradical\u{1b}[0m\u{1b}[K"
pub fn exit(status: Int) -> Nil

Halts the runtime and passes the given status code to the operating system.

A status code of 0 typically indicates success, while any other integer represents an error.

Examples

// $ gleam run && echo "Pizza time!"
exit(0)
// Pizza time!
// $ gleam run || echo "Ugh, shell shock ..."
exit(1)
// Ugh, shell shock ...
pub fn style(
  string: String,
  with flags: Dict(String, List(String)),
  custom lookups: List(
    #(List(String), List(#(String, List(String)))),
  ),
) -> String

Applies ANSI styles to a string, resetting styling at the end.

If a style label isn’t found within a Lookup associated with the corresponding StyleFlags key’s category, that label is silently ignored.

Examples

import gleam/dict
pub const lookups: Lookups = [
  #(
    ["color", "background"],
    [
      #("buttercup", ["252", "226", "174"]),
      #("mint", ["182", "255", "234"]),
      #("pink", ["255", "175", "243"]),
    ],
  ),
]
style(
  "cowabunga",
  with: display(["bold", "italic", "awesome"])
  |> dict.merge(color(["pink", "righteous"]))
  |> dict.merge(background(["brightblack", "excellent"])),
  custom: lookups,
)
// -> "\u{1b}[1;3;38;2;255;175;243;100mcowabunga\u{1b}[0m\u{1b}[K"
pub fn which(executable: String) -> Result(String, String)

Results in a path to the given executable on success, or an Error when no such path is found.

Examples

which("echo")
// -> Ok("/sbin/echo")
which("./priv/party")
// -> Ok("./priv/party")
which("dimension_x")
// -> Error("command `dimension_x` not found")
Search Document