Skip to content

Commit

Permalink
Merge pull request #2 from veeso/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
veeso committed Jun 7, 2021
2 parents 1688324 + 6eeaae2 commit ce72f7b
Show file tree
Hide file tree
Showing 6 changed files with 612 additions and 71 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
# Changelog

- [Changelog](#changelog)
- [0.2.0](#020)
- [0.1.1](#011)
- [0.1.0](#010)

---

## 0.2.0

Released on 07/06/2021

- **Keep state property**:
- Possibility to keep active the selected node after an update
- **With node property**:
- Set the id of the default active node in the properties
- **PAGE_UP** and **PAGE_DOWN** keys
- Advance by remaining siblings forward or backward
- You can set a maximum amount of steps with `with_steps` in props
- Dependencies:
- `tui-realm` updated to `0.4.0`

## 0.1.1

Released on 07/06/2021
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tui-realm-treeview"
version = "0.1.1"
version = "0.2.0"
authors = ["Christian Visintin"]
edition = "2018"
categories = ["command-line-utilities"]
Expand All @@ -14,13 +14,13 @@ readme = "README.md"
repository = "https://github.com/veeso/tui-realm-treeview"

[dependencies]
tuirealm = "^0.3.2"
tuirealm = "^0.4.0"
tui-tree-widget = "^0.6.0"

[dev-dependencies]
crossterm = "0.19.0"
pretty_assertions = "0.7.2"
tuirealm = { version = "^0.3.2", features = [ "with-components" ] }
tuirealm = { version = "^0.4.0", features = [ "with-components" ] }

[[example]]
name = "demo"
Expand Down
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<img src="docs/images/tui-realm-treeview.svg" width="256" height="256" />
</p>

[![License: MIT](https://img.shields.io/badge/License-MIT-teal.svg)](https://opensource.org/licenses/MIT) [![Stars](https://img.shields.io/github/stars/veeso/tui-realm-treeview.svg)](https://github.com/veeso/tui-realm-treeview) [![Downloads](https://img.shields.io/crates/d/tui-realm-treeview.svg)](https://crates.io/crates/tui-realm-treeview) [![Crates.io](https://img.shields.io/badge/crates.io-v0.1.1-orange.svg)](https://crates.io/crates/tui-realm-treeview) [![Docs](https://docs.rs/tui-realm-treeview/badge.svg)](https://docs.rs/tui-realm-treeview)
[![License: MIT](https://img.shields.io/badge/License-MIT-teal.svg)](https://opensource.org/licenses/MIT) [![Stars](https://img.shields.io/github/stars/veeso/tui-realm-treeview.svg)](https://github.com/veeso/tui-realm-treeview) [![Downloads](https://img.shields.io/crates/d/tui-realm-treeview.svg)](https://crates.io/crates/tui-realm-treeview) [![Crates.io](https://img.shields.io/badge/crates.io-v0.2.0-orange.svg)](https://crates.io/crates/tui-realm-treeview) [![Docs](https://docs.rs/tui-realm-treeview/badge.svg)](https://docs.rs/tui-realm-treeview)

[![Build](https://github.com/veeso/tui-realm-treeview/workflows/Linux/badge.svg)](https://github.com/veeso/tui-realm-treeview/actions) [![Build](https://github.com/veeso/tui-realm-treeview/workflows/MacOS/badge.svg)](https://github.com/veeso/tui-realm-treeview/actions) [![Build](https://github.com/veeso/tui-realm-treeview/workflows/Windows/badge.svg)](https://github.com/veeso/tui-realm-treeview/actions) [![Coverage Status](https://coveralls.io/repos/github/veeso/tui-realm-treeview/badge.svg?branch=main)](https://coveralls.io/github/veeso/tui-realm-treeview?branch=main)

Developed by Christian Visintin
Current version: 0.1.1 (07/06/2021)
Current version: 0.2.0 (07/06/2021)

---

Expand All @@ -19,6 +19,7 @@ Current version: 0.1.1 (07/06/2021)
- [Add tui-realm-treeview to your Cargo.toml 🦀](#add-tui-realm-treeview-to-your-cargotoml-)
- [Use the treeview component](#use-the-treeview-component)
- [About performance](#about-performance)
- [Behaviour](#behaviour)
- [Documentation 📚](#documentation-)
- [Contributing and issues 🤝🏻](#contributing-and-issues-)
- [Changelog ⏳](#changelog-)
Expand All @@ -40,7 +41,7 @@ tui-realm-treeview is an implementation of a **treeview component** for [tui-rea
### Add tui-realm-treeview to your Cargo.toml 🦀

```toml
tui-realm-treeview = "0.1.1"
tui-realm-treeview = "0.2.0"
```

### Use the treeview component
Expand All @@ -51,9 +52,10 @@ View how to use the treeview-component following the [example](examples/demo.rs)
cargo run --example demo
```

- Press `ENTER` to change directory
- Press `ENTER` to expand the selected directory
- Press `BACKSPACE` to go to upper directory
- Move up and down with `UP/DOWN` arrow keys
- Advance by up to 8 entries with `PGUP/PGDOWN`
- Open directories with `RIGHT`
- Close directories with `LEFT`
- Change window between input field and treeview with `TAB`
Expand All @@ -66,7 +68,40 @@ In this library there is a consistent use of recursion, and since rust is not fu
Best practices:

- Except when dealing with small trees, always set a depth for the tree
- For file systems, depth 3 should be fine
- For file systems, depth 3 should be fine for each directory, then expand each directory on demand as implemented in the example

---

## Behaviour

**Events**:

| Event | Message | Behaviour |
|---------------------|------------|------------------------------------------------------|
| `KeyCode::Down` | `OnChange` | Go to next element |
| `KeyCode::Up` | `OnChange` | Go to previous element |
| `KeyCode::PageDown` | `OnChange` | Move cursor down by defined max steps or end of node |
| `KeyCode::PageUp` | `OnChange` | Move cursor up by defined max steps or begin of node |
| `KeyCode::Right` | `OnChange` | Open highlighted node |
| `KeyCode::Left` | `OnChange` | Close highlighted node |
| `KeyCode::Char(_)` | `OnKey` | Return pressed key |

**Update**: `Msg::OnChange` if the value changed, `Msg::None` otherwise.

**State**: the state returned is a `One(Str)` containing the id of the selected node. If no node is selected `None` is returned.

**Properties**:

- `with_foreground`: foreground color. The foreground will be used as foreground for the selected item, when focus is false, otherwise as background
- `with_background`: background color. The background color will be used as background for unselected entry, but will be used as foreground for the selected entry when focus is true
- `with_borders`: set borders properties for component
- `with_title`: Set box title
- `with_highlighted_str`: The provided string will be displayed on the left side of the selected entry in the tree
- `with_tree_and_depth`: Sets the tree and its max depth for Props builder
- `with_tree`: Sets the tree for Props builder
- `with_node`: Select initial node in the tree. This option has priority over `keep_state`
- `keep_state`: If keep is true, the selected entry will be kept after an update of the tree (obviously if the entry still exists in the tree).
- `with_max_page_steps`: Defines the maximum amount of steps to perform after a PG_DOWN / PG_UP

---

Expand Down
29 changes: 26 additions & 3 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const COMPONENT_INPUT: &str = "INPUT";
const COMPONENT_LABEL: &str = "LABEL";
const COMPONENT_TREEVIEW: &str = "TREEVIEW";

const MAX_DEPTH: usize = 3;

struct Model {
path: PathBuf,
tree: Tree,
Expand All @@ -55,7 +57,7 @@ impl Model {
quit: false,
redraw: true,
view,
tree: Tree::new(Self::dir_tree(p, 3)),
tree: Tree::new(Self::dir_tree(p, MAX_DEPTH)),
path: p.to_path_buf(),
}
}
Expand All @@ -74,13 +76,28 @@ impl Model {

pub fn scan_dir(&mut self, p: &Path) {
self.path = p.to_path_buf();
self.tree = Tree::new(Self::dir_tree(p, 3));
self.tree = Tree::new(Self::dir_tree(p, MAX_DEPTH));
}

pub fn upper_dir(&self) -> Option<&Path> {
self.path.parent()
}

pub fn extend_dir(&mut self, id: &str, p: &Path, depth: usize) {
if let Some(node) = self.tree.query_mut(id) {
if depth > 0 && p.is_dir() {
// Clear node
node.clear();
// Scan dir
if let Ok(e) = std::fs::read_dir(p) {
e.flatten().for_each(|x| {
node.add_child(Self::dir_tree(x.path().as_path(), depth - 1))
});
}
}
}
}

fn dir_tree(p: &Path, depth: usize) -> Node {
let name: String = match p.file_name() {
None => "/".to_string(),
Expand Down Expand Up @@ -144,6 +161,8 @@ fn main() {
.with_title(Some(title))
.with_tree(model.tree.root())
.with_highlighted_str("🚀")
.keep_state(true)
.with_max_page_steps(8)
.build(),
)),
);
Expand Down Expand Up @@ -211,7 +230,11 @@ impl Update for Model {
}
(COMPONENT_TREEVIEW, Msg::OnSubmit(Payload::One(Value::Str(node_id)))) => {
// Update tree
self.scan_dir(PathBuf::from(node_id.as_str()).as_path());
self.extend_dir(
node_id.as_str(),
PathBuf::from(node_id.as_str()).as_path(),
MAX_DEPTH,
);
// Update
let props = TreeViewPropsBuilder::from(
self.view.get_props(COMPONENT_TREEVIEW).unwrap(),
Expand Down
Loading

0 comments on commit ce72f7b

Please sign in to comment.