Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 22, 2021
1 parent 90f158c commit 4c2606c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 59 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

- [Changelog](#changelog)
- [1.1.0](#110)
- [1.0.0](#100)
- [0.3.0](#030)
- [0.2.1](#021)
Expand All @@ -10,6 +11,12 @@

---

## 1.1.0

Released on 22/11/2021

- Compatibility with tui-realm 1.1.0

## 1.0.0

Released on 13/11/2021
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "tui-realm-treeview"
version = "1.0.0"
version = "1.1.0"
authors = ["Christian Visintin"]
edition = "2018"
edition = "2021"
categories = ["command-line-utilities"]
description = "Treeview component for tui-realm"
documentation = "https://docs.rs/tui-realm-treeview"
Expand All @@ -15,13 +15,13 @@ repository = "https://github.com/veeso/tui-realm-treeview"

[dependencies]
orange-trees = "0.1.0"
tuirealm = { version = "^1.0.0", default-features = false, features = [ "derive" ]}
tuirealm = { version = "^1.1.0", default-features = false, features = [ "derive" ]}
unicode-width = "0.1.8"

[dev-dependencies]
crossterm = "0.20"
pretty_assertions = "0.7.2"
tui-realm-stdlib = "^1.0.0"
tui-realm-stdlib = "^1.1.0"

[features]
default = [ "with-crossterm" ]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</p>

<p align="center">Developed by <a href="https://veeso.github.io/" target="_blank">@veeso</a></p>
<p align="center">Current version: 1.0.0 (13/11/2021)</p>
<p align="center">Current version: 1.1.0 (22/11/2021)</p>

<p align="center">
<a href="https://opensource.org/licenses/MIT"
Expand Down Expand Up @@ -95,13 +95,13 @@ It uses the [Orange trees](https://github.com/veeso/orange-trees) engine for imp
### Add tui-realm-treeview to your Cargo.toml 🦀

```toml
tui-realm-treeview = "^1.0.0"
tui-realm-treeview = "^1.1.0"
```

Or if you don't use **Crossterm**, define the backend as you do with tui-realm:

```toml
tui-realm-treeview = { version = "^1.0.0", default-features = false, features = [ "with-termion" ] }
tui-realm-treeview = { version = "^1.1.0", default-features = false, features = [ "with-termion" ] }
```

### Examples 📋
Expand Down
111 changes: 59 additions & 52 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use tuirealm::{
props::{Alignment, AttrValue, Attribute, BorderType, Borders, Color, InputType, Style},
terminal::TerminalBridge,
Application, Component, EventListenerCfg, MockComponent, NoUserEvent, State, StateValue, Sub,
SubClause, SubEventClause, Update, View,
SubClause, SubEventClause, Update,
};
// tui
use tuirealm::tui::layout::{Constraint, Direction as LayoutDirection, Layout};
Expand Down Expand Up @@ -61,6 +61,7 @@ pub enum Id {
}

struct Model {
app: Application<Id, Msg, NoUserEvent>,
path: PathBuf,
tree: Tree,
quit: bool, // Becomes true when the user presses <ESC>
Expand All @@ -70,7 +71,38 @@ struct Model {

impl Model {
fn new(p: &Path) -> Self {
// Setup app
let mut app: Application<Id, Msg, NoUserEvent> = Application::init(
EventListenerCfg::default().default_input_listener(Duration::from_millis(10)),
);
assert!(app
.mount(
Id::FsTree,
Box::new(FsTree::new(Tree::new(Self::dir_tree(p, MAX_DEPTH)), None)),
vec![]
)
.is_ok());
assert!(app
.mount(Id::GoTo, Box::new(GoTo::default()), vec![])
.is_ok());
// Mount global listener which will listen for <ESC>
assert!(app
.mount(
Id::GlobalListener,
Box::new(GlobalListener::default()),
vec![Sub::new(
SubEventClause::Keyboard(KeyEvent {
code: Key::Esc,
modifiers: KeyModifiers::NONE,
}),
SubClause::Always
)]
)
.is_ok());
// We need to give focus to input then
assert!(app.active(&Id::FsTree).is_ok());
Model {
app,
quit: false,
redraw: true,
tree: Tree::new(Self::dir_tree(p, MAX_DEPTH)),
Expand Down Expand Up @@ -118,33 +150,35 @@ impl Model {
node
}

fn view(&mut self, app: &mut Application<Id, Msg, NoUserEvent>) {
fn view(&mut self) {
let _ = self.terminal.raw_mut().draw(|f| {
// Prepare chunks
let chunks = Layout::default()
.direction(LayoutDirection::Vertical)
.margin(1)
.constraints([Constraint::Min(5), Constraint::Length(3)].as_ref())
.split(f.size());
app.view(&Id::FsTree, f, chunks[0]);
app.view(&Id::GoTo, f, chunks[1]);
self.app.view(&Id::FsTree, f, chunks[0]);
self.app.view(&Id::GoTo, f, chunks[1]);
});
}

fn reload_tree(&mut self, view: &mut View<Id, Msg, NoUserEvent>) {
let current_node = match view.state(&Id::FsTree).ok().unwrap() {
fn reload_tree(&mut self) {
let current_node = match self.app.state(&Id::FsTree).ok().unwrap() {
State::One(StateValue::String(id)) => Some(id),
_ => None,
};
// Remount tree
assert!(view.umount(&Id::FsTree).is_ok());
assert!(view
assert!(self.app.umount(&Id::FsTree).is_ok());
assert!(self
.app
.mount(
Id::FsTree,
Box::new(FsTree::new(self.tree.clone(), current_node))
Box::new(FsTree::new(self.tree.clone(), current_node)),
vec![]
)
.is_ok());
assert!(view.active(&Id::FsTree).is_ok());
assert!(self.app.active(&Id::FsTree).is_ok());
}
}

Expand All @@ -153,48 +187,20 @@ fn main() {
let mut model: Model = Model::new(std::env::current_dir().ok().unwrap().as_path());
let _ = model.terminal.enable_raw_mode();
let _ = model.terminal.enter_alternate_screen();
// Setup app
let mut app: Application<Id, Msg, NoUserEvent> = Application::init(
EventListenerCfg::default().default_input_listener(Duration::from_millis(10)),
);
assert!(app
.mount(
Id::FsTree,
Box::new(FsTree::new(model.tree.clone(), None)),
vec![]
)
.is_ok());
assert!(app
.mount(Id::GoTo, Box::new(GoTo::default()), vec![])
.is_ok());
// Mount global listener which will listen for <ESC>
assert!(app
.mount(
Id::GlobalListener,
Box::new(GlobalListener::default()),
vec![Sub::new(
SubEventClause::Keyboard(KeyEvent {
code: Key::Esc,
modifiers: KeyModifiers::NONE,
}),
SubClause::Always
)]
)
.is_ok());
// We need to give focus to input then
assert!(app.active(&Id::FsTree).is_ok());
// let's loop until quit is true
while !model.quit {
// Tick
if let Ok(sz) = app.tick(&mut model, PollStrategy::Once) {
if sz > 0 {
// NOTE: redraw if at least one msg has been processed
model.redraw = true;
if let Ok(messages) = model.app.tick(PollStrategy::Once) {
for msg in messages.into_iter() {
let mut msg = Some(msg);
while msg.is_some() {
msg = model.update(msg);
}
}
}
// Redraw
if model.redraw {
model.view(&mut app);
model.view();
model.redraw = false;
}
}
Expand All @@ -206,37 +212,38 @@ fn main() {

// -- update

impl Update<Id, Msg, NoUserEvent> for Model {
fn update(&mut self, view: &mut View<Id, Msg, NoUserEvent>, msg: Option<Msg>) -> Option<Msg> {
impl Update<Msg> for Model {
fn update(&mut self, msg: Option<Msg>) -> Option<Msg> {
self.redraw = true;
match msg.unwrap_or(Msg::None) {
Msg::AppClose => {
self.quit = true;
None
}
Msg::ExtendDir(path) => {
self.extend_dir(&path, PathBuf::from(path.as_str()).as_path(), MAX_DEPTH);
self.reload_tree(view);
self.reload_tree();
None
}
Msg::GoTo(path) => {
// Go to and reload tree
self.scan_dir(path.as_path());
self.reload_tree(view);
self.reload_tree();
None
}
Msg::GoToUpperDir => {
if let Some(parent) = self.upper_dir() {
self.scan_dir(parent.as_path());
self.reload_tree(view);
self.reload_tree();
}
None
}
Msg::FsTreeBlur => {
assert!(view.active(&Id::GoTo).is_ok());
assert!(self.app.active(&Id::GoTo).is_ok());
None
}
Msg::GoToBlur => {
assert!(view.active(&Id::FsTree).is_ok());
assert!(self.app.active(&Id::FsTree).is_ok());
None
}
Msg::None => None,
Expand Down

0 comments on commit 4c2606c

Please sign in to comment.