Skip to content

Commit

Permalink
chore(examples):Migrated the pg-chat example to ratatui (#3385)
Browse files Browse the repository at this point in the history
* migrated the pg-chat example to ratatui

* fixed formatting mistake
  • Loading branch information
CommanderStorm authored Jul 29, 2024
1 parent cc48182 commit fd4cb99
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 33 deletions.
141 changes: 122 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/postgres/chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ workspace = "../../../"
sqlx = { path = "../../../", features = [ "postgres", "runtime-tokio-native-tls" ] }
futures = "0.3.1"
tokio = { version = "1.20.0", features = [ "rt-multi-thread", "macros" ] }
tui = "0.19.0"
crossterm = "0.25"
ratatui = "0.27.0"
crossterm = "0.27.0"
unicode-width = "0.1"
25 changes: 13 additions & 12 deletions examples/postgres/chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use sqlx::postgres::PgListener;
use sqlx::PgPool;
use std::sync::Arc;
use std::{error::Error, io};
use tokio::{sync::Mutex, time::Duration};
use tui::{
use ratatui::text::Line;
use ratatui::{
backend::{Backend, CrosstermBackend},
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans, Text},
text::{Span, Text},
widgets::{Block, Borders, List, ListItem, Paragraph},
Frame, Terminal,
};
use sqlx::postgres::PgListener;
use sqlx::PgPool;
use std::sync::Arc;
use std::{error::Error, io};
use tokio::{sync::Mutex, time::Duration};
use unicode_width::UnicodeWidthStr;

struct ChatApp {
Expand Down Expand Up @@ -53,7 +54,7 @@ impl ChatApp {
.await
.iter()
.map(|m| {
let content = vec![Spans::from(Span::raw(m.to_owned()))];
let content = vec![Line::from(Span::raw(m.to_owned()))];
ListItem::new(content)
})
.collect();
Expand Down Expand Up @@ -85,7 +86,7 @@ impl ChatApp {
}
}

fn ui<B: Backend>(&mut self, frame: &mut Frame<B>, messages: Vec<ListItem>) {
fn ui(&mut self, frame: &mut Frame, messages: Vec<ListItem>) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
Expand All @@ -99,7 +100,7 @@ impl ChatApp {
)
.split(frame.size());

let text = Text::from(Spans::from(vec![
let text = Text::from(Line::from(vec![
Span::raw("Press "),
Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to send the message, "),
Expand All @@ -109,7 +110,7 @@ impl ChatApp {
let help_message = Paragraph::new(text);
frame.render_widget(help_message, chunks[0]);

let input = Paragraph::new(self.input.as_ref())
let input = Paragraph::new(self.input.as_str())
.style(Style::default().fg(Color::Yellow))
.block(Block::default().borders(Borders::ALL).title("Input"));
frame.render_widget(input, chunks[1]);
Expand All @@ -131,7 +132,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// setup postgres
let conn_url =
std::env::var("DATABASE_URL").expect("Env var DATABASE_URL is required for this example.");
let pool = sqlx::PgPool::connect(&conn_url).await?;
let pool = PgPool::connect(&conn_url).await?;

let mut listener = PgListener::connect(&conn_url).await?;
listener.listen("chan0").await?;
Expand Down

0 comments on commit fd4cb99

Please sign in to comment.