styling
This commit is contained in:
+74
-28
@@ -5,11 +5,10 @@
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use crate::ui::utils::{create_frame, render_to_frame};
|
||||
///
|
||||
///
|
||||
/// let frame = create_frame(50, 10);
|
||||
/// render_to_frame(crate::State::Normal, 50, &mut frame, &text_element);
|
||||
/// ```
|
||||
|
||||
use crate::{Element, Value};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
@@ -49,7 +48,7 @@ pub fn compress_string(input: &str, re: &Regex) -> (String, HashMap<usize, Strin
|
||||
}
|
||||
|
||||
/// Merges a frame withe a line by x
|
||||
fn merge_line(frame_: &str, line_: &str, x: usize) -> String {
|
||||
fn merge_line(frame_: &str, line_: &str, x: usize) -> (String, String) {
|
||||
let (frame_, fm) = compress_string(frame_, &ANSI);
|
||||
let (mut line_, lm) = compress_string(line_, &ANSI);
|
||||
|
||||
@@ -82,35 +81,86 @@ fn merge_line(frame_: &str, line_: &str, x: usize) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
(res, line_)
|
||||
}
|
||||
|
||||
/// Render to a frame
|
||||
pub fn render_to_frame(
|
||||
focused: bool,
|
||||
pub struct Frame {
|
||||
frame: Vec<String>,
|
||||
width: usize,
|
||||
frame: &mut Vec<String>,
|
||||
element: &Element,
|
||||
) {
|
||||
let data = element.get_data();
|
||||
for (i, line) in element.render(focused).split('\n').enumerate() {
|
||||
if (data.1 + i) < frame.len() {
|
||||
let frame_line = frame.get_mut(data.1 + i).unwrap();
|
||||
match data.0 {
|
||||
Value::Custom(x) => {
|
||||
*frame_line = merge_line(&frame_line, line, x);
|
||||
used: Vec<usize>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn new(width: usize, height: usize) -> Self {
|
||||
Self {
|
||||
frame: vec![" ".repeat(width); height],
|
||||
width,
|
||||
used: vec![0; height],
|
||||
}
|
||||
}
|
||||
pub fn render(&mut self, focused: bool, element: &Element) {
|
||||
// let data = element.get_data();
|
||||
let output = element.render(focused);
|
||||
let x;
|
||||
let mut y = 0;
|
||||
match output.1 .1 {
|
||||
crate::ui::Number::Px(_y) => {
|
||||
y = _y;
|
||||
}
|
||||
crate::ui::Number::Pe(p) => y = (self.frame.len() * p) / 100,
|
||||
crate::ui::Number::Center => {
|
||||
y = (self.frame.len() - output.0.matches('\n').count() + 1) / 2
|
||||
}
|
||||
crate::ui::Number::Auto => {
|
||||
for (i, n) in self.used.iter().enumerate() {
|
||||
if *n == 0 {
|
||||
y = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for (x, c) in frame_line.chars().rev().enumerate() {
|
||||
if c != ' ' {
|
||||
*frame_line = merge_line(&frame_line, line, width - x);
|
||||
return;
|
||||
}
|
||||
}
|
||||
crate::ui::Number::Default => {
|
||||
for (i, n) in self.used.iter().enumerate() {
|
||||
if *n == 0 {
|
||||
y = i;
|
||||
break;
|
||||
}
|
||||
*frame_line = merge_line(&frame_line, line, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
match output.1 .0 {
|
||||
crate::ui::Number::Px(_x) => {
|
||||
x = _x;
|
||||
}
|
||||
crate::ui::Number::Pe(p) => x = (self.width * p) / 100,
|
||||
crate::ui::Number::Center => {
|
||||
x = (self.width
|
||||
- ANSI
|
||||
.replace_all(&output.0.lines().next().unwrap_or(""), "")
|
||||
.len())
|
||||
/ 2;
|
||||
}
|
||||
crate::ui::Number::Auto => {
|
||||
x = *self.used.get(y).unwrap();
|
||||
}
|
||||
crate::ui::Number::Default => {
|
||||
x = 0;
|
||||
}
|
||||
}
|
||||
for (i, line) in output.0.split('\n').enumerate() {
|
||||
if self.frame.len() > y + i {
|
||||
let frame_line = self.frame.get_mut(y + i).unwrap();
|
||||
let merged = merge_line(&frame_line, line, x);
|
||||
*frame_line = merged.0;
|
||||
*self.used.get_mut(y + i).unwrap() += merged.1.len() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn output(&self) -> String {
|
||||
self.frame.join("\n")
|
||||
}
|
||||
pub fn output_nnl(&self) -> String {
|
||||
self.frame.join("")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,10 +230,6 @@ pub fn closest_component(
|
||||
.unwrap_or(current_index) // If no component is found, return the current index
|
||||
}
|
||||
|
||||
pub fn create_frame(width: usize, height: usize) -> Vec<String> {
|
||||
vec![" ".repeat(width); height]
|
||||
}
|
||||
|
||||
pub fn get_term_size() -> (usize, usize) {
|
||||
let (width, height) = crossterm::terminal::size().unwrap();
|
||||
(width as usize, height as usize)
|
||||
|
||||
Reference in New Issue
Block a user