Skip to content

Commit a67a463

Browse files
CommanderStormjrasanen
authored andcommitted
chore(examples):Migrated the pg-chat example to ratatui (launchbadge#3385)
* migrated the pg-chat example to ratatui * fixed formatting mistake
1 parent 70d65c5 commit a67a463

File tree

3 files changed

+137
-33
lines changed

3 files changed

+137
-33
lines changed

Cargo.lock

+122-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/postgres/chat/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ workspace = "../../../"
88
sqlx = { path = "../../../", features = [ "postgres", "runtime-tokio-native-tls" ] }
99
futures = "0.3.1"
1010
tokio = { version = "1.20.0", features = [ "rt-multi-thread", "macros" ] }
11-
tui = "0.19.0"
12-
crossterm = "0.25"
11+
ratatui = "0.27.0"
12+
crossterm = "0.27.0"
1313
unicode-width = "0.1"

examples/postgres/chat/src/main.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ use crossterm::{
33
execute,
44
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
55
};
6-
use sqlx::postgres::PgListener;
7-
use sqlx::PgPool;
8-
use std::sync::Arc;
9-
use std::{error::Error, io};
10-
use tokio::{sync::Mutex, time::Duration};
11-
use tui::{
6+
use ratatui::text::Line;
7+
use ratatui::{
128
backend::{Backend, CrosstermBackend},
139
layout::{Constraint, Direction, Layout},
1410
style::{Color, Modifier, Style},
15-
text::{Span, Spans, Text},
11+
text::{Span, Text},
1612
widgets::{Block, Borders, List, ListItem, Paragraph},
1713
Frame, Terminal,
1814
};
15+
use sqlx::postgres::PgListener;
16+
use sqlx::PgPool;
17+
use std::sync::Arc;
18+
use std::{error::Error, io};
19+
use tokio::{sync::Mutex, time::Duration};
1920
use unicode_width::UnicodeWidthStr;
2021

2122
struct ChatApp {
@@ -53,7 +54,7 @@ impl ChatApp {
5354
.await
5455
.iter()
5556
.map(|m| {
56-
let content = vec![Spans::from(Span::raw(m.to_owned()))];
57+
let content = vec![Line::from(Span::raw(m.to_owned()))];
5758
ListItem::new(content)
5859
})
5960
.collect();
@@ -85,7 +86,7 @@ impl ChatApp {
8586
}
8687
}
8788

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

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

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

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

0 commit comments

Comments
 (0)