Switch to new toss commit
This commit is contained in:
parent
11422801b0
commit
93cc2a6c13
12 changed files with 62 additions and 80 deletions
|
|
@ -79,6 +79,6 @@ impl<M: Msg> TreeView<M> {
|
|||
let blocks = self
|
||||
.layout_blocks(store, cursor.as_ref(), frame, size)
|
||||
.await;
|
||||
Self::render_blocks(frame, pos, size, &blocks);
|
||||
Self::render_blocks(frame, pos, size, blocks);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use crossterm::style::ContentStyle;
|
||||
use toss::styled::Styled;
|
||||
|
||||
use crate::chat::Cursor;
|
||||
|
||||
|
|
@ -24,9 +24,8 @@ impl<I> Block<I> {
|
|||
id: I,
|
||||
indent: usize,
|
||||
time: DateTime<Utc>,
|
||||
nick: String,
|
||||
nick_style: ContentStyle,
|
||||
lines: Vec<String>,
|
||||
nick: Styled,
|
||||
lines: Vec<Styled>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
|
|
@ -35,11 +34,7 @@ impl<I> Block<I> {
|
|||
indent,
|
||||
time: Some(time),
|
||||
cursor: false,
|
||||
body: BlockBody::Msg(MsgBlock {
|
||||
nick,
|
||||
nick_style,
|
||||
lines,
|
||||
}),
|
||||
body: BlockBody::Msg(MsgBlock { nick, lines }),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -66,9 +61,8 @@ pub enum BlockBody {
|
|||
}
|
||||
|
||||
pub struct MsgBlock {
|
||||
pub nick: String,
|
||||
pub nick_style: ContentStyle,
|
||||
pub lines: Vec<String>,
|
||||
pub nick: Styled,
|
||||
pub lines: Vec<Styled>,
|
||||
}
|
||||
|
||||
/// Pre-layouted messages as a sequence of blocks.
|
||||
|
|
|
|||
|
|
@ -13,14 +13,14 @@ fn msg_to_block<M: Msg>(frame: &mut Frame, size: Size, msg: &M, indent: usize) -
|
|||
let nick = msg.nick();
|
||||
let content = msg.content();
|
||||
|
||||
let content_width = size.width as i32 - util::after_nick(frame, indent, &nick);
|
||||
let content_width = size.width as i32 - util::after_nick(frame, indent, &nick.text());
|
||||
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, msg.nick_style(), lines)
|
||||
let breaks = frame.wrap(&content.text(), content_width);
|
||||
let lines = content.split_at_indices(&breaks);
|
||||
Block::msg(msg.id(), indent, msg.time(), nick, lines)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
//! Rendering blocks to a [`Frame`].
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use crossterm::style::ContentStyle;
|
||||
use toss::frame::{Frame, Pos, Size};
|
||||
use toss::styled::Styled;
|
||||
|
||||
use crate::store::Msg;
|
||||
|
||||
|
|
@ -24,46 +24,39 @@ fn render_time(frame: &mut Frame, x: i32, y: i32, cursor: bool, time: Option<Dat
|
|||
|
||||
if let Some(time) = time {
|
||||
let time = format!("{}", time.format(TIME_FORMAT));
|
||||
frame.write(pos, &time, style);
|
||||
frame.write(pos, (&time, style));
|
||||
} else {
|
||||
frame.write(pos, TIME_EMPTY, style);
|
||||
frame.write(pos, (TIME_EMPTY, style));
|
||||
}
|
||||
}
|
||||
|
||||
fn render_indent(frame: &mut Frame, x: i32, y: i32, cursor: bool, indent: usize) {
|
||||
for i in 0..indent {
|
||||
let pos = Pos::new(x + util::after_indent(i), y);
|
||||
let style = if cursor {
|
||||
style_indent_inverted()
|
||||
} else {
|
||||
style_indent()
|
||||
};
|
||||
|
||||
let style = if cursor {
|
||||
style_indent_inverted()
|
||||
} else {
|
||||
style_indent()
|
||||
};
|
||||
|
||||
frame.write(pos, INDENT, style);
|
||||
let mut styled = Styled::default();
|
||||
for _ in 0..indent {
|
||||
styled = styled.then((INDENT, style));
|
||||
}
|
||||
|
||||
frame.write(Pos::new(x + util::after_indent(0), y), styled);
|
||||
}
|
||||
|
||||
fn render_nick(
|
||||
frame: &mut Frame,
|
||||
x: i32,
|
||||
y: i32,
|
||||
indent: usize,
|
||||
nick: &str,
|
||||
nick_style: ContentStyle,
|
||||
) {
|
||||
fn render_nick(frame: &mut Frame, x: i32, y: i32, indent: usize, nick: Styled) {
|
||||
let nick_pos = Pos::new(x + util::after_indent(indent), y);
|
||||
let inner_nick_pos = Pos::new(nick_pos.x + 1, nick_pos.y);
|
||||
frame.write(nick_pos, &format!("[{nick}]"), ContentStyle::default());
|
||||
frame.write(inner_nick_pos, nick, nick_style);
|
||||
let styled = Styled::new("[").and_then(nick).then("]");
|
||||
frame.write(nick_pos, styled);
|
||||
}
|
||||
|
||||
fn render_block<M: Msg>(frame: &mut Frame, pos: Pos, size: Size, block: &Block<M::Id>) {
|
||||
match &block.body {
|
||||
fn render_block<M: Msg>(frame: &mut Frame, pos: Pos, size: Size, block: Block<M::Id>) {
|
||||
match block.body {
|
||||
BlockBody::Msg(msg) => {
|
||||
let after_nick = util::after_nick(frame, block.indent, &msg.nick);
|
||||
let after_nick = util::after_nick(frame, block.indent, &msg.nick.text());
|
||||
|
||||
for (i, line) in msg.lines.iter().enumerate() {
|
||||
for (i, line) in msg.lines.into_iter().enumerate() {
|
||||
let y = pos.y + block.line + i as i32;
|
||||
if y < 0 || y >= pos.y + size.height as i32 {
|
||||
continue;
|
||||
|
|
@ -72,7 +65,7 @@ fn render_block<M: Msg>(frame: &mut Frame, pos: Pos, size: Size, block: &Block<M
|
|||
if i == 0 {
|
||||
render_indent(frame, pos.x, y, block.cursor, block.indent);
|
||||
render_time(frame, pos.x, y, block.cursor, block.time);
|
||||
render_nick(frame, pos.x, y, block.indent, &msg.nick, msg.nick_style);
|
||||
render_nick(frame, pos.x, y, block.indent, msg.nick.clone());
|
||||
} else {
|
||||
render_indent(frame, pos.x, y, false, block.indent + 1);
|
||||
render_indent(frame, pos.x, y, block.cursor, block.indent);
|
||||
|
|
@ -80,7 +73,7 @@ fn render_block<M: Msg>(frame: &mut Frame, pos: Pos, size: Size, block: &Block<M
|
|||
}
|
||||
|
||||
let line_pos = Pos::new(pos.x + after_nick, y);
|
||||
frame.write(line_pos, line, ContentStyle::default());
|
||||
frame.write(line_pos, line);
|
||||
}
|
||||
}
|
||||
BlockBody::Placeholder => {
|
||||
|
|
@ -88,14 +81,14 @@ fn render_block<M: Msg>(frame: &mut Frame, pos: Pos, size: Size, block: &Block<M
|
|||
render_time(frame, pos.x, y, block.cursor, block.time);
|
||||
render_indent(frame, pos.x, y, block.cursor, block.indent);
|
||||
let pos = Pos::new(pos.x + util::after_indent(block.indent), y);
|
||||
frame.write(pos, PLACEHOLDER, style_placeholder());
|
||||
frame.write(pos, (PLACEHOLDER, style_placeholder()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: Msg> TreeView<M> {
|
||||
pub fn render_blocks(frame: &mut Frame, pos: Pos, size: Size, layout: &Blocks<M::Id>) {
|
||||
for block in &layout.blocks {
|
||||
pub fn render_blocks(frame: &mut Frame, pos: Pos, size: Size, layout: Blocks<M::Id>) {
|
||||
for block in layout.blocks {
|
||||
render_block::<M>(frame, pos, size, block);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use crossterm::style::{ContentStyle, Stylize};
|
|||
use log::{Level, Log};
|
||||
use parking_lot::Mutex;
|
||||
use tokio::sync::mpsc;
|
||||
use toss::styled::Styled;
|
||||
|
||||
use crate::store::{Msg, MsgStore, Path, Tree};
|
||||
|
||||
|
|
@ -33,22 +34,20 @@ impl Msg for LogMsg {
|
|||
self.time
|
||||
}
|
||||
|
||||
fn nick(&self) -> String {
|
||||
format!("{}", self.level)
|
||||
}
|
||||
|
||||
fn nick_style(&self) -> ContentStyle {
|
||||
match self.level {
|
||||
fn nick(&self) -> Styled {
|
||||
let style = match self.level {
|
||||
Level::Error => ContentStyle::default().bold().red(),
|
||||
Level::Warn => ContentStyle::default().bold().yellow(),
|
||||
Level::Info => ContentStyle::default().bold().green(),
|
||||
Level::Debug => ContentStyle::default().bold().blue(),
|
||||
Level::Trace => ContentStyle::default().bold().magenta(),
|
||||
}
|
||||
};
|
||||
let text = format!("{}", self.level);
|
||||
Styled::new((text, style))
|
||||
}
|
||||
|
||||
fn content(&self) -> String {
|
||||
self.content.clone()
|
||||
fn content(&self) -> Styled {
|
||||
Styled::new(&self.content)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#![warn(clippy::use_self)]
|
||||
|
||||
// TODO Clean up use and manipulation of toss Pos and Size
|
||||
|
||||
mod chat;
|
||||
mod euph;
|
||||
mod logger;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::hash::Hash;
|
|||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use crossterm::style::ContentStyle;
|
||||
use toss::styled::Styled;
|
||||
|
||||
pub trait Msg {
|
||||
type Id: Clone + Debug + Hash + Eq + Ord;
|
||||
|
|
@ -12,9 +12,8 @@ pub trait Msg {
|
|||
fn parent(&self) -> Option<Self::Id>;
|
||||
|
||||
fn time(&self) -> DateTime<Utc>;
|
||||
fn nick(&self) -> String;
|
||||
fn nick_style(&self) -> ContentStyle;
|
||||
fn content(&self) -> String;
|
||||
fn nick(&self) -> Styled;
|
||||
fn content(&self) -> Styled;
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use crossterm::style::ContentStyle;
|
||||
use parking_lot::FairMutex;
|
||||
use tokio::sync::mpsc;
|
||||
use toss::frame::{Frame, Pos, Size};
|
||||
|
|
@ -80,8 +79,8 @@ impl EuphRoom {
|
|||
// Clear area in case something accidentally wrote on it already
|
||||
let size = frame.size();
|
||||
for x in 0..size.width as i32 {
|
||||
frame.write(Pos::new(x, 0), " ", ContentStyle::default());
|
||||
frame.write(Pos::new(x, 1), "─", ContentStyle::default());
|
||||
frame.write(Pos::new(x, 0), " ");
|
||||
frame.write(Pos::new(x, 1), "─");
|
||||
}
|
||||
|
||||
// Write status
|
||||
|
|
@ -104,7 +103,7 @@ impl EuphRoom {
|
|||
}
|
||||
}
|
||||
};
|
||||
frame.write(Pos::new(0, 0), &status, ContentStyle::default());
|
||||
frame.write(Pos::new(0, 0), status);
|
||||
}
|
||||
|
||||
pub async fn handle_key_event(
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ impl Rooms {
|
|||
};
|
||||
|
||||
for x in 0..size.width {
|
||||
frame.write(Pos::new(x.into(), y), " ", style);
|
||||
frame.write(Pos::new(x.into(), y), (" ", style));
|
||||
}
|
||||
let suffix = if let Some(room) = self.euph_rooms.get(room) {
|
||||
if room.connected() {
|
||||
|
|
@ -157,7 +157,7 @@ impl Rooms {
|
|||
""
|
||||
};
|
||||
let room_str = format!("&{room}{suffix}");
|
||||
frame.write(Pos::new(0, y), &room_str, style);
|
||||
frame.write(Pos::new(0, y), (&room_str, style));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
use std::mem;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use crossterm::style::ContentStyle;
|
||||
use chrono::{DateTime, Utc};
|
||||
use rusqlite::types::{FromSql, FromSqlError, ToSqlOutput, Value, ValueRef};
|
||||
use rusqlite::{named_params, params, Connection, OptionalExtension, ToSql, Transaction};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use toss::styled::Styled;
|
||||
|
||||
use crate::euph;
|
||||
use crate::euph::api::{Message, Snowflake, Time};
|
||||
|
|
@ -63,16 +63,12 @@ impl Msg for EuphMsg {
|
|||
self.time.0
|
||||
}
|
||||
|
||||
fn nick(&self) -> String {
|
||||
self.nick.clone()
|
||||
fn nick(&self) -> Styled {
|
||||
(&self.nick, euph::nick_style(&self.nick)).into()
|
||||
}
|
||||
|
||||
fn nick_style(&self) -> ContentStyle {
|
||||
euph::nick_style(&self.nick)
|
||||
}
|
||||
|
||||
fn content(&self) -> String {
|
||||
self.content.trim().to_string()
|
||||
fn content(&self) -> Styled {
|
||||
self.content.trim().into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue