Skip to content

Commit

Permalink
More conversions to RenderContext
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Dec 14, 2023
1 parent 54a3338 commit ed61f26
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nutmeg"
version = "0.1.5-pre"
version = "0.2.0-pre"
edition = "2021"
description = "An unopinionated progress bar library"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion examples/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nutmeg = { path = "../..", version = "0.1.5-pre" }
nutmeg = { path = "../..", version = "0.2.0-pre" }
tracing = "*"
tracing-fmt = "*"
tracing-subscriber = "*"
3 changes: 1 addition & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2023 Martin Pool.

//! Context passed from the library to the render function.
/// Context passed from the library to the render function.
pub struct RenderContext {
pub(crate) width: usize,
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ For example:
}
}
let model = Model {
let model = CountModel {
count: 0,
};
let view = Arc::new(nutmeg::View::new(model, nutmeg::Options::new()));
Expand Down Expand Up @@ -268,12 +268,12 @@ pub trait Model {
/// struct Model { i: usize, total: usize }
///
/// impl nutmeg::Model for Model {
/// fn render(&mut self, _context: &nutmeg::Context) -> String {
/// fn render(&mut self, _context: &nutmeg::RenderContext) -> String {
/// format!("phase {}/{}", self.i, self.total)
/// }
/// }
/// ```
fn render(&mut self, context: &Context) -> String;
fn render(&mut self, context: &RenderContext) -> String;

/// Optionally render a final message when the view is finished.
///
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<T> Model for T
where
T: Display,
{
fn render(&mut self, _context: &Context) -> String {
fn render(&mut self, _context: &RenderContext) -> String {
self.to_string()
}
}
Expand Down Expand Up @@ -356,7 +356,7 @@ where
/// }
///
/// impl nutmeg::Model for Model {
/// fn render(&mut self, _context: &nutmeg::Context) -> String {
/// fn render(&mut self, _context: &nutmeg::RenderContext) -> String {
/// format!("i={}", self.i)
/// }
/// }
Expand Down Expand Up @@ -754,7 +754,7 @@ impl<M: Model> InnerView<M> {
}
}
if let Some(width) = self.options.destination.width() {
let context = Context { width };
let context = RenderContext { width };
let mut rendered = self.model.render(&context);
if rendered.ends_with('\n') {
// Handle models that incorrectly add a trailing newline, rather than
Expand Down
10 changes: 5 additions & 5 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::{Duration, Instant};

#[allow(unused)] // For docstrings
use crate::View;
use crate::{estimate_remaining, percent_done, Model};
use crate::{estimate_remaining, percent_done, Model, RenderContext};

/// A Nutmeg progress model that concatenates a pair of strings to render
/// the progress bar.
Expand Down Expand Up @@ -58,7 +58,7 @@ impl StringPair {
}

impl Model for StringPair {
fn render(&mut self, _context: &nutmeg::RenderContext) -> String {
fn render(&mut self, _context: &RenderContext) -> String {
format!("{}{}", self.prefix, self.suffix)
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ impl LinearModel {
}

impl Model for LinearModel {
fn render(&mut self, _context: &nutmeg::RenderContext) -> String {
fn render(&mut self, _context: &RenderContext) -> String {
format!(
"{}: {}/{}, {}, {} remaining",
self.message,
Expand Down Expand Up @@ -185,7 +185,7 @@ impl UnboundedModel {
}

impl Model for UnboundedModel {
fn render(&mut self, _context: &nutmeg::RenderContext) -> String {
fn render(&mut self, _context: &RenderContext) -> String {
format!(
"{}: {} in {}",
self.message,
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<T, R> Model for BasicModel<T, R>
where
R: FnMut(&mut T) -> String,
{
fn render(&mut self, _context: &nutmeg::RenderContext) -> String {
fn render(&mut self, _context: &RenderContext) -> String {
(self.render_fn)(&mut self.value)
}
}
2 changes: 1 addition & 1 deletion tests/api/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn default_width_when_not_on_stdout() {
impl nutmeg::Model for Model {
fn render(&mut self, context: &nutmeg::RenderContext) -> String {
assert_eq!(context.width(), 80);
format!("width={width}")
format!("width={}", context.width())
}
}
let model = Model();
Expand Down

0 comments on commit ed61f26

Please sign in to comment.