From 93cc2a6c134d45c0ad83320f6438b128245c560f Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 4 Jul 2022 19:40:11 +0200 Subject: [PATCH] Switch to new toss commit --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/chat/tree.rs | 2 +- src/chat/tree/blocks.rs | 18 +++++-------- src/chat/tree/layout.rs | 8 +++--- src/chat/tree/render.rs | 57 ++++++++++++++++++----------------------- src/logger.rs | 17 ++++++------ src/main.rs | 2 ++ src/store.rs | 7 +++-- src/ui/room.rs | 7 +++-- src/ui/rooms.rs | 4 +-- src/vault/euph.rs | 16 +++++------- 12 files changed, 62 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c48caa5..5a6871d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1120,7 +1120,7 @@ dependencies = [ [[package]] name = "toss" version = "0.1.0" -source = "git+https://github.com/Garmelon/toss.git?rev=761519c1a7cdc950eab70fd6539c71bf22919a50#761519c1a7cdc950eab70fd6539c71bf22919a50" +source = "git+https://github.com/Garmelon/toss.git?rev=ee9d6018c0efb5e6eb02db94dff7483d33b96a43#ee9d6018c0efb5e6eb02db94dff7483d33b96a43" dependencies = [ "crossterm", "unicode-linebreak", diff --git a/Cargo.toml b/Cargo.toml index 6568aef..75b0833 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,4 @@ features = ["rustls-tls-native-roots"] [dependencies.toss] git = "https://github.com/Garmelon/toss.git" -rev = "761519c1a7cdc950eab70fd6539c71bf22919a50" +rev = "ee9d6018c0efb5e6eb02db94dff7483d33b96a43" diff --git a/src/chat/tree.rs b/src/chat/tree.rs index 6123599..dba8488 100644 --- a/src/chat/tree.rs +++ b/src/chat/tree.rs @@ -79,6 +79,6 @@ impl TreeView { 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); } } diff --git a/src/chat/tree/blocks.rs b/src/chat/tree/blocks.rs index 05e582c..64eca2d 100644 --- a/src/chat/tree/blocks.rs +++ b/src/chat/tree/blocks.rs @@ -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 Block { id: I, indent: usize, time: DateTime, - nick: String, - nick_style: ContentStyle, - lines: Vec, + nick: Styled, + lines: Vec, ) -> Self { Self { id, @@ -35,11 +34,7 @@ impl Block { 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, + pub nick: Styled, + pub lines: Vec, } /// Pre-layouted messages as a sequence of blocks. diff --git a/src/chat/tree/layout.rs b/src/chat/tree/layout.rs index 6149035..5760676 100644 --- a/src/chat/tree/layout.rs +++ b/src/chat/tree/layout.rs @@ -13,14 +13,14 @@ fn msg_to_block(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::>(); - 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) } } diff --git a/src/chat/tree/render.rs b/src/chat/tree/render.rs index 2518e25..218b3b8 100644 --- a/src/chat/tree/render.rs +++ b/src/chat/tree/render.rs @@ -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(frame: &mut Frame, pos: Pos, size: Size, block: &Block) { - match &block.body { +fn render_block(frame: &mut Frame, pos: Pos, size: Size, block: Block) { + 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(frame: &mut Frame, pos: Pos, size: Size, block: &Block(frame: &mut Frame, pos: Pos, size: Size, block: &Block { @@ -88,14 +81,14 @@ fn render_block(frame: &mut Frame, pos: Pos, size: Size, block: &Block TreeView { - pub fn render_blocks(frame: &mut Frame, pos: Pos, size: Size, layout: &Blocks) { - for block in &layout.blocks { + pub fn render_blocks(frame: &mut Frame, pos: Pos, size: Size, layout: Blocks) { + for block in layout.blocks { render_block::(frame, pos, size, block); } } diff --git a/src/logger.rs b/src/logger.rs index 7aec22f..4913b6f 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -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) } } diff --git a/src/main.rs b/src/main.rs index f6a299a..9aa259e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/store.rs b/src/store.rs index 019c4ce..9973322 100644 --- a/src/store.rs +++ b/src/store.rs @@ -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; fn time(&self) -> DateTime; - 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)] diff --git a/src/ui/room.rs b/src/ui/room.rs index f6162d8..bad7070 100644 --- a/src/ui/room.rs +++ b/src/ui/room.rs @@ -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( diff --git a/src/ui/rooms.rs b/src/ui/rooms.rs index 3ac78df..74a1f3f 100644 --- a/src/ui/rooms.rs +++ b/src/ui/rooms.rs @@ -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)); } } diff --git a/src/vault/euph.rs b/src/vault/euph.rs index 2214030..4cc69fe 100644 --- a/src/vault/euph.rs +++ b/src/vault/euph.rs @@ -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() } }