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:
parent
1b95fb704f
commit
9cd395d8f7
@ -17,9 +17,9 @@ impl Coordinates {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn size(&self) -> &Size {
|
pub fn size(&self) -> &Size {
|
||||||
// &self.size
|
&self.size
|
||||||
// }
|
}
|
||||||
|
|
||||||
pub fn xsize(&self) -> u16 {
|
pub fn xsize(&self) -> u16 {
|
||||||
self.size.xsize()
|
self.size.xsize()
|
||||||
@ -59,7 +59,7 @@ impl Position {
|
|||||||
pub fn x(&self) -> u16 {
|
pub fn x(&self) -> u16 {
|
||||||
(self.0).1
|
(self.0).1
|
||||||
}
|
}
|
||||||
// pub fn y(&self) -> u16 {
|
pub fn y(&self) -> u16 {
|
||||||
// (self.0).1
|
(self.0).1
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ use crate::listview::ListView;
|
|||||||
use crate::miller_columns::MillerColumns;
|
use crate::miller_columns::MillerColumns;
|
||||||
use crate::widget::Widget;
|
use crate::widget::Widget;
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
pub struct FileBrowser {
|
pub struct FileBrowser {
|
||||||
pub columns: MillerColumns<ListView<Files>>,
|
pub columns: MillerColumns<ListView<Files>>,
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ use std::time::SystemTime;
|
|||||||
|
|
||||||
use lscolors::LsColors;
|
use lscolors::LsColors;
|
||||||
use mime_detective;
|
use mime_detective;
|
||||||
use rayon::prelude::*;
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref COLORS: LsColors = LsColors::from_env().unwrap();
|
static ref COLORS: LsColors = LsColors::from_env().unwrap();
|
||||||
|
142
src/hbox.rs
142
src/hbox.rs
@ -3,62 +3,74 @@ use termion::event::{Event};
|
|||||||
use crate::widget::Widget;
|
use crate::widget::Widget;
|
||||||
use crate::coordinates::{Coordinates, Size, Position};
|
use crate::coordinates::{Coordinates, Size, Position};
|
||||||
|
|
||||||
// pub struct Child<T> {
|
#[derive(PartialEq)]
|
||||||
// widget: T,
|
pub struct HBox<T: Widget> {
|
||||||
// position: (u16, u16),
|
pub coordinates: Coordinates,
|
||||||
// size: (u16, u16),
|
pub widgets: Vec<T>,
|
||||||
// active: bool
|
pub active: Option<usize>,
|
||||||
// }
|
|
||||||
|
|
||||||
pub struct HBox {
|
|
||||||
dimensions: (u16, u16),
|
|
||||||
position: (u16, u16),
|
|
||||||
coordinates: Coordinates,
|
|
||||||
children: Vec<Box<Widget>>,
|
|
||||||
active: usize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl HBox {
|
impl<T> HBox<T> where T: Widget {
|
||||||
pub fn new(widgets: Vec<Box<Widget>>,
|
pub fn new() -> HBox<T> {
|
||||||
dimensions: (u16, u16),
|
HBox { coordinates: Coordinates::new(),
|
||||||
coordinates: Coordinates,
|
widgets: vec![],
|
||||||
position: (u16, u16),
|
active: None
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn resize_children(&mut self) {
|
pub fn resize_children(&mut self) {
|
||||||
let hbox_size = dbg!(self.dimensions);
|
let coords: Vec<Coordinates>
|
||||||
let hbox_position = dbg!(self.position);
|
= self.widgets.iter().map(
|
||||||
let cell_size = dbg!(hbox_size.0 / self.children.len() as u16);
|
|w|
|
||||||
let mut current_pos = dbg!(hbox_position.1);
|
self.calculate_coordinates(w)).collect();
|
||||||
|
for (widget, coord) in self.widgets.iter_mut().zip(coords.iter()) {
|
||||||
for widget in &mut self.children {
|
widget.set_coordinates(coord);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn widget(&self, index: usize) -> &Box<Widget> {
|
pub fn push_widget(&mut self, widget: T) where T: PartialEq {
|
||||||
// &self.children[index]
|
self.widgets.push(widget);
|
||||||
// }
|
self.resize_children();
|
||||||
|
self.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn active_widget(&self) -> &Box<Widget> {
|
pub fn pop_widget(&mut self) -> Option<T> {
|
||||||
&self.children[self.active]
|
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 {
|
impl<T> Widget for HBox<T> where T: Widget {
|
||||||
fn render(&self) -> Vec<String> {
|
|
||||||
// HBox doesnt' draw anything itself
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_header(&self) -> String {
|
fn render_header(&self) -> String {
|
||||||
self.active_widget().render_header()
|
self.active_widget().render_header()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh(&mut self) {
|
fn refresh(&mut self) {
|
||||||
for child in &mut self.children {
|
self.resize_children();
|
||||||
|
for child in &mut self.widgets {
|
||||||
child.refresh();
|
child.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_drawlist(&self) -> String {
|
fn get_drawlist(&self) -> String {
|
||||||
self.children.iter().map(|child| {
|
self.widgets.iter().map(|child| {
|
||||||
child.get_drawlist()
|
child.get_drawlist()
|
||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_size(&self) -> Size {
|
fn get_size(&self) -> &Size {
|
||||||
Size( self.dimensions )
|
&self.coordinates.size
|
||||||
}
|
}
|
||||||
fn get_position(&self) -> Position {
|
fn get_position(&self) -> &Position {
|
||||||
Position ( self.position )
|
&self.coordinates.position
|
||||||
}
|
}
|
||||||
fn set_size(&mut self, size: Size) {
|
fn set_size(&mut self, size: Size) {
|
||||||
self.dimensions = size.0;
|
self.coordinates.size = size;
|
||||||
}
|
}
|
||||||
fn set_position(&mut self, position: Position) {
|
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) {
|
fn on_event(&mut self, event: Event) {
|
||||||
self.children[self.active].on_event(event);
|
self.widgets.last_mut().unwrap().on_event(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ use crate::widget::Widget;
|
|||||||
|
|
||||||
// Maybe also buffer drawlist for efficiency when it doesn't change every draw
|
// Maybe also buffer drawlist for efficiency when it doesn't change every draw
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
pub struct ListView<T>
|
pub struct ListView<T>
|
||||||
where
|
where
|
||||||
T: Send,
|
T: Send,
|
||||||
|
@ -25,6 +25,7 @@ mod textview;
|
|||||||
mod widget;
|
mod widget;
|
||||||
mod win_main;
|
mod win_main;
|
||||||
mod window;
|
mod window;
|
||||||
|
mod hbox;
|
||||||
|
|
||||||
use window::Window;
|
use window::Window;
|
||||||
|
|
||||||
|
@ -3,9 +3,11 @@ use termion::event::Key;
|
|||||||
use crate::coordinates::{Coordinates, Position, Size};
|
use crate::coordinates::{Coordinates, Position, Size};
|
||||||
use crate::preview::Previewer;
|
use crate::preview::Previewer;
|
||||||
use crate::widget::Widget;
|
use crate::widget::Widget;
|
||||||
|
use crate::hbox::HBox;
|
||||||
|
|
||||||
pub struct MillerColumns<T> {
|
#[derive(PartialEq)]
|
||||||
pub widgets: Vec<T>,
|
pub struct MillerColumns<T> where T: Widget {
|
||||||
|
pub widgets: HBox<T>,
|
||||||
// pub left: Option<T>,
|
// pub left: Option<T>,
|
||||||
// pub main: Option<T>,
|
// pub main: Option<T>,
|
||||||
pub preview: Previewer,
|
pub preview: Previewer,
|
||||||
@ -19,30 +21,26 @@ where
|
|||||||
{
|
{
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
widgets: vec![],
|
widgets: HBox::new(),
|
||||||
coordinates: Coordinates::new(),
|
coordinates: Coordinates::new(),
|
||||||
ratio: (33, 33, 33),
|
ratio: (33, 33, 33),
|
||||||
preview: Previewer::new(),
|
preview: Previewer::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_widget(&mut self, mut widget: T) {
|
pub fn push_widget(&mut self, widget: T) {
|
||||||
let mcoords = self.calculate_coordinates().1;
|
self.widgets.push_widget(widget);
|
||||||
widget.set_coordinates(&mcoords);
|
|
||||||
self.widgets.push(widget);
|
|
||||||
self.refresh();
|
self.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop_widget(&mut self) -> Option<T> {
|
pub fn pop_widget(&mut self) -> Option<T> {
|
||||||
let widget = self.widgets.pop();
|
let widget = self.widgets.pop_widget();
|
||||||
self.refresh();
|
self.refresh();
|
||||||
widget
|
widget
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prepend_widget(&mut self, mut widget: T) {
|
pub fn prepend_widget(&mut self, widget: T) {
|
||||||
let lcoords = self.calculate_coordinates().0;
|
self.widgets.prepend_widget(widget);
|
||||||
widget.set_coordinates(&lcoords);
|
|
||||||
self.widgets.insert(0, widget);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calculate_coordinates(&self) -> (Coordinates, Coordinates, Coordinates) {
|
pub fn calculate_coordinates(&self) -> (Coordinates, Coordinates, Coordinates) {
|
||||||
@ -82,25 +80,25 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_left_widget(&self) -> Option<&T> {
|
pub fn get_left_widget(&self) -> Option<&T> {
|
||||||
let len = self.widgets.len();
|
let len = self.widgets.widgets.len();
|
||||||
if len < 2 {
|
if len < 2 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
self.widgets.get(len - 2)
|
self.widgets.widgets.get(len - 2)
|
||||||
}
|
}
|
||||||
pub fn get_left_widget_mut(&mut self) -> Option<&mut T> {
|
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 {
|
if len < 2 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
self.widgets.get(len - 2)?.get_position();
|
self.widgets.widgets.get(len - 2)?.get_position();
|
||||||
self.widgets.get_mut(len - 2)
|
self.widgets.widgets.get_mut(len - 2)
|
||||||
}
|
}
|
||||||
pub fn get_main_widget(&self) -> &T {
|
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 {
|
pub fn get_main_widget_mut(&mut self) -> &mut T {
|
||||||
self.widgets.last_mut().unwrap()
|
self.widgets.widgets.last_mut().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
use rayon as rayon;
|
use std::io::Write;
|
||||||
|
|
||||||
use std::io::{stdout, Write};
|
|
||||||
use std::sync::atomic::AtomicUsize;
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use crate::coordinates::{Coordinates, Position, Size};
|
use crate::coordinates::{Coordinates, Position, Size};
|
||||||
@ -10,11 +7,23 @@ use crate::listview::ListView;
|
|||||||
use crate::textview::TextView;
|
use crate::textview::TextView;
|
||||||
use crate::widget::Widget;
|
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 struct Previewer {
|
||||||
pub file: Option<File>,
|
pub file: Option<File>,
|
||||||
pub buffer: String,
|
pub buffer: String,
|
||||||
pub coordinates: Coordinates,
|
pub coordinates: Coordinates,
|
||||||
pub instances: Mutex<usize>
|
pub instances: InstanceCounter
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Previewer {
|
impl Previewer {
|
||||||
@ -23,17 +32,17 @@ impl Previewer {
|
|||||||
file: None,
|
file: None,
|
||||||
buffer: String::new(),
|
buffer: String::new(),
|
||||||
coordinates: Coordinates::new(),
|
coordinates: Coordinates::new(),
|
||||||
instances: From::from(0)
|
instances: InstanceCounter(Mutex::new(0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn set_file(&mut self, file: &File) {
|
pub fn set_file(&mut self, file: &File) {
|
||||||
//return;
|
//return;
|
||||||
let mut instance = self.instances.try_lock().unwrap();
|
let mut instance = self.instances.0.try_lock().unwrap();
|
||||||
if *instance > 2 { return }
|
if *instance > 2 { return }
|
||||||
*instance = *instance + 1;
|
*instance = *instance + 1;
|
||||||
let coordinates = self.coordinates.clone();
|
let coordinates = self.coordinates.clone();
|
||||||
let file = file.clone();
|
let file = file.clone();
|
||||||
|
let redraw = crate::term::reset() + &self.get_redraw_empty_list(0);
|
||||||
|
|
||||||
|
|
||||||
//self.threads.install(|| {
|
//self.threads.install(|| {
|
||||||
@ -66,6 +75,33 @@ impl Previewer {
|
|||||||
let output = textview.get_drawlist()
|
let output = textview.get_drawlist()
|
||||||
+ &textview.get_redraw_empty_list(len - 1);
|
+ &textview.get_redraw_empty_list(len - 1);
|
||||||
write!(std::io::stdout(), "{}", output).unwrap();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use crate::files::File;
|
|||||||
use crate::term::sized_string;
|
use crate::term::sized_string;
|
||||||
use crate::widget::Widget;
|
use crate::widget::Widget;
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
pub struct TextView {
|
pub struct TextView {
|
||||||
pub lines: Vec<String>,
|
pub lines: Vec<String>,
|
||||||
pub buffer: String,
|
pub buffer: String,
|
||||||
@ -60,8 +61,9 @@ impl Widget for TextView {
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, line)| {
|
.map(|(i, line)| {
|
||||||
format!(
|
format!(
|
||||||
"{}{:xsize$}",
|
"{}{}{:xsize$}",
|
||||||
crate::term::goto_xy(xpos, i as u16),
|
crate::term::goto_xy(xpos, i as u16 + 2),
|
||||||
|
crate::term::reset(),
|
||||||
sized_string(&line, xsize),
|
sized_string(&line, xsize),
|
||||||
xsize = xsize as usize
|
xsize = xsize as usize
|
||||||
)
|
)
|
||||||
|
@ -2,7 +2,7 @@ use termion::event::{Event, Key, MouseEvent};
|
|||||||
|
|
||||||
use crate::coordinates::{Coordinates, Position, Size};
|
use crate::coordinates::{Coordinates, Position, Size};
|
||||||
|
|
||||||
pub trait Widget {
|
pub trait Widget: PartialEq {
|
||||||
//fn render(&self) -> Vec<String>;
|
//fn render(&self) -> Vec<String>;
|
||||||
fn get_size(&self) -> &Size;
|
fn get_size(&self) -> &Size;
|
||||||
fn get_position(&self) -> &Position;
|
fn get_position(&self) -> &Position;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user