From 246cde91f970d960e8b4dec19f6ba937b7458667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Sun, 23 Aug 2020 18:27:14 +0200 Subject: [PATCH] +notcurses_align & modify ncplane_align #937 --- rust/src/nc.rs | 23 ++++++++++++++++++++--- rust/src/plane.rs | 18 ++---------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/rust/src/nc.rs b/rust/src/nc.rs index b9cc1c15d..9d0351f48 100644 --- a/rust/src/nc.rs +++ b/rust/src/nc.rs @@ -38,9 +38,10 @@ // // static inline functions to reimplement: 4 // ----------------------------------------- (done / (x) wont / remaining) -// (+) implement : 4 / 0 / 0 -// (#) unit tests: 0 / 0 / 4 +// (+) implement : 4 / 0 / 1 +// (#) unit tests: 0 / 0 / 5 // ----------------------------------------- +//+notcurses_align //+notcurses_getc_blocking //+notcurses_getc_nblock //+notcurses_stddim_yx @@ -49,7 +50,23 @@ use core::ptr::null; use crate as nc; -use nc::{ncinput, ncplane, notcurses}; +use nc::types::{Align, Input, FullMode, Plane, ALIGN_CENTER, ALIGN_LEFT}; + +/// return the offset into 'availcols' at which 'cols' ought be output given the requirements of 'align' +// TODO: TEST +#[inline] +pub fn notcurses_align(availcols: i32, align: Align, cols: i32) -> i32 { + if align == ALIGN_LEFT { + return 0; + } + if cols > availcols { + return 0; + } + if align == ALIGN_CENTER { + return (availcols - cols) / 2; + } + availcols - cols // ALIGN_RIGHT +} /// 'input' may be NULL if the caller is uninterested in event details. /// If no event is ready, returns 0. diff --git a/rust/src/plane.rs b/rust/src/plane.rs index 36b3ff635..9d0b0a533 100644 --- a/rust/src/plane.rs +++ b/rust/src/plane.rs @@ -144,7 +144,7 @@ use cstr_core::CString; use crate as nc; use nc::types::{ Align, AlphaBits, Cell, Channel, ChannelPair, Color, EGCBackstop, IntResult, Plane, StyleMask, - ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, EGC, + EGC, }; /// Return the column at which 'cols' columns ought start in order to be aligned @@ -155,21 +155,7 @@ use nc::types::{ // TODO: TEST #[inline] pub fn ncplane_align(plane: &Plane, align: Align, cols: i32) -> i32 { - if align == ALIGN_LEFT { - return 0; - } - - let plane_cols = ncplane_dim_x(plane); - if cols > plane_cols { - return 0; - } - - if align == ALIGN_CENTER { - return plane_cols - cols / 2; - } else if align == ALIGN_RIGHT { - return plane_cols - cols; - } - core::i32::MAX + nc::notcurses_align(nc::ncplane_dim_x(plane), align, cols) } /// Retrieve the current contents of the cell under the cursor into 'cell'.