1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00

prototype async previews

This commit is contained in:
rabite 2019-02-04 18:51:07 +01:00
parent 1b95fb704f
commit 9cd395d8f7
10 changed files with 155 additions and 100 deletions

View File

@ -17,9 +17,9 @@ impl Coordinates {
}
}
// pub fn size(&self) -> &Size {
// &self.size
// }
pub fn size(&self) -> &Size {
&self.size
}
pub fn xsize(&self) -> u16 {
self.size.xsize()
@ -59,7 +59,7 @@ impl Position {
pub fn x(&self) -> u16 {
(self.0).1
}
// pub fn y(&self) -> u16 {
// (self.0).1
// }
pub fn y(&self) -> u16 {
(self.0).1
}
}

View File

@ -9,6 +9,7 @@ use crate::listview::ListView;
use crate::miller_columns::MillerColumns;
use crate::widget::Widget;
#[derive(PartialEq)]
pub struct FileBrowser {
pub columns: MillerColumns<ListView<Files>>,
}

View File

@ -6,7 +6,7 @@ use std::time::SystemTime;
use lscolors::LsColors;
use mime_detective;
use rayon::prelude::*;
lazy_static! {
static ref COLORS: LsColors = LsColors::from_env().unwrap();

View File

@ -3,62 +3,74 @@ use termion::event::{Event};
use crate::widget::Widget;
use crate::coordinates::{Coordinates, Size, Position};
// pub struct Child<T> {
// widget: T,
// position: (u16, u16),
// size: (u16, u16),
// active: bool
// }
pub struct HBox {
dimensions: (u16, u16),
position: (u16, u16),
coordinates: Coordinates,
children: Vec<Box<Widget>>,
active: usize
#[derive(PartialEq)]
pub struct HBox<T: Widget> {
pub coordinates: Coordinates,
pub widgets: Vec<T>,
pub active: Option<usize>,
}
impl HBox {
pub fn new(widgets: Vec<Box<Widget>>,
dimensions: (u16, u16),
coordinates: Coordinates,
position: (u16, u16),
main: usize) -> HBox {
let mut hbox = HBox {
dimensions: dimensions,
coordinates: Coordinates { size: Size (dimensions),
position: Position (position),
parent: None },
position: position,
children: widgets,
active: main
};
hbox.resize_children();
hbox
impl<T> HBox<T> where T: Widget {
pub fn new() -> HBox<T> {
HBox { coordinates: Coordinates::new(),
widgets: vec![],
active: None
}
}
pub fn resize_children(&mut self) {
let hbox_size = dbg!(self.dimensions);
let hbox_position = dbg!(self.position);
let cell_size = dbg!(hbox_size.0 / self.children.len() as u16);
let mut current_pos = dbg!(hbox_position.1);
for widget in &mut self.children {
widget.set_size(Size ( (cell_size, hbox_size.1)) );
widget.set_position(dbg!((current_pos, hbox_position.1)));
widget.refresh();
dbg!(current_pos += cell_size);
let coords: Vec<Coordinates>
= self.widgets.iter().map(
|w|
self.calculate_coordinates(w)).collect();
for (widget, coord) in self.widgets.iter_mut().zip(coords.iter()) {
widget.set_coordinates(coord);
}
}
// pub fn widget(&self, index: usize) -> &Box<Widget> {
// &self.children[index]
// }
pub fn push_widget(&mut self, widget: T) where T: PartialEq {
self.widgets.push(widget);
self.resize_children();
self.refresh();
}
pub fn active_widget(&self) -> &Box<Widget> {
&self.children[self.active]
pub fn pop_widget(&mut self) -> Option<T> {
let widget = self.widgets.pop();
self.resize_children();
self.refresh();
widget
}
pub fn prepend_widget(&mut self, widget: T) {
self.widgets.insert(0, widget);
self.resize_children();
self.refresh();
}
pub fn calculate_coordinates(&self, widget: &T)
-> Coordinates where T: PartialEq {
let xsize = self.coordinates.xsize();
let ysize = self.coordinates.ysize();
let top = self.coordinates.top().x();
let pos = self.widgets.iter().position(|w | w == widget).unwrap();
let num = self.widgets.len();
let widget_xsize = (xsize / num as u16) + 1;
let widget_xpos = widget_xsize * pos as u16;
Coordinates {
size: Size((widget_xsize,
ysize)),
position: Position((widget_xpos,
top))
}
}
pub fn active_widget(&self) -> &T {
&self.widgets.last().unwrap()
}
}
@ -66,43 +78,47 @@ impl HBox {
impl Widget for HBox {
fn render(&self) -> Vec<String> {
// HBox doesnt' draw anything itself
vec![]
}
impl<T> Widget for HBox<T> where T: Widget {
fn render_header(&self) -> String {
self.active_widget().render_header()
}
fn refresh(&mut self) {
for child in &mut self.children {
self.resize_children();
for child in &mut self.widgets {
child.refresh();
}
}
fn get_drawlist(&self) -> String {
self.children.iter().map(|child| {
self.widgets.iter().map(|child| {
child.get_drawlist()
}).collect()
}
fn get_size(&self) -> Size {
Size( self.dimensions )
fn get_size(&self) -> &Size {
&self.coordinates.size
}
fn get_position(&self) -> Position {
Position ( self.position )
fn get_position(&self) -> &Position {
&self.coordinates.position
}
fn set_size(&mut self, size: Size) {
self.dimensions = size.0;
self.coordinates.size = size;
}
fn set_position(&mut self, position: Position) {
self.position = position.0;
self.coordinates.position = position;
}
fn get_coordinates(&self) -> &Coordinates {
&self.coordinates
}
fn set_coordinates(&mut self, coordinates: &Coordinates) {
if self.coordinates == *coordinates {
return;
}
self.coordinates = coordinates.clone();
self.refresh();
}
fn on_event(&mut self, event: Event) {
self.children[self.active].on_event(event);
self.widgets.last_mut().unwrap().on_event(event);
}
}

View File

@ -11,6 +11,7 @@ use crate::widget::Widget;
// Maybe also buffer drawlist for efficiency when it doesn't change every draw
#[derive(PartialEq)]
pub struct ListView<T>
where
T: Send,

View File

@ -25,6 +25,7 @@ mod textview;
mod widget;
mod win_main;
mod window;
mod hbox;
use window::Window;

View File

@ -3,9 +3,11 @@ use termion::event::Key;
use crate::coordinates::{Coordinates, Position, Size};
use crate::preview::Previewer;
use crate::widget::Widget;
use crate::hbox::HBox;
pub struct MillerColumns<T> {
pub widgets: Vec<T>,
#[derive(PartialEq)]
pub struct MillerColumns<T> where T: Widget {
pub widgets: HBox<T>,
// pub left: Option<T>,
// pub main: Option<T>,
pub preview: Previewer,
@ -19,30 +21,26 @@ where
{
pub fn new() -> Self {
Self {
widgets: vec![],
widgets: HBox::new(),
coordinates: Coordinates::new(),
ratio: (33, 33, 33),
preview: Previewer::new(),
}
}
pub fn push_widget(&mut self, mut widget: T) {
let mcoords = self.calculate_coordinates().1;
widget.set_coordinates(&mcoords);
self.widgets.push(widget);
pub fn push_widget(&mut self, widget: T) {
self.widgets.push_widget(widget);
self.refresh();
}
pub fn pop_widget(&mut self) -> Option<T> {
let widget = self.widgets.pop();
let widget = self.widgets.pop_widget();
self.refresh();
widget
}
pub fn prepend_widget(&mut self, mut widget: T) {
let lcoords = self.calculate_coordinates().0;
widget.set_coordinates(&lcoords);
self.widgets.insert(0, widget);
pub fn prepend_widget(&mut self, widget: T) {
self.widgets.prepend_widget(widget);
}
pub fn calculate_coordinates(&self) -> (Coordinates, Coordinates, Coordinates) {
@ -82,25 +80,25 @@ where
}
pub fn get_left_widget(&self) -> Option<&T> {
let len = self.widgets.len();
let len = self.widgets.widgets.len();
if len < 2 {
return None;
}
self.widgets.get(len - 2)
self.widgets.widgets.get(len - 2)
}
pub fn get_left_widget_mut(&mut self) -> Option<&mut T> {
let len = self.widgets.len();
let len = self.widgets.widgets.len();
if len < 2 {
return None;
}
self.widgets.get(len - 2)?.get_position();
self.widgets.get_mut(len - 2)
self.widgets.widgets.get(len - 2)?.get_position();
self.widgets.widgets.get_mut(len - 2)
}
pub fn get_main_widget(&self) -> &T {
self.widgets.last().unwrap()
self.widgets.widgets.last().unwrap()
}
pub fn get_main_widget_mut(&mut self) -> &mut T {
self.widgets.last_mut().unwrap()
self.widgets.widgets.last_mut().unwrap()
}
}

View File

@ -1,7 +1,4 @@
use rayon as rayon;
use std::io::{stdout, Write};
use std::sync::atomic::AtomicUsize;
use std::io::Write;
use std::sync::Mutex;
use crate::coordinates::{Coordinates, Position, Size};
@ -10,11 +7,23 @@ use crate::listview::ListView;
use crate::textview::TextView;
use crate::widget::Widget;
pub struct InstanceCounter(Mutex<usize>);
impl PartialEq for InstanceCounter {
fn eq(&self, other: &Self) -> bool {
let instance = self.0.lock().unwrap();
let other = other.0.lock().unwrap();
*instance == *other
}
}
#[derive(PartialEq)]
pub struct Previewer {
pub file: Option<File>,
pub buffer: String,
pub coordinates: Coordinates,
pub instances: Mutex<usize>
pub instances: InstanceCounter
}
impl Previewer {
@ -23,17 +32,17 @@ impl Previewer {
file: None,
buffer: String::new(),
coordinates: Coordinates::new(),
instances: From::from(0)
instances: InstanceCounter(Mutex::new(0))
}
}
pub fn set_file(&mut self, file: &File) {
//return;
let mut instance = self.instances.try_lock().unwrap();
let mut instance = self.instances.0.try_lock().unwrap();
if *instance > 2 { return }
*instance = *instance + 1;
let coordinates = self.coordinates.clone();
let file = file.clone();
let redraw = crate::term::reset() + &self.get_redraw_empty_list(0);
//self.threads.install(|| {
@ -66,6 +75,33 @@ impl Previewer {
let output = textview.get_drawlist()
+ &textview.get_redraw_empty_list(len - 1);
write!(std::io::stdout(), "{}", output).unwrap();
} else {
let output =
std::process::Command::new("scope.sh").arg(&file.name)
.arg("10".to_string())
.arg("10".to_string())
.arg("".to_string())
.arg("false".to_string())
.output().unwrap();
if output.status.code().unwrap() == 5 {
write!(std::io::stdout(), "{}", redraw).unwrap();
} else {
let output = std::str::from_utf8(&output.stdout)
.unwrap()
.to_string();
let mut textview = TextView {
lines: output.lines().map(|s| s.to_string()).collect(),
buffer: String::new(),
coordinates: Coordinates::new() };
textview.set_coordinates(&coordinates);
textview.refresh();
let len = textview.lines.len();
let output = textview.get_drawlist()
+ &textview.get_redraw_empty_list(len - 1);
write!(std::io::stdout(), "{}", output).unwrap();
}
}
}

View File

@ -7,6 +7,7 @@ use crate::files::File;
use crate::term::sized_string;
use crate::widget::Widget;
#[derive(PartialEq)]
pub struct TextView {
pub lines: Vec<String>,
pub buffer: String,
@ -60,8 +61,9 @@ impl Widget for TextView {
.enumerate()
.map(|(i, line)| {
format!(
"{}{:xsize$}",
crate::term::goto_xy(xpos, i as u16),
"{}{}{:xsize$}",
crate::term::goto_xy(xpos, i as u16 + 2),
crate::term::reset(),
sized_string(&line, xsize),
xsize = xsize as usize
)

View File

@ -2,7 +2,7 @@ use termion::event::{Event, Key, MouseEvent};
use crate::coordinates::{Coordinates, Position, Size};
pub trait Widget {
pub trait Widget: PartialEq {
//fn render(&self) -> Vec<String>;
fn get_size(&self) -> &Size;
fn get_position(&self) -> &Position;