tek/src/view/border.rs
2024-07-07 17:55:05 +03:00

130 lines
3.5 KiB
Rust

use crate::core::*;
pub trait BorderStyle {
const NW: &'static str = "";
const N: &'static str = "";
const NE: &'static str = "";
const E: &'static str = "";
const SE: &'static str = "";
const S: &'static str = "";
const SW: &'static str = "";
const W: &'static str = "";
fn draw (&self, buf: &mut Buffer, area: Rect) {
self.draw_horizontal(buf, area);
self.draw_vertical(buf, area);
self.draw_corners(buf, area);
}
fn draw_horizontal (&self, buf: &mut Buffer, area: Rect) {
let style = self.style();
for x in area.x..(area.x+area.width).saturating_sub(1) {
Self::N.blit(buf, x, area.y, style);
Self::S.blit(buf, x, area.y + area.height - 1, style);
}
}
fn draw_vertical (&self, buf: &mut Buffer, area: Rect) {
let style = self.style();
for y in area.y..(area.y+area.height).saturating_sub(1) {
Self::W.blit(buf, area.x, y, style);
Self::E.blit(buf, area.x + area.width - 1, y, style);
}
}
fn draw_corners (&self, buf: &mut Buffer, area: Rect) {
let style = self.style();
Self::NW.blit(buf, area.x, area.y, style);
Self::NE.blit(buf, area.x + area.width - 1, area.y, style);
Self::SW.blit(buf, area.x, area.y + area.height - 1, style);
Self::SE.blit(buf, area.x + area.width - 1, area.y + area.height - 1, style);
}
fn style (&self) -> Option<Style> {
None
}
}
pub struct Lozenge(pub Style);
impl BorderStyle for Lozenge {
const N: &'static str = "";
const S: &'static str = "";
const NW: &'static str = "";
const W: &'static str = "";
const SW: &'static str = "";
const NE: &'static str = "";
const E: &'static str = "";
const SE: &'static str = "";
fn style (&self) -> Option<Style> {
Some(self.0)
}
}
pub struct Quarter(pub Style);
impl BorderStyle for Quarter {
const N: &'static str = "";
const S: &'static str = "";
const NW: &'static str = "";
const W: &'static str = "";
const SW: &'static str = "";
const NE: &'static str = "🮇";
const E: &'static str = "🮇";
const SE: &'static str = "🮇";
fn style (&self) -> Option<Style> {
Some(self.0)
}
}
pub struct QuarterV(pub Style);
impl BorderStyle for QuarterV {
const NW: &'static str = "";
const W: &'static str = "";
const SW: &'static str = "";
const NE: &'static str = "🮇";
const E: &'static str = "🮇";
const SE: &'static str = "🮇";
fn style (&self) -> Option<Style> {
Some(self.0)
}
}
const LOZENGE: [[&'static str;3];3] = [
["", "", ""],
["", " ", ""],
["", "", ""],
];
pub fn lozenge_left (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<Style>) {
let y2 = y1 + h;
let y3 = y2.saturating_sub(1);
for y in y1..y2 {
if y == y1 {
LOZENGE[0][0]
} else if y == y3 {
LOZENGE[2][0]
} else {
LOZENGE[1][0]
}.blit(buf, x, y, style)
}
}
pub fn lozenge_right (buf: &mut Buffer, x: u16, y1: u16, h: u16, style: Option<Style>) {
let y2 = y1 + h;
let y3 = y2.saturating_sub(1);
for y in y1..y2 {
if y == y1 {
LOZENGE[0][2]
} else if y == y3 {
LOZENGE[2][2]
} else {
LOZENGE[1][2]
}.blit(buf, x, y, style)
}
}