Clean up message rendering
This commit is contained in:
parent
b96ade872f
commit
8cbdc89b7e
4 changed files with 107 additions and 59 deletions
|
|
@ -5,50 +5,61 @@ use chrono::{DateTime, Utc};
|
|||
use crate::chat::Cursor;
|
||||
|
||||
pub struct Block<I> {
|
||||
pub id: I,
|
||||
pub line: i32,
|
||||
pub height: i32,
|
||||
pub id: I,
|
||||
pub indent: usize,
|
||||
pub cursor: bool,
|
||||
pub content: BlockContent,
|
||||
pub time: Option<DateTime<Utc>>,
|
||||
pub indent: usize,
|
||||
pub body: BlockBody,
|
||||
}
|
||||
|
||||
impl<I> Block<I> {
|
||||
pub fn placeholder(id: I, indent: usize) -> Self {
|
||||
pub fn msg(
|
||||
id: I,
|
||||
indent: usize,
|
||||
time: DateTime<Utc>,
|
||||
nick: String,
|
||||
lines: Vec<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
line: 0,
|
||||
height: 1,
|
||||
id,
|
||||
line: 0,
|
||||
height: lines.len() as i32,
|
||||
indent,
|
||||
time: Some(time),
|
||||
cursor: false,
|
||||
content: BlockContent::Placeholder,
|
||||
body: BlockBody::Msg(MsgBlock { nick, lines }),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn placeholder(id: I, indent: usize) -> Self {
|
||||
Self {
|
||||
id,
|
||||
line: 0,
|
||||
height: 1,
|
||||
indent,
|
||||
time: None,
|
||||
cursor: false,
|
||||
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),
|
||||
Placeholder,
|
||||
}
|
||||
|
||||
pub struct MsgBlock {
|
||||
pub time: DateTime<Utc>,
|
||||
pub nick: 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.
|
||||
///
|
||||
/// These blocks are straightforward to render, but also provide a level of
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
use crossterm::style::{ContentStyle, Stylize};
|
||||
use toss::frame::Frame;
|
||||
|
||||
pub const TIME_FORMAT: &str = "%H:%M ";
|
||||
pub const TIME_EMPTY: &str = " ";
|
||||
pub const TIME_WIDTH: usize = 6;
|
||||
|
||||
pub fn style_time() -> ContentStyle {
|
||||
|
|
@ -15,9 +17,26 @@ pub const INDENT: &str = "│ ";
|
|||
pub const INDENT_WIDTH: usize = 2;
|
||||
|
||||
pub fn style_indent() -> ContentStyle {
|
||||
ContentStyle::default().grey()
|
||||
ContentStyle::default().dark_grey()
|
||||
}
|
||||
|
||||
pub fn style_indent_inverted() -> ContentStyle {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,32 +3,30 @@ use toss::frame::{Frame, Size};
|
|||
use crate::chat::Cursor;
|
||||
use crate::store::{Msg, MsgStore, Tree};
|
||||
|
||||
use super::blocks::{Block, Blocks, MsgBlock};
|
||||
use super::constants::{INDENT_WIDTH, TIME_WIDTH};
|
||||
use super::blocks::{Block, Blocks};
|
||||
use super::constants::{self, MIN_CONTENT_WIDTH};
|
||||
use super::TreeView;
|
||||
|
||||
impl<M: Msg> TreeView<M> {
|
||||
fn msg_to_block(
|
||||
&mut self,
|
||||
msg: &M,
|
||||
indent: usize,
|
||||
frame: &mut Frame,
|
||||
size: Size,
|
||||
msg: &M,
|
||||
indent: usize,
|
||||
) -> Block<M::Id> {
|
||||
let nick = msg.nick();
|
||||
let content = msg.content();
|
||||
|
||||
let used_width = TIME_WIDTH + INDENT_WIDTH * indent + 1 + frame.width(&nick) + 2;
|
||||
let rest_width = size.width as usize - used_width;
|
||||
|
||||
let lines = toss::split_at_indices(&content, &frame.wrap(&content, rest_width));
|
||||
let lines = lines.into_iter().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||
MsgBlock {
|
||||
time: msg.time(),
|
||||
nick,
|
||||
lines,
|
||||
let content_width = size.width as i32 - constants::after_nick(frame, indent, &nick);
|
||||
if content_width < MIN_CONTENT_WIDTH as i32 {
|
||||
Block::placeholder(msg.id(), indent).time(msg.time())
|
||||
} 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<_>>();
|
||||
Block::msg(msg.id(), indent, msg.time(), nick, lines)
|
||||
}
|
||||
.into_block(msg.id(), indent)
|
||||
}
|
||||
|
||||
fn layout_subtree(
|
||||
|
|
@ -41,7 +39,7 @@ impl<M: Msg> TreeView<M> {
|
|||
layout: &mut Blocks<M::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 {
|
||||
Block::placeholder(id.clone(), indent)
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue