switch to parking_lot's Mutex/RwLock

This commit is contained in:
rabite 2020-05-25 02:04:23 +02:00
parent 16948dcec4
commit a735747a27
10 changed files with 224 additions and 137 deletions

40
Cargo.lock generated
View File

@ -636,6 +636,7 @@ dependencies = [
"nix 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"notify 4.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"osstrtools 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
"parse-ansi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"pathbuftools 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -759,6 +760,14 @@ dependencies = [
"scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lock_api"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "log"
version = "0.4.8"
@ -1024,6 +1033,15 @@ dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.5.0"
@ -1039,6 +1057,19 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parse-ansi"
version = "0.1.6"
@ -1427,6 +1458,11 @@ dependencies = [
"maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "smallvec"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "strip-ansi-escapes"
version = "0.1.0"
@ -1789,6 +1825,7 @@ dependencies = [
"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum lock_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed946d4529956a20f2d63ebe1b69996d5a2137c91913fe3ebbeff957f5bca7ff"
"checksum lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum lscolors 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea3b3414b2d015c4fd689815f2551797f3c2296bb241dd709c7da233ec7cba4b"
"checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084"
@ -1820,8 +1857,10 @@ dependencies = [
"checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
"checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
"checksum osstrtools 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7acb1f1b0af8b54107dc46ca4579bae0df3663e8501849201af732c3c0bd0d94"
"checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e"
"checksum parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa7767817701cce701d5585b9c4db3cdd02086398322c1d7e8bf5094a96a2ce7"
"checksum parking_lot_core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cb88cb1cb3790baa6776844f968fea3be44956cf184fa1be5a03341f5491278c"
"checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3"
"checksum parse-ansi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "50c48b4d524f8a10bf6ab37dc0b7583f17c8ec88b617b364ddfc3baee4dcf878"
"checksum pathbuftools 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "00f002a88874c85e30d4e133baae43cf382ceecf025f9493ce23eb381fcc922e"
"checksum petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f"
@ -1868,6 +1907,7 @@ dependencies = [
"checksum sixel-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fb46e0cd5569bf910390844174a5a99d52dd40681fff92228d221d9f8bf87dea"
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
"checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6"
"checksum smallvec 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4"
"checksum strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d63676e2abafa709460982ddc02a3bb586b6d15a49b75c212e06edd3933acee"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum strum 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e5d1c33039533f051704951680f1adfd468fd37ac46816ded0d9ee068e60f05f"

View File

@ -46,6 +46,7 @@ itertools = "0.8"
nix = "0.17"
strip-ansi-escapes = "0.1"
crossbeam = "0.7"
parking_lot = "0.10"
image = { version = "0.21.1", optional = true }

View File

@ -5,9 +5,10 @@ use failure::Fail;
use termion::event::Key;
use parking_lot::Mutex;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use crate::foldview::LogEntry;
use crate::mediaview::MediaError;
@ -191,12 +192,12 @@ lazy_static! {
}
pub fn get_logs() -> HResult<Vec<LogEntry>> {
let logs = LOG.lock()?.drain(..).collect();
let logs = LOG.lock().drain(..).collect();
Ok(logs)
}
pub fn put_log<L: Into<LogEntry>>(log: L) -> HResult<()> {
LOG.lock()?.push(log.into());
LOG.lock().push(log.into());
Ok(())
}

View File

@ -1,10 +1,12 @@
use termion::event::Key;
use parking_lot::{Mutex, RwLock};
use pathbuftools::PathBufTools;
use osstrtools::OsStrTools;
use async_value::Stale;
use std::io::Write;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::Arc;
use std::path::PathBuf;
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
@ -193,8 +195,15 @@ impl Tabbable for TabView<FileBrowser> {
dirs
});
self.active_tab_mut_().fs_cache.watch_only(open_dirs).log();
self.active_tab_mut_().fs_stat.write()?.refresh().log();
self.active_tab_mut_()
.fs_cache
.watch_only(open_dirs)
.log();
self.active_tab_mut_()
.fs_stat
.write()
.refresh()
.log();
Ok(())
}
@ -461,7 +470,9 @@ impl FileBrowser {
tab_paths: None
};
self.proc_view.lock()?.run_proc_raw(cmd)?;
self.proc_view
.lock()
.run_proc_raw(cmd)?;
Ok(())
}
@ -618,10 +629,15 @@ impl FileBrowser {
None => &self.cwd
}.path.to_string_lossy().to_string();
self.bookmarks.lock()?.set_coordinates(&self.core.coordinates).log();
self.bookmarks
.lock()
.set_coordinates(&self.core.coordinates)
.log();
loop {
let bookmark = self.bookmarks.lock()?.pick(cwd.to_string());
let bookmark = self.bookmarks
.lock()
.pick(cwd.to_string());
if let Err(HError::TerminalResizedError) = bookmark {
self.core.screen.clear().log();
@ -633,7 +649,10 @@ impl FileBrowser {
if let Err(HError::WidgetResizedError) = bookmark {
let coords = &self.core.coordinates;
self.bookmarks.lock()?.set_coordinates(&coords).log();
self.bookmarks
.lock()
.set_coordinates(&coords)
.log();
self.core.screen.clear().log();
self.refresh().log();
self.draw().log();
@ -660,8 +679,14 @@ impl FileBrowser {
pub fn add_bookmark(&mut self) -> HResult<()> {
let cwd = self.cwd.path.to_string_lossy().to_string();
let coords = &self.core.coordinates;
self.bookmarks.lock()?.set_coordinates(&coords).log();
self.bookmarks.lock()?.add(&cwd)?;
self.bookmarks
.lock()
.set_coordinates(&coords)
.log();
self.bookmarks
.lock()
.add(&cwd)?;
Ok(())
}
@ -1074,10 +1099,11 @@ impl FileBrowser {
fn external_select(&mut self) -> HResult<()> {
let shell = std::env::var("SHELL").unwrap_or("bash".into());
let cmd = self.core
.config.read()?
.get()?
.select_cmd
.clone();
.config
.read()
.get()?
.select_cmd
.clone();
self.core.get_sender().send(Events::InputEnabled(false))?;
self.core.screen.suspend().log();
@ -1182,10 +1208,11 @@ impl FileBrowser {
fn external_cd(&mut self) -> HResult<()> {
let shell = std::env::var("SHELL").unwrap_or("bash".into());
let cmd = self.core
.config.read()?
.get()?
.cd_cmd
.clone();
.config
.read()
.get()?
.cd_cmd
.clone();
self.core.get_sender().send(Events::InputEnabled(false))?;
self.core.screen.suspend().log();
@ -1272,7 +1299,9 @@ impl FileBrowser {
tab_paths: Some(tab_dirs)
};
self.proc_view.lock()?.run_proc_subshell(cmd)?;
self.proc_view
.lock()
.run_proc_subshell(cmd)?;
Ok(())
}
@ -1309,7 +1338,7 @@ impl FileBrowser {
self.preview_widget().map(|preview| preview.cancel_animation()).log();
let procview = self.proc_view.clone();
loop {
match procview.lock()?.popup() {
match procview.lock().popup() {
// Ignore refresh
Err(HError::RefreshParent) => continue,
Err(HError::TerminalResizedError) |
@ -1323,7 +1352,9 @@ impl FileBrowser {
pub fn show_log(&mut self) -> HResult<()> {
self.preview_widget().map(|preview| preview.cancel_animation()).log();
loop {
let res = self.log_view.lock()?.popup();
let res = self.log_view
.lock()
.popup();
if let Err(HError::RefreshParent) = res {
continue
@ -1382,7 +1413,10 @@ impl FileBrowser {
let count_xpos = xsize - file_count.len() as u16;
let count_ypos = ypos + self.get_coordinates()?.ysize();
let fs = self.fs_stat.read()?.find_fs(&file.path)?.clone();
let fs = self.fs_stat
.read()
.find_fs(&file.path)?
.clone();
let dev = fs.get_dev().unwrap_or(String::from(""));
let free_space = fs.get_free();
@ -1429,9 +1463,19 @@ impl Widget for FileBrowser {
fn set_coordinates(&mut self, coordinates: &Coordinates) -> HResult<()> {
self.core.coordinates = coordinates.clone();
self.columns.set_coordinates(&coordinates).log();
self.proc_view.lock()?.set_coordinates(&coordinates).log();
self.log_view.lock()?.set_coordinates(&coordinates).log();
self.bookmarks.lock()?.set_coordinates(&coordinates).log();
self.proc_view
.lock()
.set_coordinates(&coordinates)
.log();
self.log_view
.lock()
.set_coordinates(&coordinates)
.log();
self.bookmarks
.lock()
.set_coordinates(&coordinates)
.log();
Ok(())
}
@ -1468,7 +1512,7 @@ impl Widget for FileBrowser {
let xsize = term::xsize_u();
let mut status = self.get_core()?
.status_bar_content
.lock()?;
.lock();
let status = status.as_mut()
.take();
let active = self.columns

View File

@ -1,6 +1,7 @@
use lazy_static;
use termion::event::Key;
use failure::{self, Fail};
use parking_lot::{Mutex, RwLock};
use crate::widget::{Widget, WidgetCore};
use crate::coordinates::Coordinates;
@ -9,7 +10,7 @@ use crate::fail::{HResult, HError, ErrorLog, ErrorCause};
use crate::imgview::ImgView;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock,
use std::sync::{Arc,
mpsc::{channel, Sender}};
use std::io::{BufRead, BufReader, Write};
@ -120,7 +121,7 @@ impl MediaView {
// Use current size. Widget could have been resized at some point
let (xsize, ysize, xpix, ypix) =
{
let view = thread_imgview.lock()?;
let view = thread_imgview.lock();
let (xsize, ysize) = view.core.coordinates.size_u();
let (xpix, ypix) = view.core.coordinates.size_pixels()?;
(xsize, ysize, xpix, ypix)
@ -159,7 +160,7 @@ impl MediaView {
let mut stdout = BufReader::new(previewer.stdout.take()?);
let mut stdin = previewer.stdin.take()?;
cprocess.lock().map(|mut p| *p = Some(previewer))?;
*cprocess.lock() = Some(previewer);
let mut frame = vec![];
let newline = String::from("\n");
@ -167,7 +168,7 @@ impl MediaView {
let rx_cmd = rx_cmd.clone();
std::thread::spawn(move || -> HResult<()> {
for cmd in rx_cmd.lock()?.iter() {
for cmd in rx_cmd.lock().iter() {
write!(stdin, "{}", cmd)?;
write!(stdin, "\n")?;
stdin.flush()?;
@ -177,9 +178,10 @@ impl MediaView {
loop {
// Check if preview-gen finished and break out of loop to restart
if let Ok(Some(code)) = cprocess.lock()?
.as_mut()?
.try_wait() {
if let Ok(Some(code)) = cprocess.lock()
.as_mut()?
.try_wait()
{
if code.success() {
break;
} else {
@ -200,7 +202,7 @@ impl MediaView {
stdout.read_line(&mut line_buf)?;
let h = line_buf.trim().parse::<usize>()?;
let mut height = height.lock().unwrap();
let mut height = height.lock();
if *height != h {
new_height = true;
} else {
@ -212,26 +214,25 @@ impl MediaView {
line_buf.clear();
stdout.read_line(&mut line_buf)?;
let pos = &line_buf.trim();
*position.lock().unwrap() = pos
*position.lock() = pos
.parse::<usize>()?;
line_buf.clear();
stdout.read_line(&mut line_buf)?;
let dur = &line_buf.trim();
*duration.lock().unwrap() = dur
*duration.lock() = dur
.parse::<usize>()?;
if let Ok(mut imgview) = thread_imgview.lock() {
if new_height {
imgview.core.clear()?;
}
imgview.set_image_data(frame);
sender.send(crate::widget::Events::WidgetReady)
.map_err(|e| HError::from(e))
.log();
let mut imgview = thread_imgview.lock();
if new_height {
imgview.core.clear()?;
}
imgview.set_image_data(frame);
sender.send(crate::widget::Events::WidgetReady)
.map_err(|e| HError::from(e))
.log();
line_buf.clear();
frame = vec![];
@ -304,8 +305,8 @@ impl MediaView {
pub fn progress_bar(&self) -> HResult<String> {
let xsize = self.core.coordinates.xsize_u();
let position = self.position.lock()?.clone();
let duration = self.duration.lock()?.clone();
let position = self.position.lock().clone();
let duration = self.duration.lock().clone();
if duration == 0 || position == 0 {
Ok(format!("{:elements$}", "|", elements=xsize))
@ -323,8 +324,8 @@ impl MediaView {
}
pub fn progress_string(&self) -> HResult<String> {
let position = self.position.lock()?.clone();
let duration = self.duration.lock()?.clone();
let position = self.position.lock().clone();
let duration = self.duration.lock().clone();
let fposition = self.format_secs(position);
let fduration = self.format_secs(duration);
@ -342,7 +343,7 @@ impl MediaView {
let mut icons = String::new();
if *MUTE.read()? == true {
if *MUTE.read() == true {
icons += &crate::term::goto_xy_u(xpos+xsize-2, ypos+lines);
icons += mute_char;
} else {
@ -351,7 +352,7 @@ impl MediaView {
icons += " ";
}
if *AUTOPLAY.read()? == true {
if *AUTOPLAY.read() == true {
icons += &crate::term::goto_xy_u(xpos+xsize-4, ypos+lines);
icons += play_char;
} else {
@ -371,8 +372,8 @@ impl MediaView {
}
pub fn toggle_pause(&mut self) -> HResult<()> {
let auto = AUTOPLAY.read()?.clone();
let pos = self.position.lock()?.clone();
let auto = AUTOPLAY.read().clone();
let pos = self.position.lock().clone();
// This combination means only first frame was shown, since
// self.paused will be false, even with autoplay off
@ -410,40 +411,31 @@ impl MediaView {
}
pub fn autoplay(&self) -> bool {
if let Ok(autoplay) = AUTOPLAY.read() {
return *autoplay;
}
return true;
*AUTOPLAY.read()
}
pub fn mute(&self) -> bool {
if let Ok(mute) = MUTE.read() {
return *mute;
}
return false;
*MUTE.read()
}
pub fn toggle_autoplay(&self) {
if let Ok(mut autoplay) = AUTOPLAY.write() {
*autoplay = !*autoplay;
}
*AUTOPLAY.write() = !*AUTOPLAY.read();
}
pub fn toggle_mute(&self) {
if let Ok(mut mute) = MUTE.write() {
*mute = !*mute;
if *mute {
self.controller.send(String::from("m")).ok();
} else {
self.controller.send(String::from("u")).ok();
}
let mut mute = MUTE.write();
*mute = !*mute;
if *mute {
self.controller.send(String::from("m")).ok();
} else {
self.controller.send(String::from("u")).ok();
}
}
pub fn kill(&mut self) -> HResult<()> {
let proc = self.process.clone();
std::thread::spawn(move || -> HResult<()> {
proc.lock()?
proc.lock()
.as_mut()
.map(|p| {
p.kill().map_err(|e| HError::from(e)).log();
@ -470,7 +462,7 @@ impl Widget for MediaView {
self.core.coordinates = coordinates.clone();
let mut imgview = self.imgview.lock()?;
let mut imgview = self.imgview.lock();
imgview.set_image_data(vec![]);
imgview.set_coordinates(&coordinates)?;
@ -497,13 +489,13 @@ impl Widget for MediaView {
fn get_drawlist(&self) -> HResult<String> {
let (xpos, ypos) = self.core.coordinates.position_u();
let height = *self.height.lock()?;
let height = *self.height.lock();
let progress_str = self.progress_string()?;
let progress_bar = self.progress_bar()?;
let frame= self.imgview
.lock()
.map(|img| img.get_drawlist())?;
let frame = self.imgview
.lock()
.get_drawlist();
let mut frame = frame?;

View File

@ -1,7 +1,9 @@
use async_value::{Async, Stale};
use termion::event::Key;
use parking_lot::Mutex;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::path::PathBuf;
use crate::files::{File, Files, Kind, Ticker};
@ -26,7 +28,7 @@ fn kill_proc() -> HResult<()> {
// Kill media previewer if it still runs
ImgView::kill_running();
let mut pid = SUBPROC.lock()?;
let mut pid = SUBPROC.lock();
pid.map(|pid|
// Do this in another thread so we can wait on process to exit with SIGHUP
std::thread::spawn(move || {
@ -75,7 +77,10 @@ impl<W: Widget + Send + 'static> AsyncWidget<W> {
closure(stale).map_err(|e| e.into()));
widget.on_ready(move |_, stale| {
if !stale.is_stale()? {
sender.lock().map(|s| s.send(crate::widget::Events::WidgetReady)).ok();
sender.lock()
.send(crate::widget::Events::WidgetReady)
.map_err(HError::from)
.log();
}
Ok(())
}).log();
@ -104,8 +109,9 @@ impl<W: Widget + Send + 'static> AsyncWidget<W> {
widget.on_ready(move |_, stale| {
if !stale.is_stale()? {
sender.lock()
.map(|s| s.send(crate::widget::Events::WidgetReady))
.ok();
.send(crate::widget::Events::WidgetReady)
.map_err(HError::from)
.log();
}
Ok(())
}).log();
@ -539,7 +545,7 @@ impl Previewer {
let pid = process.id();
{
let mut pid_ = SUBPROC.lock()?;
let mut pid_ = SUBPROC.lock();
*pid_ = Some(pid);
}
@ -548,7 +554,7 @@ impl Previewer {
if stale.is_stale()? { return Previewer::preview_failed(&file) }
{
let mut pid_ = SUBPROC.lock()?;
let mut pid_ = SUBPROC.lock();
*pid_ = None;
}

View File

@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::sync::mpsc::Sender;
use std::process::{Child, Command};
use std::os::unix::process::{CommandExt, ExitStatusExt};
@ -10,6 +10,7 @@ use termion::event::Key;
use unicode_width::UnicodeWidthStr;
use osstrtools::{OsStringTools, OsStrTools, OsStrConcat};
use async_value::Stale;
use parking_lot::Mutex;
use crate::listview::{Listable, ListView};
use crate::textview::TextView;
@ -133,10 +134,10 @@ impl Process {
let success = self.success.clone();
let sender = self.sender.clone();
let cmd = self.cmd.clone();
let pid = self.handle.lock()?.id();
let pid = self.handle.lock().id();
std::thread::spawn(move || -> HResult<()> {
let stdout = handle.lock()?.stdout.take()?;
let stdout = handle.lock().stdout.take()?;
let mut stdout = BufReader::new(stdout);
let mut processor = move |cmd, sender: &Sender<Events>| -> HResult<()> {
loop {
@ -146,7 +147,7 @@ impl Process {
if len == 0 { return Ok(()) }
output.lock()?.push_str(&buffer);
output.lock().push_str(&buffer);
let status = format!("{}: read {} chars!", cmd, len);
sender.send(Events::Status(status))?;
@ -159,15 +160,15 @@ impl Process {
};
processor(&cmd, &sender).log();
if let Ok(proc_status) = handle.lock()?.wait() {
if let Ok(proc_status) = handle.lock().wait() {
let proc_success = proc_status.success();
let proc_status = match proc_status.code() {
Some(status) => status,
None => proc_status.signal().unwrap_or(-1)
};
*success.lock()? = Some(proc_success);
*status.lock()? = Some(proc_status);
*success.lock() = Some(proc_success);
*status.lock() = Some(proc_status);
let color_success =
if proc_success {
@ -363,7 +364,7 @@ impl ListView<Vec<Process>> {
fn kill_proc(&mut self) -> HResult<()> {
let proc = self.selected_proc()?;
proc.handle.lock()?.kill()?;
proc.handle.lock().kill()?;
Ok(())
}
@ -380,8 +381,8 @@ impl ListView<Vec<Process>> {
}
pub fn render_proc(&self, proc: &Process) -> HResult<String> {
let pid = proc.handle.lock()?.id();
let status = match *proc.status.lock()? {
let pid = proc.handle.lock().id();
let status = match *proc.status.lock() {
Some(status) => format!("{}", status),
None => format!("<{}>", pid),
};
@ -392,7 +393,7 @@ impl ListView<Vec<Process>> {
let padding = sized_string.len() - sized_string.width_cjk();
let padding = xsize - padding as u16;
let color_status = match *proc.success.lock().unwrap() {
let color_status = match *proc.success.lock() {
Some(false) => { format!("{}{}", term::color_red(), status) }
_ => { status }
};
@ -530,7 +531,7 @@ impl ProcView {
if Some(self.get_listview_mut().get_selection()) == self.viewing {
return Ok(());
}
let output = self.get_listview_mut().selected_proc()?.output.lock()?.clone();
let output = self.get_listview_mut().selected_proc()?.output.lock().clone();
let animator = self.animator.clone();
animator.set_fresh().log();
@ -601,7 +602,7 @@ impl Widget for ProcView {
let procs_running = listview
.content
.iter()
.filter(|proc| proc.status.lock().unwrap().is_none())
.filter(|proc| proc.status.lock().is_none())
.count();
let header = format!("Running processes: {} / {}",
@ -617,9 +618,9 @@ impl Widget for ProcView {
if let Some(proc) = listview.content.get(selection) {
let cmd = &proc.cmd;
let pid = proc.handle.lock()?.id();
let proc_status = proc.status.lock()?;
let proc_success = proc.success.lock()?;
let pid = proc.handle.lock().id();
let proc_status = proc.status.lock();
let proc_success = proc.success.lock();
let procinfo = if proc_status.is_some() {
let color_success =

View File

@ -1,13 +1,12 @@
use mime_guess::Mime;
use termion::event::Key;
use parking_lot::Mutex;
use async_value::Async;
use std::path::PathBuf;
use std::sync::{
Arc, Mutex,
mpsc::Sender,
};
use std::sync::{Arc,
mpsc::Sender};
use std::ffi::OsString;
use std::str::FromStr;
@ -420,9 +419,7 @@ impl QuickAction {
proc_view
.lock()
.map(|mut proc_view| {
proc_view.run_proc_raw(cmd)
})??;
.run_proc_raw(cmd)?;
Ok(())
}

View File

@ -1,5 +1,5 @@
use std::io::{Stdout, Write, BufWriter, BufRead};
use std::sync::{Arc, Mutex, RwLock};
use std::sync::Arc;
use termion;
use termion::screen::AlternateScreen;
@ -7,6 +7,7 @@ use termion::raw::{IntoRawMode, RawTerminal};
use parse_ansi::parse_bytes;
use crate::unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
use parking_lot::{Mutex, RwLock};
use crate::fail::{HResult, ErrorLog};
use crate::trait_ext::ExtractResult;
@ -35,23 +36,23 @@ impl Screen {
}
pub fn set_size(&self, size: (usize, usize)) -> HResult<()> {
*self.size.write()? = Some(size);
*self.size.write() = Some(size);
Ok(())
}
pub fn is_resized(&self) -> HResult<bool> {
Ok(self.size.read()?.is_some())
Ok(self.size.read().is_some())
}
pub fn get_size(&self) -> HResult<(usize, usize)> {
match self.size.read()?.clone() {
match self.size.read().clone() {
Some((xsize, ysize)) => Ok((xsize, ysize)),
None => Ok((self.xsize()?, self.ysize()?))
}
}
pub fn take_size(&self) -> HResult<(usize, usize)> {
Ok(self.size.write()?.take()?)
Ok(self.size.write().take()?)
}
pub fn set_title(&mut self, title: &str) -> HResult<()> {
@ -71,16 +72,12 @@ impl Write for Screen {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.screen
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other,
"Screen Mutex poisoned!"))
.and_then(|mut s| s.write(buf))
.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.screen
.lock()
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other,
"Screen Mutex poisoned!"))
.and_then(|mut s| s.flush())
.flush()
}
}
@ -155,13 +152,13 @@ pub trait ScreenExt: Write {
impl ScreenExt for Screen {
fn suspend_raw_mode(&mut self) -> HResult<()> {
self.screen
.lock()?
.lock()
.suspend_raw_mode()
}
fn activate_raw_mode(&mut self) -> HResult<()> {
self.screen
.lock()?
.lock()
.activate_raw_mode()
}
}

View File

@ -1,10 +1,11 @@
use std::sync::{Arc, Mutex, RwLock};
use std::sync::Arc;
use std::sync::mpsc::{Sender, Receiver, channel};
use std::io::{Write, stdin};
use termion::event::{Event, Key, MouseEvent};
use termion::input::TermRead;
use async_value::{Async, Stale};
use parking_lot::{Mutex, RwLock};
use crate::coordinates::{Coordinates, Position, Size};
@ -91,17 +92,17 @@ impl WidgetCore {
config: Arc::new(RwLock::new(config)) };
let minibuffer = MiniBuffer::new(&core);
*core.minibuffer.lock().unwrap() = Some(minibuffer);
*core.minibuffer.lock() = Some(minibuffer);
Ok(core)
}
pub fn get_sender(&self) -> Sender<Events> {
self.event_sender.lock().unwrap().clone()
self.event_sender.lock().clone()
}
pub fn draw_status(&self) -> HResult<()> {
let xsize = term::xsize_u();
let status = match self.status_bar_content.lock()?.as_ref() {
let status = match self.status_bar_content.lock().as_ref() {
Some(status) => status.to_string(),
None => "".to_string(),
};
@ -121,7 +122,7 @@ impl WidgetCore {
pub fn show_status(&self, status: &str) -> HResult<()> {
HError::log::<()>(status).ok();
{
let mut status_content = self.status_bar_content.lock()?;
let mut status_content = self.status_bar_content.lock();
*status_content = Some(status.to_string());
}
self.draw_status()?;
@ -129,7 +130,7 @@ impl WidgetCore {
}
pub fn clear_status(&self) -> HResult<()> {
if self.status_bar_content.lock()?.take().is_some() {
if self.status_bar_content.lock().take().is_some() {
self.draw_status().log();
}
Ok(())
@ -137,7 +138,7 @@ impl WidgetCore {
pub fn minibuffer_clear(&self) -> HResult<()> {
self.minibuffer
.lock()?
.lock()
.as_mut()?
.clear();
@ -146,7 +147,7 @@ impl WidgetCore {
pub fn minibuffer(&self, query: &str) -> HResult<String> {
let answer = self.minibuffer
.lock()?
.lock()
.as_mut()?
.query(query, false);
let mut screen = self.screen()?;
@ -156,7 +157,7 @@ impl WidgetCore {
pub fn minibuffer_continuous(&self, query: &str) -> HResult<String> {
let answer = self.minibuffer
.lock()?
.lock()
.as_mut()?
.query(query, true);
let mut screen = self.screen()?;
@ -203,9 +204,9 @@ impl WidgetCore {
fn get_conf(&self) -> HResult<Config> {
let conf = self.config
.read()?
.get()?
.clone();
.read()
.get()?
.clone();
Ok(conf)
}
}
@ -397,7 +398,10 @@ pub trait Widget {
HError::input_updated(input)?
}
Events::ConfigLoaded => {
self.get_core_mut()?.config.write()?.pull_async()?;
self.get_core_mut()?
.config
.write()
.pull_async()?;
}
_ => {}
}
@ -472,7 +476,7 @@ pub trait Widget {
fn handle_input(&mut self) -> HResult<()> {
let (tx_internal_event, rx_internal_event) = channel();
let rx_global_event = self.get_core()?.event_receiver.lock()?.take()?;
let rx_global_event = self.get_core()?.event_receiver.lock().take()?;
dispatch_events(tx_internal_event, rx_global_event, self.get_core()?.screen()?);
@ -492,7 +496,11 @@ pub trait Widget {
self.get_core()?.screen()?.clear().log();
}
Events::ConfigLoaded => {
self.get_core_mut()?.config.write()?.pull_async().ok();
self.get_core_mut()?
.config
.write()
.pull_async()
.ok();
self.config_loaded().log();
}
_ => {}
@ -536,7 +544,7 @@ fn dispatch_events(tx_internal: Sender<Events>,
match &event {
Events::ExclusiveEvent(tx_event) => {
tx_exclusive_event = match tx_event {
Some(locked_sender) => locked_sender.lock().unwrap().take(),
Some(locked_sender) => locked_sender.lock().take(),
None => None
}
}