2024-08-09 09:14:27 +02:00
|
|
|
use crate::buffer::Controller as BufferController;
|
|
|
|
use crate::cursor::Controller as CursorController;
|
|
|
|
use crate::workspace::Workspace;
|
2024-08-08 23:58:45 +02:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::types::PyString;
|
2024-08-15 17:58:02 +02:00
|
|
|
use pyo3_asyncio::generic::future_into_py;
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
#[pymethods]
|
2024-08-09 09:14:27 +02:00
|
|
|
impl Workspace {
|
2024-08-08 23:58:45 +02:00
|
|
|
// join a workspace
|
|
|
|
#[pyo3(name = "create")]
|
|
|
|
fn pycreate<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&'p PyAny> {
|
|
|
|
let ws = self.clone();
|
|
|
|
|
|
|
|
pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
|
|
ws.create(path.as_str()).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
#[pyo3(name = "attach")]
|
|
|
|
fn pyattach<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&PyAny> {
|
|
|
|
let ws = self.clone();
|
|
|
|
|
|
|
|
pyo3_asyncio::tokio::future_into_py(py, async move {
|
2024-08-09 09:14:27 +02:00
|
|
|
let buffctl: BufferController = ws.attach(path.as_str()).await?;
|
2024-08-15 17:58:02 +02:00
|
|
|
Ok(buffctl)
|
|
|
|
// Python::with_gil(|py| Py::new(py, buffctl))
|
2024-08-08 23:58:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "detach")]
|
|
|
|
fn pydetach(&self, path: String) -> bool {
|
|
|
|
match self.detach(path.as_str()) {
|
2024-08-09 09:14:27 +02:00
|
|
|
crate::workspace::worker::DetachResult::NotAttached => false,
|
|
|
|
crate::workspace::worker::DetachResult::Detaching => true,
|
|
|
|
crate::workspace::worker::DetachResult::AlreadyDetached => true,
|
2024-08-08 23:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 17:58:02 +02:00
|
|
|
// #[pyo3(name = "event")]
|
|
|
|
// fn pyevent(&self, py: Python<'_>, path: String) -> PyResult<&PyAny> {
|
|
|
|
// let rc = self.clone();
|
|
|
|
// future_into_py(py, async move { Ok(rc.event().await?) })
|
|
|
|
// }
|
|
|
|
|
2024-08-08 23:58:45 +02:00
|
|
|
#[pyo3(name = "fetch_buffers")]
|
|
|
|
fn pyfetch_buffers<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> {
|
|
|
|
let ws = self.clone();
|
|
|
|
|
|
|
|
pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
|
|
ws.fetch_buffers().await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "fetch_users")]
|
|
|
|
fn pyfetch_users<'p>(&'p self, py: Python<'p>) -> PyResult<&PyAny> {
|
|
|
|
let ws = self.clone();
|
|
|
|
|
|
|
|
pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
|
|
ws.fetch_users().await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "list_buffer_users")]
|
|
|
|
fn pylist_buffer_users<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&PyAny> {
|
|
|
|
let ws = self.clone();
|
|
|
|
|
|
|
|
pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
|
|
let usrlist: Vec<String> = ws
|
|
|
|
.list_buffer_users(path.as_str())
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
2024-08-21 14:57:07 +02:00
|
|
|
.map(|e| e.id.to_string())
|
2024-08-08 23:58:45 +02:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(usrlist)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "delete")]
|
|
|
|
fn pydelete<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<&PyAny> {
|
|
|
|
let ws = self.clone();
|
|
|
|
|
|
|
|
pyo3_asyncio::tokio::future_into_py(py, async move {
|
|
|
|
ws.delete(path.as_str()).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "id")]
|
|
|
|
fn pyid(&self, py: Python<'_>) -> Py<PyString> {
|
|
|
|
PyString::new(py, self.id().as_str()).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "cursor")]
|
2024-08-09 09:14:27 +02:00
|
|
|
fn pycursor(&self, py: Python<'_>) -> PyResult<Py<CursorController>> {
|
2024-08-08 23:58:45 +02:00
|
|
|
Py::new(py, self.cursor())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "buffer_by_name")]
|
|
|
|
fn pybuffer_by_name(
|
|
|
|
&self,
|
|
|
|
py: Python<'_>,
|
|
|
|
path: String,
|
2024-08-09 09:14:27 +02:00
|
|
|
) -> PyResult<Option<Py<BufferController>>> {
|
2024-08-08 23:58:45 +02:00
|
|
|
let Some(bufctl) = self.buffer_by_name(path.as_str()) else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Some(Py::new(py, bufctl)?))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "buffer_list")]
|
|
|
|
fn pybuffer_list(&self) -> Vec<String> {
|
|
|
|
self.buffer_list()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyo3(name = "filetree")]
|
2024-08-21 14:57:07 +02:00
|
|
|
fn pyfiletree(&self, filter: Option<&str>) -> Vec<String> {
|
|
|
|
self.filetree(filter)
|
2024-08-08 23:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-15 17:58:02 +02:00
|
|
|
|
|
|
|
// #[pyclass]
|
|
|
|
// enum PyEvent {
|
|
|
|
// FileTreeUpdated,
|
|
|
|
// UserJoin { name: String },
|
|
|
|
// UserLeave { name: String },
|
|
|
|
// }
|