Skip to content

Commit

Permalink
update Plane methods and crate examples
Browse files Browse the repository at this point in the history
- update `Plane` methods to only require a shared reference to `Notcurses`: `from_cli`, `new_at`, `new_sized`, `new_sized_at`.
- update examples: `visual`, `hello-world`, `cli`, `info`, `input`.
  • Loading branch information
joseluis committed Oct 3, 2024
1 parent 926a441 commit 8d41da2
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use notcurses::*;

fn main() -> NotcursesResult<()> {
let mut nc = Notcurses::new_cli()?;
let nc = Notcurses::new_cli()?;
let mut cli = nc.cli_plane()?;

putstrln![cli, "{cli:?}"]?;
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use notcurses::*;

fn main() -> NotcursesResult<()> {
let mut nc = Notcurses::new_cli()?;
let nc = Notcurses::new_cli()?;
let mut cli = nc.cli_plane()?;
cli.putstrln("\nhello world!")?;
cli.render()?;
Expand Down
2 changes: 1 addition & 1 deletion examples/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use notcurses::*;

fn main() -> NotcursesResult<()> {
let mut nc = Notcurses::new_cli()?;
let nc = Notcurses::new_cli()?;
let mut cli = nc.cli_plane()?;

let caps = nc.capabilities();
Expand Down
4 changes: 2 additions & 2 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::time::Instant;
use std::{thread::sleep, time::Duration};

fn main() -> NotcursesResult<()> {
let mut nc = Notcurses::new()?;
let nc = Notcurses::new()?;
nc.mice_enable(MiceEvents::All)?;

let mut plane = Plane::new(&mut nc)?;
let mut plane = Plane::new(&nc)?;
plane.set_scrolling(true);

// blocking
Expand Down
10 changes: 5 additions & 5 deletions examples/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use notcurses::*;

fn main() -> NotcursesResult<()> {
let mut nc = Notcurses::new_cli()?;
let nc = Notcurses::new_cli()?;

// # constructors

// create a root plane at (1, 1), with a child at (2, 2)
let mut rootp = Plane::new_at(&mut nc, (1, 1))?;
let mut rootp = Plane::new_at(&nc, (1, 1))?;
let child = rootp.new_child_at((2, 2))?;

// check their position relative to their parent
Expand All @@ -29,7 +29,7 @@ fn main() -> NotcursesResult<()> {
// create a square of Size::new(5, 5) at Position::new(10, 10)
let size = Size::new(5, 5);
let top_left = Position::new(10, 10);
let p1 = Plane::new_sized_at(&mut nc, size, top_left)?;
let p1 = Plane::new_sized_at(&nc, size, top_left)?;

// check top-left and bottom-right square coordinates are inside the plane:
assert_eq![p1.translate_root(top_left), (Position::new(0, 0), true)];
Expand All @@ -44,8 +44,8 @@ fn main() -> NotcursesResult<()> {
// ...

// # strings
// let mut p1 = Plane::new(&mut nc)?;
let mut p1 = Plane::new_sized(&mut nc, (4, 4))?;
// let mut p1 = Plane::new(&nc)?;
let mut p1 = Plane::new_sized(&nc, (4, 4))?;
p1.set_scrolling(true);

assert_eq!["hello world".len() as u32, p1.putstr("hello world")?];
Expand Down
12 changes: 6 additions & 6 deletions examples/visual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const W: u32 = 10;
const NUMPIX: usize = (H * W) as usize;

fn main() -> NotcursesResult<()> {
let mut nc = Notcurses::new_cli()?;
let nc = Notcurses::new_cli()?;

// Create a byte buffer with random rgba pixels:
let mut rng = rand::thread_rng();
Expand All @@ -32,19 +32,19 @@ fn main() -> NotcursesResult<()> {
visual.set_blitter_pixel();

// Blit the visual to a new plane:
let mut new_plane = visual.blit(&mut nc)?;
let mut new_plane = visual.blit(&nc)?;
new_plane.render()?;
sleep(Duration::from_millis(1000));

// Blit the visual to a pre-existing plane:
let mut existing_plane = Plane::builder().position((0, 25)).build(&mut nc)?;
visual.blit_plane(&mut nc, &mut existing_plane)?;
let mut existing_plane = Plane::builder().position((0, 25)).build(&nc)?;
visual.blit_plane(&nc, &mut existing_plane)?;
existing_plane.render()?;
sleep(Duration::from_millis(1000));

// Blit the visual into a new child plane:
let mut parent_plane = Plane::builder().position((10, 50)).build(&mut nc)?;
let mut child = visual.blit_child(&mut nc, &mut parent_plane)?;
let mut parent_plane = Plane::builder().position((10, 50)).build(&nc)?;
let mut child = visual.blit_child(&nc, &mut parent_plane)?;
parent_plane.render()?;
// child.render()?;
sleep(Duration::from_millis(1000));
Expand Down
8 changes: 4 additions & 4 deletions src/plane/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Plane {
///
/// Returns an error if there's already one *cli* plane instantiated.
#[inline]
pub fn from_cli(notcurses: &mut Notcurses) -> Result<Plane> {
pub fn from_cli(notcurses: &Notcurses) -> Result<Plane> {
notcurses.cli_plane()
}

Expand Down Expand Up @@ -115,7 +115,7 @@ impl Plane {
///
/// The plane will have the size of the terminal.
#[inline]
pub fn new_at(nc: &mut Notcurses, position: impl Into<Position>) -> Result<Self> {
pub fn new_at(nc: &Notcurses, position: impl Into<Position>) -> Result<Self> {
Self::builder().position(position).build(nc)
}

Expand All @@ -124,7 +124,7 @@ impl Plane {
/// - `size` must be greater than `0` in both dimensions.
/// - The plane will be positioned at `(0, 0)`.
#[inline]
pub fn new_sized(nc: &mut Notcurses, size: impl Into<Size>) -> Result<Self> {
pub fn new_sized(nc: &Notcurses, size: impl Into<Size>) -> Result<Self> {
Self::builder().size(size).build(nc)
}

Expand All @@ -133,7 +133,7 @@ impl Plane {
/// `size` must be greater than `0` in both dimensions.
#[inline]
pub fn new_sized_at(
nc: &mut Notcurses,
nc: &Notcurses,
size: impl Into<Size>,
position: impl Into<Position>,
) -> Result<Self> {
Expand Down

0 comments on commit 8d41da2

Please sign in to comment.