Polish xplr library API

Minor improvements to the xplr library API.

Closes: https://github.com/sayanarijit/xplr/issues/213
pull/215/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 69ac8883ab
commit 72a86f8e0e

@ -0,0 +1,14 @@
fn main() {
let pwd = std::path::PathBuf::from("/");
match xplr::run(pwd, None) {
Ok(Some(out)) => print!("{}", out),
Ok(None) => {}
Err(err) => {
if !err.to_string().is_empty() {
eprintln!("error: {}", err);
};
std::process::exit(1);
}
}
}

@ -4,6 +4,7 @@ use crate::explorer;
use crate::input::Key; use crate::input::Key;
use crate::lua; use crate::lua;
use crate::permissions::Permissions; use crate::permissions::Permissions;
use crate::runner;
use crate::ui::Layout; use crate::ui::Layout;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
@ -1765,8 +1766,8 @@ impl App {
pub fn explore_pwd(self) -> Result<Self> { pub fn explore_pwd(self) -> Result<Self> {
let dir = explorer::explore_sync( let dir = explorer::explore_sync(
self.explorer_config().clone(), self.explorer_config().clone(),
self.pwd().clone(), self.pwd().into(),
self.focused_node().map(|n| n.absolute_path().clone()), self.focused_node().map(|n| n.absolute_path().into()),
)?; )?;
self.add_directory(dir.parent().clone(), dir) self.add_directory(dir.parent().clone(), dir)
} }
@ -2794,4 +2795,15 @@ impl App {
last_modes: self.last_modes.clone(), last_modes: self.last_modes.clone(),
} }
} }
pub fn run(self, focused_path: Option<PathBuf>, lua: &mlua::Lua) -> Result<Option<String>> {
runner::run(self, focused_path, lua)
}
}
/// Run xplr TUI
pub fn run(pwd: PathBuf, focused_path: Option<PathBuf>) -> Result<Option<String>> {
let lua = mlua::Lua::new();
let app = App::create(pwd, &lua)?;
app.run(focused_path, &lua)
} }

@ -5,14 +5,12 @@ use std::path::PathBuf;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use std::thread; use std::thread;
pub fn explore_sync( pub(crate) fn explore_sync(
config: ExplorerConfig, config: ExplorerConfig,
parent: String, parent: PathBuf,
focused_path: Option<String>, focused_path: Option<PathBuf>,
) -> Result<DirectoryBuffer> { ) -> Result<DirectoryBuffer> {
let path = PathBuf::from(&parent); let dirs = fs::read_dir(&parent)?;
let dirs = fs::read_dir(&path)?;
let mut nodes = dirs let mut nodes = dirs
.filter_map(|d| { .filter_map(|d| {
d.ok().map(|e| { d.ok().map(|e| {
@ -22,30 +20,35 @@ pub fn explore_sync(
.unwrap_or_default() .unwrap_or_default()
}) })
}) })
.map(|name| Node::new(parent.clone(), name)) .map(|name| Node::new(parent.to_string_lossy().to_string(), name))
.filter(|n| config.filter(n)) .filter(|n| config.filter(n))
.collect::<Vec<Node>>(); .collect::<Vec<Node>>();
nodes.sort_by(|a, b| config.sort(a, b)); nodes.sort_by(|a, b| config.sort(a, b));
let focus_index = if let Some(focus) = focused_path { let focus_index = if let Some(focus) = focused_path {
let focus_str = focus.to_string_lossy().to_string();
nodes nodes
.iter() .iter()
.enumerate() .enumerate()
.find(|(_, n)| n.relative_path() == &focus) .find(|(_, n)| n.relative_path() == &focus_str)
.map(|(i, _)| i) .map(|(i, _)| i)
.unwrap_or(0) .unwrap_or(0)
} else { } else {
0 0
}; };
Ok(DirectoryBuffer::new(parent, nodes, focus_index)) Ok(DirectoryBuffer::new(
parent.to_string_lossy().to_string(),
nodes,
focus_index,
))
} }
pub fn explore_async( pub(crate) fn explore_async(
config: ExplorerConfig, config: ExplorerConfig,
parent: String, parent: PathBuf,
focused_path: Option<String>, focused_path: Option<PathBuf>,
tx_msg_in: Sender<Task>, tx_msg_in: Sender<Task>,
) { ) {
thread::spawn(move || { thread::spawn(move || {
@ -53,7 +56,10 @@ pub fn explore_async(
.map(|buf| { .map(|buf| {
tx_msg_in tx_msg_in
.send(Task::new( .send(Task::new(
MsgIn::Internal(InternalMsg::AddDirectory(parent.clone(), buf)), MsgIn::Internal(InternalMsg::AddDirectory(
parent.to_string_lossy().to_string(),
buf,
)),
None, None,
)) ))
.unwrap_or_default(); .unwrap_or_default();
@ -69,19 +75,23 @@ pub fn explore_async(
}); });
} }
pub fn explore_recursive_async( pub(crate) fn explore_recursive_async(
config: ExplorerConfig, config: ExplorerConfig,
parent: String, parent: PathBuf,
focused_path: Option<String>, focused_path: Option<PathBuf>,
tx_msg_in: Sender<Task>, tx_msg_in: Sender<Task>,
) { ) {
let path = PathBuf::from(&parent); explore_async(
explore_async(config.clone(), parent, focused_path, tx_msg_in.clone()); config.clone(),
if let Some(grand_parent) = path.parent() { parent.clone(),
focused_path,
tx_msg_in.clone(),
);
if let Some(grand_parent) = parent.parent() {
explore_recursive_async( explore_recursive_async(
config, config,
grand_parent.to_string_lossy().to_string(), grand_parent.into(),
path.file_name().map(|f| f.to_string_lossy().to_string()), parent.file_name().map(|p| p.into()),
tx_msg_in, tx_msg_in,
); );
} }

@ -14,3 +14,5 @@ pub mod pipe_reader;
pub mod pwd_watcher; pub mod pwd_watcher;
pub mod runner; pub mod runner;
pub mod ui; pub mod ui;
pub use app::run;

@ -3,7 +3,6 @@
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use xplr::app; use xplr::app;
use xplr::runner;
fn main() { fn main() {
let mut pwd = PathBuf::from(env::args().nth(1).unwrap_or_else(|| ".".into())) let mut pwd = PathBuf::from(env::args().nth(1).unwrap_or_else(|| ".".into()))
@ -12,23 +11,11 @@ fn main() {
let mut focused_path = None; let mut focused_path = None;
if pwd.is_file() { if pwd.is_file() {
focused_path = Some( focused_path = pwd.file_name().map(|p| p.into());
pwd.file_name() pwd = pwd.parent().map(|p| p.into()).unwrap_or_else(|| ".".into());
.unwrap_or_default()
.to_string_lossy()
.to_string(),
);
pwd = pwd.parent().map(|p| p.into()).unwrap_or_default();
} }
let lua = mlua::Lua::new(); match app::run(pwd, focused_path) {
let app = app::App::create(pwd, &lua).unwrap_or_else(|e| {
eprintln!("error: {}", e);
std::process::exit(1);
});
match runner::run(app, focused_path, lua) {
Ok(Some(out)) => print!("{}", out), Ok(Some(out)) => print!("{}", out),
Ok(None) => {} Ok(None) => {}
Err(err) => { Err(err) => {

@ -16,6 +16,7 @@ use mlua::LuaSerdeExt;
use std::fs; use std::fs;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::PathBuf;
use std::process::{Command, ExitStatus, Stdio}; use std::process::{Command, ExitStatus, Stdio};
use std::sync::mpsc; use std::sync::mpsc;
use termion::get_tty; use termion::get_tty;
@ -86,10 +87,10 @@ fn call(app: &app::App, cmd: app::Command, silent: bool) -> io::Result<ExitStatu
.status() .status()
} }
pub fn run( pub(crate) fn run(
mut app: app::App, mut app: app::App,
focused_path: Option<String>, focused_path: Option<PathBuf>,
lua: mlua::Lua, lua: &mlua::Lua,
) -> Result<Option<String>> { ) -> Result<Option<String>> {
fs::create_dir_all(app.session_path())?; fs::create_dir_all(app.session_path())?;
@ -99,7 +100,10 @@ pub fn run(
app = app.explore_pwd()?; app = app.explore_pwd()?;
app = if let Some(f) = focused_path.clone() { app = if let Some(f) = focused_path
.clone()
.map(|f| f.to_string_lossy().to_string())
{
app.focus_by_file_name(&f, true)? app.focus_by_file_name(&f, true)?
} else { } else {
app.focus_first(true)? app.focus_first(true)?
@ -107,7 +111,7 @@ pub fn run(
explorer::explore_recursive_async( explorer::explore_recursive_async(
app.explorer_config().clone(), app.explorer_config().clone(),
app.pwd().clone(), app.pwd().into(),
focused_path, focused_path,
tx_msg_in.clone(), tx_msg_in.clone(),
); );
@ -170,8 +174,8 @@ pub fn run(
app::MsgOut::ExplorePwdAsync => { app::MsgOut::ExplorePwdAsync => {
explorer::explore_async( explorer::explore_async(
app.explorer_config().clone(), app.explorer_config().clone(),
app.pwd().clone(), app.pwd().into(),
app.focused_node().map(|n| n.relative_path().clone()), app.focused_node().map(|n| n.relative_path().into()),
tx_msg_in.clone(), tx_msg_in.clone(),
); );
tx_pwd_watcher.send(app.pwd().clone())?; tx_pwd_watcher.send(app.pwd().clone())?;
@ -180,8 +184,8 @@ pub fn run(
app::MsgOut::ExploreParentsAsync => { app::MsgOut::ExploreParentsAsync => {
explorer::explore_recursive_async( explorer::explore_recursive_async(
app.explorer_config().clone(), app.explorer_config().clone(),
app.pwd().clone(), app.pwd().into(),
app.focused_node().map(|n| n.relative_path().clone()), app.focused_node().map(|n| n.relative_path().into()),
tx_msg_in.clone(), tx_msg_in.clone(),
); );
tx_pwd_watcher.send(app.pwd().clone())?; tx_pwd_watcher.send(app.pwd().clone())?;

Loading…
Cancel
Save