Skip to content

Commit

Permalink
upgrade to ratatui
Browse files Browse the repository at this point in the history
 - prompt user for confirmation before adding an entire folder to playlist
  • Loading branch information
kamiyaa committed Jun 3, 2024
1 parent 2123074 commit 17c1fe0
Show file tree
Hide file tree
Showing 23 changed files with 468 additions and 262 deletions.
549 changes: 363 additions & 186 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ optional = true
version = "^0"
features = ["all"]

[dependencies.tui]
features = ["termion"]
default-features = false
[dependencies.ratatui]
version = "^0"
default-features = false
features = ["termion"]

[dependencies.uuid]
features = [ "v4" ]
Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/config/option/layout_option.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use tui::layout::Direction;
use ratatui::layout::Direction;

use dizi::error::{DiziError, DiziErrorKind, DiziResult};

Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/config/theme/style.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde_derive::Deserialize;

use tui::style;
use ratatui::style;

const fn default_color() -> style::Color {
style::Color::Reset
Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/context/app_context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ratatui::layout::Rect;
use std::io;
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::mpsc;
use tui::layout::Rect;

use dizi::utils;

Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/context/message_queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

use tui::style::{Color, Style};
use ratatui::style::{Color, Style};

#[derive(Clone, Debug, Default)]
pub struct Message {
Expand Down
43 changes: 35 additions & 8 deletions src/bin/client/key_command/impl_appexecute.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use dizi::error::DiziResult;
use dizi::request::client::ClientRequest;
use termion::event::Key;

use crate::commands::*;
use crate::config::option::WidgetType;
use crate::config::AppKeyMapping;
use crate::context::AppContext;
use crate::ui::AppBackend;
use crate::util::request::send_client_request;
use crate::{commands::*, ui::widgets::TuiPrompt};

use super::{AppExecute, Command};

Expand Down Expand Up @@ -50,7 +51,7 @@ impl AppExecute for Command {
selection::select_files(context, pattern.as_str(), options)?
}

Self::ServerRequest(request) => execute_request(context, request)?,
Self::ServerRequest(request) => execute_request(backend, context, request)?,

Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context)?,
Self::ToggleView => {
Expand All @@ -70,22 +71,48 @@ impl AppExecute for Command {
}
}

pub fn execute_request(context: &mut AppContext, request: &ClientRequest) -> DiziResult {
pub fn execute_request(
backend: &mut AppBackend,
context: &mut AppContext,
request: &ClientRequest,
) -> DiziResult {
match request {
ClientRequest::ServerQuit => {
quit::server_quit(context)?;
}
ClientRequest::PlaylistAppend { path: None } => {
if let Some(entry) = context
let entry_file_path = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|s| s.curr_entry_ref())
{
let request = ClientRequest::PlaylistAppend {
path: Some(entry.file_path().to_path_buf()),
.map(|e| e.file_path().to_path_buf());

if let Some(entry_path) = entry_file_path {
if !entry_path.is_dir() {
let request = ClientRequest::PlaylistAppend {
path: Some(entry_path),
};
send_client_request(context, &request)?;
return Ok(());
}

let ch = {
let prompt_str = format!("Add all songs in this directory? [Y/n]");
let mut prompt = TuiPrompt::new(&prompt_str);
prompt.get_key(backend, context)
};
send_client_request(context, &request)?;

match ch {
Key::Char('Y') | Key::Char('y') | Key::Char('\n') => {
let request = ClientRequest::PlaylistAppend {
path: Some(entry_path),
};
send_client_request(context, &request)?;
return Ok(());
}
_ => {}
}
}
}
ClientRequest::PlaylistOpen {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/run/run_ui.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::{BufRead, BufReader};
use std::thread;

use ratatui::layout::{Constraint, Rect};
use termion::event::Event;
use tui::layout::{Constraint, Rect};

use dizi::error::DiziResult;
use dizi::request::client::ClientRequest;
Expand Down
8 changes: 4 additions & 4 deletions src/bin/client/ui/backend.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::io::stdout;
use std::io::Write;

use ratatui::backend::TermionBackend;
use termion::raw::{IntoRawMode, RawTerminal};
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;

use tui::widgets::Widget;
use ratatui::widgets::Widget;

#[cfg(feature = "mouse")]
use termion::input::MouseTerminal;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl New for Screen {
}
}

pub type AppTerminal = tui::Terminal<TermionBackend<Screen>>;
pub type AppTerminal = ratatui::Terminal<TermionBackend<Screen>>;

pub struct AppBackend {
pub terminal: Option<AppTerminal>,
Expand All @@ -50,7 +50,7 @@ impl AppBackend {
write!(alt_screen, "{}", termion::clear::All)?;

let backend = TermionBackend::new(alt_screen);
let mut terminal = tui::Terminal::new(backend)?;
let mut terminal = ratatui::Terminal::new(backend)?;
terminal.hide_cursor()?;
Ok(Self {
terminal: Some(terminal),
Expand Down
6 changes: 3 additions & 3 deletions src/bin/client/ui/views/tui_command_menu.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::iter::Iterator;

use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::widgets::{Clear, Widget};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::{Clear, Widget};

use crate::config::KeyMapping;
use crate::context::AppContext;
Expand Down
6 changes: 3 additions & 3 deletions src/bin/client/ui/views/tui_folder_view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tui::buffer::Buffer;
use tui::layout::Rect;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;

use tui::widgets::Widget;
use ratatui::widgets::Widget;

use crate::context::AppContext;
use crate::ui::widgets::TuiDirListDetailed;
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client/ui/views/tui_textfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use rustyline::{
At, Word,
};

use ratatui::layout::Rect;
use ratatui::widgets::Clear;
use termion::event::{Event, Key};
use tui::layout::Rect;
use tui::widgets::Clear;
use unicode_width::UnicodeWidthStr;

use crate::context::AppContext;
Expand Down
14 changes: 7 additions & 7 deletions src/bin/client/ui/views/tui_view.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use tui::buffer::Buffer;
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Style};
use tui::text::Span;
use tui::widgets::{Block, Borders, Paragraph, Widget, Wrap};
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Style};
use ratatui::text::Span;
use ratatui::widgets::{Block, Borders, Paragraph, Widget, Wrap};

use crate::config::option::{LayoutComposition, WidgetType};
use crate::context::AppContext;
Expand Down Expand Up @@ -142,8 +142,8 @@ pub fn render_widget(
.direction(direction.clone())
.constraints(constraints)
.split(area);
for (widget, rect) in widgets.iter().zip(layout_rect) {
render_widget(context, widget, rect, buf);
for (widget, rect) in widgets.iter().zip(layout_rect.iter()) {
render_widget(context, widget, *rect, buf);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/bin/client/ui/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tui_footer;
mod tui_menu;
mod tui_player;
mod tui_playlist;
mod tui_prompt;
mod tui_text;
mod tui_topbar;

Expand All @@ -11,5 +12,6 @@ pub use self::tui_footer::*;
pub use self::tui_menu::*;
pub use self::tui_player::*;
pub use self::tui_playlist::*;
pub use self::tui_prompt::*;
pub use self::tui_text::*;
pub use self::tui_topbar::*;
8 changes: 4 additions & 4 deletions src/bin/client/ui/widgets/tui_dirlist_detailed.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::cmp::Ordering;

use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Modifier, Style};
use tui::widgets::Widget;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::widgets::Widget;
use unicode_width::UnicodeWidthStr;

use dizi::song::Song;
Expand Down
12 changes: 6 additions & 6 deletions src/bin/client/ui/widgets/tui_footer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::text::{Span, Spans};
use tui::widgets::{Paragraph, Widget};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Widget};

use dizi::player::PlayerState;

Expand Down Expand Up @@ -47,6 +47,6 @@ impl<'a> Widget for TuiFooter<'a> {
)),
];

Paragraph::new(Spans::from(text)).render(area, buf);
Paragraph::new(Line::from(text)).render(area, buf);
}
}
8 changes: 4 additions & 4 deletions src/bin/client/ui/widgets/tui_menu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, Widget};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::widgets::{Block, Borders, Widget};

pub struct TuiMenu<'a> {
options: &'a [&'a str],
Expand Down
14 changes: 7 additions & 7 deletions src/bin/client/ui/widgets/tui_player.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::time;

use dizi::playlist::PlaylistType;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans};
use tui::widgets::{Paragraph, Widget, Wrap};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Widget, Wrap};

use dizi::player::{PlayerState, PlayerStatus};

Expand Down Expand Up @@ -64,7 +64,7 @@ impl<'a> Widget for TuiPlayer<'a> {
let player_status_style = Style::default()
.fg(Color::Green)
.add_modifier(Modifier::BOLD);
let text = Spans::from(vec![
let text = Line::from(vec![
Span::styled(
format!("Volume: {}% ", self.player.get_volume(),),
player_status_style,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<'a> Widget for TuiPlayer<'a> {
PlayerStatus::Paused => "\u{2590} \u{258C}",
};

let text = Spans::from(vec![
let text = Line::from(vec![
Span::raw(format!(
"{} {} / {} ",
player_status, duration_played_str, total_duration_str
Expand Down
8 changes: 4 additions & 4 deletions src/bin/client/ui/widgets/tui_playlist.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::cmp::Ordering;

use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Modifier, Style};
use tui::widgets::Widget;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::widgets::Widget;

use unicode_width::UnicodeWidthStr;

Expand Down
8 changes: 4 additions & 4 deletions src/bin/client/ui/widgets/tui_prompt.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::text::Span;
use ratatui::widgets::{Clear, Paragraph, Wrap};
use termion::event::{Event, Key};
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::text::Span;
use tui::widgets::{Clear, Paragraph, Wrap};

use crate::context::AppContext;
use crate::event::process_event;
Expand Down
8 changes: 4 additions & 4 deletions src/bin/client/ui/widgets/tui_text.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::Widget;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::widgets::Widget;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

#[derive(Clone, Debug)]
Expand Down
14 changes: 7 additions & 7 deletions src/bin/client/ui/widgets/tui_topbar.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::Path;

use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans};
use tui::widgets::{Paragraph, Widget};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Widget};

use crate::context::AppContext;

Expand Down Expand Up @@ -36,8 +36,8 @@ impl<'a> Widget for TuiTopBar<'a> {
}

let text = match ellipses {
Some(s) => Spans::from(vec![s, Span::styled(curr_path_str, path_style)]),
None => Spans::from(vec![Span::styled(curr_path_str, path_style)]),
Some(s) => Line::from(vec![s, Span::styled(curr_path_str, path_style)]),
None => Line::from(vec![Span::styled(curr_path_str, path_style)]),
};

Paragraph::new(text).render(area, buf);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/client/util/style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tui::style::Style;
use ratatui::style::Style;

use crate::fs::{FileType, JoshutoDirEntry, LinkType};
use crate::util::unix;
Expand Down

1 comment on commit 17c1fe0

@eliasdiek
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kamiyaa hey ,I'm looking for someone with your stack to work on our platform ,either as a freelancer or in a full time role . are you up for it ? contact me on telegram @eliasdiek or via email at eliasdiekk@gmail.com

Please sign in to comment.