Deno Standard Library

repository·main·Indexed 25 days ago

https://github.com/denoland/std

A collection of high-quality, reliable APIs for Deno and web environments. Includes modules for text formatting (@std/fmt), command line argument parsing (parseArgs), sensitive input handling (promptSecret), Unicode width calculation, and ANSI escape sequences for terminal manipulation. Newer versions (>= 1.0.0) are hosted on JSR, while older versions (up to 0.224.0) remain available at deno.land/std.

Tokens
8.3K
Snippets
23
Records
50
Agent score
86%

What's inside Deno Standard Library

  1. Access the Deno Standard Library

    main

    The Deno Standard Library provides high-quality APIs for Deno and the web.

    Important Versioning Note:

    • Newer versions (>= 1.0.0): Hosted on JSR.
    • Older versions (up to 0.224.0): Still available at deno.land/std.

    To use the latest modules, you should use the JSR registry.

  2. Use the @std/fmt package for text formatting

    main

    The @std/fmt library provides utilities for formatting various text types, including human-readable bytes, CLI styles (colors), time durations, and printf-style string formatting.

    Note that while most modules support all major runtimes, the printf module has Deno-specific features (such as %v, %i, and %I format specifiers) that may not be available in other runtimes.

    import { format } from "@std/fmt/bytes";
    import { red } from "@std/fmt/colors";
    
    console.log(red(format(1337))); // Prints "1.34 kB"
  3. Print formatted strings with printf

    main

    Use @std/fmt/printf for printf-style string formatting.

    Runtime Compatibility Warning: While mostly compatible with major runtimes, certain format specifiers like %v, %i, and %I are exclusive to Deno.

  4. Use ANSI escape sequences for terminal manipulation

    main

    The @std/cli/unstable-ansi module provides constants and functions to generate ANSI escape sequences. These sequences can be used to manipulate terminal behavior, such as moving the cursor, erasing text, or changing character width.

    Note: These codes do not change terminal settings automatically; they only take effect when passed to stdout or stderr. This module is browser compatible. Because this is an unstable API, terminal compatibility is not guaranteed.

    import * as Ansi from "@std/cli/unstable-ansi";
    
    console.log(Ansi.DOUBLE_HEIGHT_TOP + "Hello World!");
    console.log(Ansi.DOUBLE_HEIGHT_BOTTOM + "Hello World!");
  5. Configure SpinnerOptions

    main

    When instantiating a Spinner, you can provide a SpinnerOptions object to customize its behavior:

    OptionTypeDefaultDescription
    spinnerstring[]["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]The sequence of characters used for the animation frames.
    messagestring""The text displayed next to the spinner. Can be updated dynamically via the message property.
    intervalnumber75The delay between animation frames in milliseconds.
    colorColorundefinedThe color of the spinner. Defaults to the terminal default.
    outputDeno.stdout | Deno.stderrDeno.stdoutThe stream to which the spinner writes.
  6. Configure ProgressBarOptions

    main

    When instantiating ProgressBar, you can provide a ProgressBarOptions object to customize its behavior:

    OptionTypeDefaultDescription
    maxnumberRequiredThe total value representing 100% progress.
    writableWritableStream<Uint8Array>Deno.stderr.writableThe stream where progress reports are written.
    valuenumber0The starting offset for progress.
    barLengthnumber50The character length of the progress bar.
    fillCharstring'#'The character used to fill the bar.
    emptyCharstring'-'The character used for the remaining bar length.
    clearbooleanfalseIf true, the progress bar line is cleared from the terminal upon completion.
    formatter(formatter: ProgressBarFormatter) => stringdefaultFormatterA function to define custom visual output.
    keepOpenbooleantrueWhether to keep the writable stream open after the progress bar stops.
    intervalnumber1000The update interval in milliseconds.
  7. Customize Spinner color

    main

    The color property of a Spinner instance can be set using the Color type. This can be changed while the spinner is actively running. Providing undefined reverts the spinner to the default terminal color.

    Supported Color values:

    • black, red, green, yellow, blue, magenta, cyan, white, gray
    • Or a custom Ansi escape sequence string.
    import { Spinner } from "@std/cli/unstable-spinner";
    
    const spinner = new Spinner({ message: "Loading...", color: "yellow" });
    spinner.start();
    
    // ... do work ...
    
    // Change color dynamically
    spinner.color = "magenta";