Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for unix socket #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 202 additions & 9 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
extern crate mpd;
use mpd::error::Result;
use mpd::search::Window;
use mpd::{Client, Song, Status};
use nucleo_matcher::{Matcher, Utf32String};
use ratatui::crossterm::event::KeyEvent;
use ratatui::widgets::*;
use std::env;
use std::net::TcpStream;
use std::time::Duration;
mod impl_album_song;
mod impl_artiststate;
mod impl_library;
Expand All @@ -16,6 +19,9 @@ use crate::config::Config;
use crate::model::proto::*;
use crate::update::build_library;

#[cfg(unix)]
use {std::os::unix::net::UnixStream, std::path::PathBuf};

#[derive(Clone, Debug)]
pub enum Screen {
Library,
Expand Down Expand Up @@ -102,10 +108,17 @@ pub struct QueueSelector {
pub state: TableState,
}

// thin wrapper around all supported ways to communicate with MPD
pub enum Connection {
#[cfg(unix)]
UnixSocket(Client<UnixStream>),
TcpSocket(Client<TcpStream>),
}

pub struct Model {
pub state: State,
pub status: Status,
pub conn: Client,
pub conn: Connection,
pub screen: Screen,
pub library: LibraryState,
pub queue: QueueSelector,
Expand All @@ -118,14 +131,27 @@ pub struct Model {

impl Model {
pub fn new() -> Result<Self> {
let mpd_url = format!(
"{}:{}",
env::var("MPD_HOST").unwrap_or_else(|_| "localhost".to_string()),
env::var("MPD_PORT").unwrap_or_else(|_| "6600".to_string())
);
let mut conn = Client::connect(mpd_url.clone()).unwrap_or_else(|_| {
panic!("Failed to connect to mpd server at {}", mpd_url)
});
let mpd_host =
env::var("MPD_HOST").unwrap_or_else(|_| "localhost".to_string());
let mpd_port =
env::var("MPD_PORT").unwrap_or_else(|_| "6600".to_string());
let mpd_url = format!("{mpd_host}:{mpd_port}");

#[cfg(unix)]
let mut conn = if env::var("MPD_HOST").is_ok()
&& PathBuf::from(&mpd_host).exists()
{
let client = Client::<UnixStream>::connect(&mpd_host)?;
Connection::UnixSocket(client)
} else {
let client = Client::<TcpStream>::connect(&mpd_url)?;
Connection::TcpSocket(client)
};
#[cfg(not(unix))]
let mut conn = {
let client = Client::<TcpStream>::connect(&mpd_url)?;
Connection::TcpSocket(client)
};

Ok(Model {
state: State::Running,
Expand Down Expand Up @@ -217,3 +243,170 @@ impl Model {
}
}
}

impl Connection {
pub(crate) fn status(&mut self) -> Result<Status> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.status(),
Connection::TcpSocket(conn) => conn.status(),
}
}

pub(crate) fn currentsong(&mut self) -> Result<Option<Song>> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.currentsong(),
Connection::TcpSocket(conn) => conn.currentsong(),
}
}

pub(crate) fn list_groups(
&mut self,
vec: Vec<&str>,
) -> Result<Vec<Vec<String>>> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.list_groups(vec),
Connection::TcpSocket(conn) => conn.list_groups(vec),
}
}

pub(crate) fn queue(&mut self) -> Result<Vec<Song>> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.queue(),
Connection::TcpSocket(conn) => conn.queue(),
}
}

pub(crate) fn delete(&mut self, p: u32) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.delete(p),
Connection::TcpSocket(conn) => conn.delete(p),
}
}

pub(crate) fn swap(&mut self, p: u32, to: u32) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.swap(p, to),
Connection::TcpSocket(conn) => conn.swap(p, to),
}
}

pub(crate) fn switch(&mut self, pos: u32) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.switch(pos),
Connection::TcpSocket(conn) => conn.switch(pos),
}
}

pub(crate) fn findadd(&mut self, query: &mut mpd::Query) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.findadd(query),
Connection::TcpSocket(conn) => conn.findadd(query),
}
}

pub(crate) fn find<W>(
&mut self,
query: &mut mpd::Query,
window: W,
) -> Result<Vec<Song>>
where
W: Into<Window>,
{
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.find(query, window),
Connection::TcpSocket(conn) => conn.find(query, window),
}
}

pub(crate) fn list_group_2(
&mut self,
terms: (String, String),
) -> Result<Vec<(String, String)>> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.list_group_2(terms),
Connection::TcpSocket(conn) => conn.list_group_2(terms),
}
}

pub(crate) fn clear(&mut self) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.clear(),
Connection::TcpSocket(conn) => conn.clear(),
}
}

pub(crate) fn consume(&mut self, value: bool) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.consume(value),
Connection::TcpSocket(conn) => conn.consume(value),
}
}

pub(crate) fn random(&mut self, value: bool) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.random(value),
Connection::TcpSocket(conn) => conn.random(value),
}
}

pub(crate) fn single(&mut self, value: bool) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.single(value),
Connection::TcpSocket(conn) => conn.single(value),
}
}

pub(crate) fn repeat(&mut self, value: bool) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.repeat(value),
Connection::TcpSocket(conn) => conn.repeat(value),
}
}

pub(crate) fn toggle_pause(&mut self) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.toggle_pause(),
Connection::TcpSocket(conn) => conn.toggle_pause(),
}
}

pub(crate) fn next(&mut self) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.next(),
Connection::TcpSocket(conn) => conn.next(),
}
}

pub(crate) fn prev(&mut self) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.prev(),
Connection::TcpSocket(conn) => conn.prev(),
}
}

pub(crate) fn seek(&mut self, place: u32, pos: Duration) -> Result<()> {
match self {
#[cfg(unix)]
Connection::UnixSocket(conn) => conn.seek(place, pos),
Connection::TcpSocket(conn) => conn.seek(place, pos),
}
}
}