terminal: remove obsolete position.rs module
Run cargo lints / Lint on ${{ matrix.build }} (linux-amd64, ubuntu-latest, stable, x86_64-unknown-linux-gnu) (pull_request) Successful in 9m15s Details
Run Tests / Test on ${{ matrix.build }} (linux-amd64, ubuntu-latest, stable, x86_64-unknown-linux-gnu) (pull_request) Successful in 13m15s Details

The functions in terminal::position were pretty much obsolete after
commit

0e3a0c4b70 Add safe UI widget area drawing API

So this commit does a little cleanup and removes the module.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/330/head
Manos Pitsidianakis 2023-12-17 19:49:23 +02:00
parent 34a2d52e7e
commit 08518e1ca8
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
5 changed files with 107 additions and 115 deletions

View File

@ -28,8 +28,6 @@ mod color;
mod screen;
pub use color::*;
#[macro_use]
pub mod position;
#[macro_use]
pub mod cells;
#[macro_use]
pub mod keys;
@ -41,7 +39,10 @@ use std::io::{BufRead, Write};
pub use braille::BraillePixelIter;
pub use screen::{Area, Screen, ScreenGeneration, StateStdout, Tty, Virtual};
pub use self::{cells::*, keys::*, position::*, text_editing::*};
pub use self::{cells::*, keys::*, text_editing::*};
/// A type alias for a `(x, y)` position on screen.
pub type Pos = (usize, usize);
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Alignment {

View File

@ -35,7 +35,7 @@ use melib::{
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use smallvec::SmallVec;
use super::{position::*, Area, Color, ScreenGeneration};
use super::{Area, Color, Pos, ScreenGeneration};
use crate::{state::Context, ThemeAttribute};
/// In a scroll region up and down cursor movements shift the region vertically.
@ -393,6 +393,16 @@ impl CellBuffer {
return BoundsIterator::empty(self.generation());
}
#[inline(always)]
fn get_x(p: Pos) -> usize {
p.0
}
#[inline(always)]
fn get_y(p: Pos) -> usize {
p.1
}
BoundsIterator {
width: area.width(),
height: area.height(),
@ -499,20 +509,6 @@ impl CellBuffer {
/// Change foreground and background colors in an `Area`
pub fn change_colors(&mut self, area: Area, fg_color: Color, bg_color: Color) {
if cfg!(feature = "debug-tracing") {
let bounds = self.size();
let upper_left = area.upper_left();
let bottom_right = area.bottom_right();
let (x, y) = upper_left;
if y > (get_y(bottom_right))
|| x > get_x(bottom_right)
|| y >= get_y(bounds)
|| x >= get_x(bounds)
{
log::debug!("BUG: Invalid area in change_colors:\n area: {:?}", area);
return;
}
}
for row in self.bounds_iter(area) {
for c in row {
self[c].set_fg(fg_color).set_bg(bg_color);
@ -522,20 +518,6 @@ impl CellBuffer {
/// Change [`ThemeAttribute`] in an `Area`
pub fn change_theme(&mut self, area: Area, theme: ThemeAttribute) {
if cfg!(feature = "debug-tracing") {
let bounds = self.size();
let upper_left = area.upper_left();
let bottom_right = area.bottom_right();
let (x, y) = upper_left;
if y > (get_y(bottom_right))
|| x > get_x(bottom_right)
|| y >= get_y(bounds)
|| x >= get_x(bounds)
{
log::debug!("BUG: Invalid area in change_theme:\n area: {:?}", area);
return;
}
}
for row in self.bounds_iter(area) {
for c in row {
self[c]
@ -563,6 +545,16 @@ impl CellBuffer {
return dest.upper_left();
}
#[inline(always)]
fn get_x(p: Pos) -> usize {
p.0
}
#[inline(always)]
fn get_y(p: Pos) -> usize {
p.1
}
let mut ret = dest.bottom_right();
let mut src_x = get_x(src.upper_left());
let mut src_y = get_y(src.upper_left());
@ -643,6 +635,17 @@ impl CellBuffer {
if area.is_empty() {
return (0, 0);
}
#[inline(always)]
fn get_x(p: Pos) -> usize {
p.0
}
#[inline(always)]
fn get_y(p: Pos) -> usize {
p.1
}
let mut bounds = self.size();
let upper_left = area.upper_left();
let bottom_right = area.bottom_right();
@ -1706,31 +1709,30 @@ pub mod boundaries {
/// Returns the inner area of the created box.
pub fn create_box(grid: &mut CellBuffer, area: Area) -> Area {
debug_assert_eq!(grid.generation(), area.generation());
let upper_left = area.upper_left();
let bottom_right = area.bottom_right();
if !grid.ascii_drawing {
for x in get_x(upper_left)..get_x(bottom_right) {
grid[(x, get_y(upper_left))].set_ch(HORZ_BOUNDARY);
grid[(x, get_y(bottom_right))].set_ch(HORZ_BOUNDARY);
for (top, bottom) in grid
.bounds_iter(area.nth_row(0))
.zip(grid.bounds_iter(area.nth_row(area.height().saturating_sub(1))))
{
for c in top.chain(bottom) {
grid[c].set_ch(HORZ_BOUNDARY);
}
}
for y in get_y(upper_left)..get_y(bottom_right) {
grid[(get_x(upper_left), y)].set_ch(VERT_BOUNDARY);
grid[(get_x(bottom_right), y)].set_ch(VERT_BOUNDARY);
for (left, right) in grid
.bounds_iter(area.nth_col(0))
.zip(grid.bounds_iter(area.nth_col(area.width().saturating_sub(1))))
{
for c in left.chain(right) {
grid[c].set_ch(VERT_BOUNDARY);
}
}
set_and_join_box(grid, upper_left, BoxBoundary::Horizontal);
set_and_join_box(
grid,
set_x(upper_left, get_x(bottom_right)),
BoxBoundary::Horizontal,
);
set_and_join_box(
grid,
set_y(upper_left, get_y(bottom_right)),
BoxBoundary::Vertical,
);
set_and_join_box(grid, bottom_right, BoxBoundary::Vertical);
set_and_join_box(grid, area.upper_left(), BoxBoundary::Horizontal);
set_and_join_box(grid, area.upper_right(), BoxBoundary::Horizontal);
set_and_join_box(grid, area.bottom_left(), BoxBoundary::Vertical);
set_and_join_box(grid, area.bottom_right(), BoxBoundary::Vertical);
}
area.skip(1, 1).skip_rows_from_end(1).skip_cols_from_end(1)

View File

@ -1,52 +0,0 @@
/*
* meli
*
* Copyright 2017-2018 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
//! Simple type definitions and macro helper for a `(x, y)` position on the
//! terminal and the areas they define.
/// A `(x, y)` position on screen.
pub type Pos = (usize, usize);
#[inline(always)]
pub fn get_x(p: Pos) -> usize {
p.0
}
#[inline(always)]
pub fn get_y(p: Pos) -> usize {
p.1
}
#[inline(always)]
pub fn set_x(p: Pos, new_x: usize) -> Pos {
(new_x, p.1)
}
#[inline(always)]
pub fn set_y(p: Pos, new_y: usize) -> Pos {
(p.0, new_y)
}
#[inline(always)]
pub fn pos_inc(p: Pos, inc: (usize, usize)) -> Pos {
(p.0 + inc.0, p.1 + inc.1)
}
#[inline(always)]
pub fn pos_dec(p: Pos, dec: (usize, usize)) -> Pos {
(p.0.saturating_sub(dec.0), p.1.saturating_sub(dec.1))
}

View File

@ -27,9 +27,9 @@ use termion::{clear, cursor, raw::IntoRawMode, screen::AlternateScreen};
use crate::{
terminal::{
cells::CellBuffer, position::*, Alignment, BracketModeEnd, BracketModeStart, Cell, Color,
DisableMouse, DisableSGRMouse, EnableMouse, EnableSGRMouse,
RestoreWindowTitleIconFromStack, SaveWindowTitleIconToStack,
cells::CellBuffer, Alignment, BracketModeEnd, BracketModeStart, Cell, Color, DisableMouse,
DisableSGRMouse, EnableMouse, EnableSGRMouse, Pos, RestoreWindowTitleIconFromStack,
SaveWindowTitleIconToStack,
},
Attr, Context,
};
@ -891,6 +891,16 @@ impl Area {
self.bottom_right
}
#[inline]
pub const fn upper_right(&self) -> Pos {
set_x(self.upper_left, get_x(self.bottom_right))
}
#[inline]
pub const fn bottom_left(&self) -> Pos {
set_y(self.upper_left, get_y(self.bottom_right))
}
#[inline]
pub const fn offset(&self) -> Pos {
self.offset
@ -932,6 +942,31 @@ impl Area {
}
}
#[inline(always)]
const fn pos_inc(p: Pos, inc: (usize, usize)) -> Pos {
(p.0 + inc.0, p.1 + inc.1)
}
#[inline(always)]
const fn get_x(p: Pos) -> usize {
p.0
}
#[inline(always)]
const fn get_y(p: Pos) -> usize {
p.1
}
#[inline(always)]
const fn set_x(p: Pos, new_x: usize) -> Pos {
(new_x, p.1)
}
#[inline(always)]
const fn set_y(p: Pos, new_y: usize) -> Pos {
(p.0, new_y)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -979,11 +979,14 @@ impl Component for Tabbed {
}
if (children_maps == self.help_view.curr_views) && must_redraw_shortcuts {
let dialog_area = area.align_inside(
/* add box perimeter padding */
pos_inc(self.help_view.content.area().size(), (1, 1)),
/* horizontal */
// add box perimeter padding
{
let (w, h) = self.help_view.content.area().size();
(w + 1, h + 1)
},
// horizontal
Alignment::Center,
/* vertical */
// vertical
Alignment::Center,
);
context.dirty_areas.push_back(dialog_area);
@ -1154,11 +1157,14 @@ impl Component for Tabbed {
}
self.help_view.curr_views = children_maps;
let dialog_area = area.align_inside(
/* add box perimeter padding */
pos_inc(self.help_view.content.area().size(), (1, 1)),
/* horizontal */
// add box perimeter padding
{
let (w, h) = self.help_view.content.area().size();
(w + 1, h + 1)
},
// horizontal
Alignment::Center,
/* vertical */
// vertical
Alignment::Center,
);
context.dirty_areas.push_back(dialog_area);