Skip to content

Commit

Permalink
Several optimizations
Browse files Browse the repository at this point in the history
- Write to pipes only when the value changes.
- Introduce a minimal delay to the background threads.

Fixes: #10
  • Loading branch information
sayanarijit committed Apr 6, 2021
1 parent 65ddb0e commit 40aaad7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
with import <nixpkgs> { };

# Update the src url, version and sha256 when new version
# Run nix-build and update the src url, version and sha256 when new version

rustPlatform.buildRustPackage rec {
name = "xplr";
Expand All @@ -9,6 +9,6 @@ rustPlatform.buildRustPackage rec {
("https://github.com/sayanarijit/xplr/archive/refs/tags/v0.3.2.tar.gz");
buildInputs = [ cargo ];
checkPhase = "";
cargoSha256 = "sha256-IyaYkHXmqXziXNK6uYU+XNNWA8a8S8cuMxkopps/9kk=";
cargoSha256 = "0000000000000000000000000000000000000000000000000000";
}

15 changes: 12 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use std::io;
use std::path::PathBuf;

pub const VERSION: &str = "v0.3.2"; // Update Cargo.toml and default.nix

pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW";

pub const UNSUPPORTED_STR: &str = "???";
pub const UPGRADE_GUIDE_LINK: &str = "github.com/sayanarijit/xplr/wiki/Upgrade-Guide";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipe {
Expand Down Expand Up @@ -808,6 +807,12 @@ impl App {
self.directory_buffer().and_then(|d| d.focused_node())
}

pub fn focused_node_str(&self) -> String {
self.focused_node()
.map(|n| format!("{}\n", n.absolute_path.clone()))
.unwrap_or_default()
}

pub fn enqueue(mut self, task: Task) -> Self {
self.tasks.push(task);
self
Expand Down Expand Up @@ -1281,6 +1286,10 @@ impl App {
&self.mode
}

pub fn mode_str(&self) -> String {
format!("{}\n", &self.mode.name)
}

/// Get a reference to the app's directory buffers.
pub fn directory_buffers(&self) -> &HashMap<String, DirectoryBuffer> {
&self.directory_buffers
Expand All @@ -1302,7 +1311,7 @@ impl App {
}

/// Get a reference to the app's runtime path.
pub fn session_path(&self) -> &String {
pub fn session_path(&self) -> &str {
&self.session_path
}

Expand Down
2 changes: 2 additions & 0 deletions src/event_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::input::Key;
use crossterm::event::{self, Event};
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
use std::time::Duration;

pub fn keep_reading(tx_msg_in: Sender<Task>, rx_event_reader: Receiver<bool>) {
thread::spawn(move || {
Expand Down Expand Up @@ -37,6 +38,7 @@ pub fn keep_reading(tx_msg_in: Sender<Task>, rx_event_reader: Receiver<bool>) {
}
}
}
thread::sleep(Duration::from_millis(1))
}
});
}
70 changes: 45 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::fs;
use std::io::prelude::*;
use std::path::PathBuf;
use std::sync::mpsc;
use std::time::Duration;
use termion::get_tty;
use tui::backend::CrosstermBackend;
use tui::Terminal;
Expand Down Expand Up @@ -40,6 +41,22 @@ fn main() -> Result<()> {

let mut app = app::App::create(pwd)?;

if app.version() != &app.config().version {
let msg = format!(
"version mismatch, to update your config file to {}, visit {}",
app.version(),
app::UPGRADE_GUIDE_LINK,
);

tx_msg_in.send(app::Task::new(
0,
app::MsgIn::External(app::ExternalMsg::LogInfo(msg)),
None,
))?;
};

fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?;

explorer::explore(
app.explorer_config().clone(),
app.pwd().clone(),
Expand Down Expand Up @@ -73,12 +90,12 @@ fn main() -> Result<()> {
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;

// Threads
auto_refresher::start_auto_refreshing(tx_msg_in.clone());
pipe_reader::keep_reading(app.pipe().msg_in.clone(), tx_msg_in.clone());

event_reader::keep_reading(tx_msg_in.clone(), rx_event_reader);

let mut last_pwd = app.pwd().clone();
let mut last_app = app.clone();
'outer: while result.is_ok() {
while let Some(msg) = app.pop_msg_out() {
match msg {
Expand Down Expand Up @@ -112,33 +129,41 @@ fn main() -> Result<()> {

app::MsgOut::Refresh => {
app = app.refresh_selection()?;
if app.pwd() != &last_pwd {
if app.pwd() != last_app.pwd() {
explorer::explore(
app.explorer_config().clone(),
app.pwd().clone(),
app.focused_node().map(|n| n.relative_path.clone()),
tx_msg_in.clone(),
);
last_pwd = app.pwd().to_owned();
};

// UI
terminal.draw(|f| ui::draw(f, &app, &hb))?;

let focused = app
.focused_node()
.map(|n| format!("{}\n", n.absolute_path.clone()))
.unwrap_or_default();
if app.focused_node() != last_app.focused_node() {
fs::write(&app.pipe().focus_out, app.focused_node_str())?;
};

if app.selection() != last_app.selection() {
fs::write(&app.pipe().selection_out, app.selection_str())?;
};

if app.mode_str() != last_app.mode_str() {
fs::write(&app.pipe().mode_out, app.mode_str())?;
};

let mode = format!("{}\n", &app.mode().name);
if app.directory_buffer() != last_app.directory_buffer() {
fs::write(&app.pipe().directory_nodes_out, app.directory_nodes_str())?;
};

fs::write(&app.pipe().focus_out, focused)?;
fs::write(&app.pipe().selection_out, app.selection_str())?;
fs::write(&app.pipe().mode_out, mode)?;
fs::write(&app.pipe().directory_nodes_out, app.directory_nodes_str())?;
fs::write(&app.pipe().global_help_menu_out, app.global_help_menu_str())?;
fs::write(&app.pipe().logs_out, app.logs_str())?;
fs::write(&app.pipe().result_out, app.result_str())?;
if app.logs() != last_app.logs() {
fs::write(&app.pipe().logs_out, app.logs_str())?;
};

if app.result() != last_app.result() {
fs::write(&app.pipe().result_out, app.result_str())?;
}
}

app::MsgOut::Call(cmd) => {
Expand All @@ -148,14 +173,8 @@ fn main() -> Result<()> {
execute!(terminal.backend_mut(), term::LeaveAlternateScreen)?;
terminal.show_cursor()?;

let pid = std::process::id().to_string();
let input_buffer = app.input_buffer().unwrap_or_default();

let focus_path = app
.focused_node()
.map(|n| n.absolute_path.clone())
.unwrap_or_default();

let focus_index = app
.directory_buffer()
.map(|d| d.focus)
Expand All @@ -176,9 +195,9 @@ fn main() -> Result<()> {
.current_dir(app.pwd())
.env("XPLR_APP_VERSION", app.version())
.env("XPLR_CONFIG_VERSION", &app.config().version)
.env("XPLR_PID", pid)
.env("XPLR_PID", &app.pid().to_string())
.env("XPLR_INPUT_BUFFER", input_buffer)
.env("XPLR_FOCUS_PATH", focus_path)
.env("XPLR_FOCUS_PATH", app.focused_node_str())
.env("XPLR_FOCUS_INDEX", focus_index)
.env("XPLR_SESSION_PATH", session_path)
.env("XPLR_PIPE_MSG_IN", pipe_msg_in)
Expand Down Expand Up @@ -212,6 +231,7 @@ fn main() -> Result<()> {
terminal.draw(|f| ui::draw(f, &app, &hb))?;
}
};
last_app = app.clone();
}

for task in rx_msg_in.try_iter() {
Expand All @@ -226,7 +246,7 @@ fn main() -> Result<()> {
app = new_app;
result = new_result;

// thread::sleep(Duration::from_millis(10));
std::thread::sleep(Duration::from_millis(1));
}

term::disable_raw_mode()?;
Expand Down
2 changes: 1 addition & 1 deletion src/pipe_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ pub fn keep_reading(pipe: String, tx: Sender<Task>) {
});
fs::write(&pipe, "").unwrap();
};
thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(50));
});
}

0 comments on commit 40aaad7

Please sign in to comment.