21 releases (10 breaking)

0.11.2 Dec 5, 2024
0.11.0 Oct 24, 2024
0.6.1 Jul 26, 2024
0.6.0 Jan 5, 2024
0.4.0 Dec 29, 2023

#2013 in Command line utilities

Download history 164/week @ 2024-08-21 236/week @ 2024-08-28 468/week @ 2024-09-04 91/week @ 2024-09-11 45/week @ 2024-09-18 80/week @ 2024-09-25 98/week @ 2024-10-02 16/week @ 2024-10-09 185/week @ 2024-10-23 35/week @ 2024-10-30 14/week @ 2024-11-06 16/week @ 2024-11-13 30/week @ 2024-11-20 17/week @ 2024-11-27 374/week @ 2024-12-04

439 downloads per month
Used in 2 crates

MIT license

52KB
645 lines

About

The sprint crate provides the Shell struct which represents a shell session in your library or CLI code and can be used for running commands:

Shell exposes its properties so you can easily create a custom shell or modify an existing shell with the settings you want.


The sprint crate also provides the sprint CLI which provides an easy way to use the library directly from the command line in three modes:

CLI examples

$ sprint -h
Command runner

Usage: sprint [OPTIONS] [STRING]...

Arguments:
  [STRING]...  File(s) or command(s)

Options:
  -s, --shell <STRING>      Shell [default: "sh -c"]
  -f, --fence <STRING>      Fence [default: ```]
  -i, --info <STRING>       Info [default: text]
  -p, --prompt <STRING>     Prompt [default: "$ "]
  -w, --watch <PATH>        Watch files/directories and rerun command on change;
                            see also `-d` option
  -d, --debounce <SECONDS>  Debounce; used only with `-w` [default: 5.0]
  -C, --color <COLOR>       Force enable/disable terminal colors [default: auto]
                            [possible values: auto, always, never]
  -h, --help                Print help
  -V, --version             Print version

Run command(s) given as arguments

$ sprint ls
```text
$ ls
Cargo.lock
Cargo.toml
CHANGELOG.md
Makefile.md
README.md
src
t
target
tests
```

Run interactively

Run in watch mode

Run a command, watch one or more file or directory paths for changes, kill the command if it is still running, and rerun the command.

Watch mode is similar to cargo-watch, watchexec, inotifywait, and other utilities except these misfire on events that don't actually modify a file's contents; sprint only runs if a watched file's contents are modified, or a file or directory is created or deleted in a watched directory.

If a command is not provided, sprint simply reports actionable changes. A .gitignore file in the current directory is used to ignore files unless given explicitly by a -w option. Use the -d option to modify the debounce time used to ignore subsequent events.

$ sprint -w src 'cargo build'
```text
$ cargo build
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
```

* Modified: `src/bin/sprint.rs`

```text
$ cargo build
   Compiling sprint v0.9.0 (/home/qtfkwk/github.com/qtfkwk/sprint)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.96s
...

Library examples

Run command(s) and show the output

use sprint::*;

let shell = Shell::default();

shell.run(&[Command::new("ls"), Command::new("ls -l")]);

// or equivalently:
//shell.run_str(&["ls", "ls -l"]);

Run command(s) and return the output

use sprint::*;

let shell = Shell::default();

let results = shell.run(&[Command {
    command: String::from("ls"),
    stdout: Pipe::string(),
    codes: vec![0],
    ..Default::default()
}]);

assert_eq!(
    results[0].stdout,
    Pipe::String(Some(String::from("\
Cargo.lock
Cargo.toml
CHANGELOG.md
Makefile.md
README.md
src
t
target
tests
\
    "))),
);

Customize

use sprint::*;

let shell = Shell {
    shell: Some(String::from("sh -c")),

    dry_run: false,
    sync: true,
    print: true,

    fence: String::from("```"),
    info: String::from("text"),
    prompt: String::from("$ "),

    fence_color: bunt::style!("#555555"),
    info_color: bunt::style!("#555555"),
    prompt_color: bunt::style!("#555555"),
    command_color: bunt::style!("#00ffff+bold"),
    error_color: bunt::style!("#ff0000+bold+italic"),
};

shell.run(&[Command::new("ls"), Command::new("ls -l")]);

Modify

use sprint::*;

let mut shell = Shell::default();

shell.shell = None;

shell.run(&[Command::new("ls"), Command::new("ls -l")]);

shell.sync = false;

shell.run(&[Command::new("ls"), Command::new("ls -l")]);

Changelog

Please find the CHANGELOG.md in the repository.

Dependencies

~10–20MB
~323K SLoC