Crates.io | storyteller |
lib.rs | storyteller |
version | 1.0.0 |
source | src |
created_at | 2022-05-05 02:26:27.516732 |
updated_at | 2024-07-24 01:28:56.124807 |
description | Library focused on supporting user oriented output while supporting multiple output types (e.g. json, a progressbar, etc.) |
homepage | |
repository | https://github.com/foresterre/storyteller |
max_upload_size | |
id | 580782 |
size | 344,188 |
A library for working with user output
This library is intended to be used by tools, such as cli's, which have multiple user interface options through which they can communicate, while also having various separate commands (or flows) which require each to carefully specify their own output formatting.
The library consists of three primary building blocks, and a default implementation on top of these building blocks. It helps you setup your program architecture
The three building blocks are:
EventHandler
: The event handler which deals with the user output, for example:
EventReporter
: Called during your program logic.
Used to communicate user output to a user. The reporter is invoked with an Event during the programs logic, so you don't have to deal with formatting and display details in the middle of the program flow.
EventListener
: Receives events, send by a reporter and runs the EventHandler
. Usually spins upa separate thread so it won't block.
On top of these building blocks, a channel based implementation is provided which runs the EventHandler
in a separate thread. To use this implementation, consult the docs for the ChannelReporter
, and the
ChannelEventListener
.
In addition to these provided elements, you have to:
impl EventHandler<Event = YourEventType>
).Click here for a larger version. (light svg, dark svg, light png, dark png)
Storyteller is used by cargo-msrv since v0.16
. To preview how events are specified, you could click around in its event module. In the handler module, several handlers can be found, such as one which writes JSON, one which prints pretty human readable output, one which prints minimal final result output used by shell commands, one which discards output and one which is used for integration testing.
use std::cell::RefCell;
use std::hash::Hasher;
use std::io::{Stderr, Write};
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{io, thread};
use storyteller::{EventHandler, FinishProcessing};
use storyteller::{
event_channel, ChannelEventListener, ChannelReporter, EventListener, EventReporter,
};
#[derive(serde::Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum Event {
DiceThrow { throw: u8 },
YouWin,
YouLose,
}
#[derive(Default)]
struct JsonHandler;
impl EventHandler for JsonHandler {
type Event = Event;
fn handle(&self, event: Self::Event) {
let serialized_event = serde_json::to_string(&event).unwrap();
eprintln!("{}", serialized_event);
}
}
// See the test function `bar` in src/tests.rs for an example where the handler is a progress bar.
fn main() {
let (sender, receiver) = event_channel::<Event>();
// Handlers are implemented by you. Here you find one which writes jsonlines messages to stderr.
// This can be anything, for example a progress bar (see src/tests.rs for an example of this),
// a fake reporter which collects events for testing or maybe even a "MultiHandler<'h>" which
// consists of a Vec<&'h dyn EventHandler> and executes multiple handlers under the hood.
let handler = JsonHandler::default();
// This one is included with the library. It just needs to be hooked up with a channel.
let reporter = ChannelReporter::new(sender);
// This one is also included with the library. It also needs to be hooked up with a channel.
let listener = ChannelEventListener::new(receiver);
// Here we use the JsonHandler we defined above, in combination with the default `EventListener`
// and `ChannelEventListener` defined above.
//
// If we don't run the handler, we'll end up in an infinite loop, because our `reporter.disconnect()`
// below will block until it receives a Disconnect message.
let fin = listener.run_handler(Arc::new(handler));
// Now onto this program, let's play a game of dice!
for _ in 0..100 {
let dice = roll_dice();
reporter
.report_event(Event::DiceThrow { throw: dice })
.unwrap();
if dice >= 3 {
reporter.report_event(Event::YouWin).unwrap();
} else {
reporter.report_event(Event::YouLose).unwrap();
}
thread::sleep(Duration::from_millis(100))
}
// Within the ChannelReporter, the sender is dropped, thereby disconnecting the channel
// Already sent events can still be processed.
let _ = reporter.disconnect();
// To keep the processing of already sent events alive, we block the handler
let _ = fin.finish_processing();
}
static SEED: AtomicU32 = AtomicU32::new(1);
fn roll_dice() -> u8 {
let mut random = SEED.load(Ordering::SeqCst);
random ^= random << 13;
random ^= random >> 17;
random ^= random << 5;
SEED.store(random, Ordering::SeqCst);
(random % 6 + 1) as u8
}
This library is a refined implementation based on an earlier experiment. It is intended to be used by, and was developed because of, cargo-msrv which has outgrown its current user output implementation.
Contributions, feedback or other correspondence are more than welcome! Feel free to send a message or create an issue ๐.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.