Clean up message rendering

This commit is contained in:
Joscha 2022-06-14 10:26:27 +02:00
parent b96ade872f
commit 8cbdc89b7e
4 changed files with 107 additions and 59 deletions

View file

@ -4,14 +4,18 @@ mod layout;
use std::marker::PhantomData; use std::marker::PhantomData;
use chrono::{DateTime, Utc};
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent};
use crossterm::style::{ContentStyle, Stylize}; use crossterm::style::{ContentStyle, Stylize};
use toss::frame::{Frame, Pos, Size}; use toss::frame::{Frame, Pos, Size};
use crate::store::{Msg, MsgStore}; use crate::store::{Msg, MsgStore};
use self::blocks::{BlockContent, Blocks}; use self::blocks::{BlockBody, Blocks};
use self::constants::{INDENT, INDENT_WIDTH, TIME_WIDTH}; use self::constants::{
after_indent, style_indent, style_indent_inverted, style_placeholder, style_time,
style_time_inverted, INDENT, INDENT_WIDTH, PLACEHOLDER, TIME_EMPTY, TIME_FORMAT, TIME_WIDTH,
};
use super::Cursor; use super::Cursor;
@ -29,23 +33,42 @@ impl<M: Msg> TreeView<M> {
} }
} }
fn render_indentation(&mut self, frame: &mut Frame, pos: Pos, indent: usize, cursor: bool) { fn render_time(frame: &mut Frame, x: i32, y: i32, time: Option<DateTime<Utc>>, cursor: bool) {
for i in 0..indent { let pos = Pos::new(x, y);
let x = TIME_WIDTH + INDENT_WIDTH * i;
let pos = Pos::new(pos.x + x as i32, pos.y);
let style = if cursor { let style = if cursor {
ContentStyle::default().black().on_white() style_time_inverted()
} else { } else {
ContentStyle::default() style_time()
}; };
if let Some(time) = time {
let time = format!("{}", time.format(TIME_FORMAT));
frame.write(pos, &time, style);
} else {
frame.write(pos, TIME_EMPTY, style);
}
}
fn render_indent(frame: &mut Frame, x: i32, y: i32, indent: usize, cursor: bool) {
for i in 0..indent {
let pos = Pos::new(x + after_indent(i), y);
let style = if cursor {
style_indent_inverted()
} else {
style_indent()
};
frame.write(pos, INDENT, style); frame.write(pos, INDENT, style);
} }
} }
fn render_layout(&mut self, frame: &mut Frame, pos: Pos, size: Size, layout: &Blocks<M::Id>) { fn render_layout(&mut self, frame: &mut Frame, pos: Pos, size: Size, layout: &Blocks<M::Id>) {
for block in &layout.blocks { for block in &layout.blocks {
match &block.content { // Draw rest of block
BlockContent::Msg(msg) => { match &block.body {
BlockBody::Msg(msg) => {
let nick_width = frame.width(&msg.nick) as i32; let nick_width = frame.width(&msg.nick) as i32;
for (i, line) in msg.lines.iter().enumerate() { for (i, line) in msg.lines.iter().enumerate() {
let y = pos.y + block.line + i as i32; let y = pos.y + block.line + i as i32;
@ -53,29 +76,26 @@ impl<M: Msg> TreeView<M> {
continue; continue;
} }
self.render_indentation( Self::render_indent(frame, pos.x, y, block.indent, block.cursor);
frame,
Pos::new(pos.x, y),
block.indent,
block.cursor,
);
let after_indent = let after_indent =
pos.x + (TIME_WIDTH + INDENT_WIDTH * block.indent) as i32; pos.x + (TIME_WIDTH + INDENT_WIDTH * block.indent) as i32;
if i == 0 { if i == 0 {
let time = format!("{}", msg.time.format("%H:%M")); Self::render_time(frame, pos.x, y, block.time, block.cursor);
frame.write(Pos::new(pos.x, y), &time, ContentStyle::default());
let nick = format!("[{}]", msg.nick); let nick = format!("[{}]", msg.nick);
frame.write(Pos::new(after_indent, y), &nick, ContentStyle::default()); frame.write(Pos::new(after_indent, y), &nick, ContentStyle::default());
} else {
Self::render_time(frame, pos.x, y, None, block.cursor);
} }
let msg_x = after_indent + 1 + nick_width + 2; let msg_x = after_indent + 1 + nick_width + 2;
frame.write(Pos::new(msg_x, y), line, ContentStyle::default()); frame.write(Pos::new(msg_x, y), line, ContentStyle::default());
} }
} }
BlockContent::Placeholder => { BlockBody::Placeholder => {
self.render_indentation(frame, pos, block.indent, block.cursor);
let x = pos.x + (TIME_WIDTH + INDENT_WIDTH * block.indent) as i32;
let y = pos.y + block.line; let y = pos.y + block.line;
frame.write(Pos::new(x, y), "[...]", ContentStyle::default()); Self::render_time(frame, pos.x, y, block.time, block.cursor);
Self::render_indent(frame, pos.x, y, block.indent, block.cursor);
let pos = Pos::new(pos.x + after_indent(block.indent), y);
frame.write(pos, PLACEHOLDER, style_placeholder());
} }
} }
} }

View file

@ -5,50 +5,61 @@ use chrono::{DateTime, Utc};
use crate::chat::Cursor; use crate::chat::Cursor;
pub struct Block<I> { pub struct Block<I> {
pub id: I,
pub line: i32, pub line: i32,
pub height: i32, pub height: i32,
pub id: I,
pub indent: usize,
pub cursor: bool, pub cursor: bool,
pub content: BlockContent, pub time: Option<DateTime<Utc>>,
pub indent: usize,
pub body: BlockBody,
} }
impl<I> Block<I> { impl<I> Block<I> {
pub fn msg(
id: I,
indent: usize,
time: DateTime<Utc>,
nick: String,
lines: Vec<String>,
) -> Self {
Self {
id,
line: 0,
height: lines.len() as i32,
indent,
time: Some(time),
cursor: false,
body: BlockBody::Msg(MsgBlock { nick, lines }),
}
}
pub fn placeholder(id: I, indent: usize) -> Self { pub fn placeholder(id: I, indent: usize) -> Self {
Self { Self {
id,
line: 0, line: 0,
height: 1, height: 1,
id,
indent, indent,
time: None,
cursor: false, cursor: false,
content: BlockContent::Placeholder, body: BlockBody::Placeholder,
} }
} }
pub fn time(mut self, time: DateTime<Utc>) -> Self {
self.time = Some(time);
self
} }
pub enum BlockContent { }
pub enum BlockBody {
Msg(MsgBlock), Msg(MsgBlock),
Placeholder, Placeholder,
} }
pub struct MsgBlock { pub struct MsgBlock {
pub time: DateTime<Utc>,
pub nick: String, pub nick: String,
pub lines: Vec<String>, pub lines: Vec<String>,
} }
impl MsgBlock {
pub fn into_block<I>(self, id: I, indent: usize) -> Block<I> {
Block {
line: 0,
height: self.lines.len() as i32,
id,
indent,
cursor: false,
content: BlockContent::Msg(self),
}
}
}
/// Pre-layouted messages as a sequence of blocks. /// Pre-layouted messages as a sequence of blocks.
/// ///
/// These blocks are straightforward to render, but also provide a level of /// These blocks are straightforward to render, but also provide a level of

View file

@ -1,6 +1,8 @@
use crossterm::style::{ContentStyle, Stylize}; use crossterm::style::{ContentStyle, Stylize};
use toss::frame::Frame;
pub const TIME_FORMAT: &str = "%H:%M "; pub const TIME_FORMAT: &str = "%H:%M ";
pub const TIME_EMPTY: &str = " ";
pub const TIME_WIDTH: usize = 6; pub const TIME_WIDTH: usize = 6;
pub fn style_time() -> ContentStyle { pub fn style_time() -> ContentStyle {
@ -15,9 +17,26 @@ pub const INDENT: &str = "│ ";
pub const INDENT_WIDTH: usize = 2; pub const INDENT_WIDTH: usize = 2;
pub fn style_indent() -> ContentStyle { pub fn style_indent() -> ContentStyle {
ContentStyle::default().grey() ContentStyle::default().dark_grey()
} }
pub fn style_indent_inverted() -> ContentStyle { pub fn style_indent_inverted() -> ContentStyle {
ContentStyle::default().black().on_white() ContentStyle::default().black().on_white()
} }
pub const PLACEHOLDER: &str = "[...]";
pub fn style_placeholder() -> ContentStyle {
ContentStyle::default().dark_grey()
}
// Something like this should fit: [+, 1234 more]
pub const MIN_CONTENT_WIDTH: usize = 14;
pub fn after_indent(indent: usize) -> i32 {
(TIME_WIDTH + indent * INDENT_WIDTH) as i32
}
pub fn after_nick(frame: &mut Frame, indent: usize, nick: &str) -> i32 {
after_indent(indent) + 1 + frame.width(nick) as i32 + 2
}

View file

@ -3,32 +3,30 @@ use toss::frame::{Frame, Size};
use crate::chat::Cursor; use crate::chat::Cursor;
use crate::store::{Msg, MsgStore, Tree}; use crate::store::{Msg, MsgStore, Tree};
use super::blocks::{Block, Blocks, MsgBlock}; use super::blocks::{Block, Blocks};
use super::constants::{INDENT_WIDTH, TIME_WIDTH}; use super::constants::{self, MIN_CONTENT_WIDTH};
use super::TreeView; use super::TreeView;
impl<M: Msg> TreeView<M> { impl<M: Msg> TreeView<M> {
fn msg_to_block( fn msg_to_block(
&mut self, &mut self,
msg: &M,
indent: usize,
frame: &mut Frame, frame: &mut Frame,
size: Size, size: Size,
msg: &M,
indent: usize,
) -> Block<M::Id> { ) -> Block<M::Id> {
let nick = msg.nick(); let nick = msg.nick();
let content = msg.content(); let content = msg.content();
let used_width = TIME_WIDTH + INDENT_WIDTH * indent + 1 + frame.width(&nick) + 2; let content_width = size.width as i32 - constants::after_nick(frame, indent, &nick);
let rest_width = size.width as usize - used_width; if content_width < MIN_CONTENT_WIDTH as i32 {
Block::placeholder(msg.id(), indent).time(msg.time())
let lines = toss::split_at_indices(&content, &frame.wrap(&content, rest_width)); } else {
let content_width = content_width as usize;
let lines = toss::split_at_indices(&content, &frame.wrap(&content, content_width));
let lines = lines.into_iter().map(|s| s.to_string()).collect::<Vec<_>>(); let lines = lines.into_iter().map(|s| s.to_string()).collect::<Vec<_>>();
MsgBlock { Block::msg(msg.id(), indent, msg.time(), nick, lines)
time: msg.time(),
nick,
lines,
} }
.into_block(msg.id(), indent)
} }
fn layout_subtree( fn layout_subtree(
@ -41,7 +39,7 @@ impl<M: Msg> TreeView<M> {
layout: &mut Blocks<M::Id>, layout: &mut Blocks<M::Id>,
) { ) {
let block = if let Some(msg) = tree.msg(id) { let block = if let Some(msg) = tree.msg(id) {
self.msg_to_block(msg, indent, frame, size) self.msg_to_block(frame, size, msg, indent)
} else { } else {
Block::placeholder(id.clone(), indent) Block::placeholder(id.clone(), indent)
}; };