@ -153,7 +153,34 @@ impl Buffer {
& mut self . content [ i ]
}
/// Returns the index in the Vec<Cell> for the given (x, y)
/// Returns the index in the Vec<Cell> for the given global (x, y) coordinates.
///
/// Global coordinates are offset by the Buffer's area offset (`x`/`y`).
///
/// # Examples
///
/// ```
/// # use tui::buffer::Buffer;
/// # use tui::layout::Rect;
/// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect);
/// // Global coordinates to the top corner of this buffer's area
/// assert_eq!(buffer.index_of(200, 100), 0);
/// ```
///
/// # Panics
///
/// Panics when given an coordinate that is outside of this Buffer's area.
///
/// ```should_panic
/// # use tui::buffer::Buffer;
/// # use tui::layout::Rect;
/// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect);
/// // Top coordinate is outside of the buffer in global coordinate space, as the Buffer's area
/// // starts at (200, 100).
/// buffer.index_of(0, 0); // Panics
/// ```
pub fn index_of ( & self , x : u16 , y : u16 ) -> usize {
debug_assert! (
x > = self . area . left ( ) & & x < self . area . right ( ) & & y > = self . area . top ( )
@ -166,10 +193,36 @@ impl Buffer {
( ( y - self . area . y ) * self . area . width + ( x - self . area . x ) ) as usize
}
/// Returns the coordinates of a cell given its index
/// Returns the (global) coordinates of a cell given its index
///
/// Global coordinates are offset by the Buffer's area offset (`x`/`y`).
///
/// # Examples
///
/// ```
/// # use tui::buffer::Buffer;
/// # use tui::layout::Rect;
/// let rect = Rect::new(200, 100, 10, 10);
/// let buffer = Buffer::empty(rect);
/// assert_eq!(buffer.pos_of(0), (200, 100));
/// assert_eq!(buffer.pos_of(14), (204, 101));
/// ```
///
/// # Panics
///
/// Panics when given an index that is outside the Buffer's content.
///
/// ```should_panic
/// # use tui::buffer::Buffer;
/// # use tui::layout::Rect;
/// let rect = Rect::new(0, 0, 10, 10); // 100 cells in total
/// let buffer = Buffer::empty(rect);
/// // Index 100 is the 101th cell, which lies outside of the area of this Buffer.
/// buffer.pos_of(100); // Panics
/// ```
pub fn pos_of ( & self , i : usize ) -> ( u16 , u16 ) {
debug_assert! (
i > = self . content . len ( ) ,
i < self . content . len ( ) ,
"Trying to get the coords of a cell outside the buffer: i={} len={}" ,
i ,
self . content . len ( )
@ -252,3 +305,42 @@ impl Buffer {
self . area = area ;
}
}
#[ cfg(test) ]
mod tests {
use super ::* ;
#[ test ]
fn it_translates_to_and_from_coordinates ( ) {
let rect = Rect ::new ( 200 , 100 , 50 , 80 ) ;
let buf = Buffer ::empty ( rect ) ;
// First cell is at the upper left corner.
assert_eq! ( buf . pos_of ( 0 ) , ( 200 , 100 ) ) ;
assert_eq! ( buf . index_of ( 200 , 100 ) , 0 ) ;
// Last cell is in the lower right.
assert_eq! ( buf . pos_of ( buf . content . len ( ) - 1 ) , ( 249 , 179 ) ) ;
assert_eq! ( buf . index_of ( 249 , 179 ) , buf . content . len ( ) - 1 ) ;
}
#[ test ]
#[ should_panic(expected = " outside the buffer " ) ]
fn pos_of_panics_on_out_of_bounds ( ) {
let rect = Rect ::new ( 0 , 0 , 10 , 10 ) ;
let buf = Buffer ::empty ( rect ) ;
// There are a total of 100 cells; zero-indexed means that 100 would be the 101st cell.
buf . pos_of ( 100 ) ;
}
#[ test ]
#[ should_panic(expected = " outside the buffer " ) ]
fn index_of_panics_on_out_of_bounds ( ) {
let rect = Rect ::new ( 0 , 0 , 10 , 10 ) ;
let buf = Buffer ::empty ( rect ) ;
// width is 10; zero-indexed means that 10 would be the 11th cell.
buf . index_of ( 10 , 0 ) ;
}
}