Skip to content

Commit

Permalink
dnetview: replace tui dependency with ratatui
Browse files Browse the repository at this point in the history
As per fdehau/tui-rs#654, tui-rs is no longer
being actively maintained. This commit updates dnetview to work with
ratatui, a community-maintained fork of tui-rs.
  • Loading branch information
lunar-mining committed Aug 6, 2023
1 parent 51bfdfd commit 8e020cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
3 changes: 2 additions & 1 deletion bin/dnetview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ features = ["rpc"]
[dependencies]
# Tui
termion = "2.0.1"
tui = {version = "0.19.0", features = ["termion"]}
#tui = {version = "0.19.0", features = ["termion"]}
ratatui = { version = "0.22.0", features = ["all-widgets", "termion"]}

# Async
smol = "1.3.0"
Expand Down
2 changes: 1 addition & 1 deletion bin/dnetview/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use log::info;
use simplelog::*;
use smol::Executor;
use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
use tui::{
use ratatui::{
backend::{Backend, TermionBackend},
Terminal,
};
Expand Down
56 changes: 28 additions & 28 deletions bin/dnetview/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use log::debug;
use std::collections::HashMap;

use tui::{
use ratatui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans},
text::{Span, Line},
widgets::{Block, Borders, List, ListItem, ListState, Paragraph},
Frame,
};
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<'a> View {
.constraints(cnstrnts)
.split(f.size());

self.render_left(f, slice.clone())?;
self.render_left(f, slice[0])?;
if self.ordered_list.is_empty() {
// we have not received any data
Ok(())
Expand All @@ -172,7 +172,7 @@ impl<'a> View {
Some(i) => match self.ordered_list.get(i) {
Some(i) => {
let id = i.clone();
self.render_right(f, slice, id)?;
self.render_right(f, slice[1], id)?;
Ok(())
}
None => Err(DnetViewError::NoIdAtIndex),
Expand All @@ -186,7 +186,7 @@ impl<'a> View {
fn render_left<B: Backend>(
&mut self,
f: &mut Frame<'_, B>,
slice: Vec<Rect>,
slice: Rect,
) -> DnetViewResult<()> {
let style = Style::default();
let mut nodes = Vec::new();
Expand All @@ -201,7 +201,7 @@ impl<'a> View {
name.push_str(&node.name);
name.push_str("(Offline)");
let name_span = Span::styled(name, style);
let lines = vec![Spans::from(name_span)];
let lines = vec![Line::from(name_span)];
let names = ListItem::new(lines);
nodes.push(names);
} else {
Expand All @@ -213,18 +213,18 @@ impl<'a> View {
name.push_str(&node.name);
name.push_str("(dnetview is not enabled)");
let name_span = Span::styled(name, style);
let lines = vec![Spans::from(name_span)];
let lines = vec![Line::from(name_span)];
let names = ListItem::new(lines);
nodes.push(names);
} else {
let name_span = Span::raw(&node.name);
let lines = vec![Spans::from(name_span)];
let lines = vec![Line::from(name_span)];
let names = ListItem::new(lines);
nodes.push(names);

if !node.inbound.is_empty() {
let name = Span::styled(format!(" Inbound"), style);
let lines = vec![Spans::from(name)];
let lines = vec![Line::from(name)];
let names = ListItem::new(lines);
nodes.push(names);

Expand Down Expand Up @@ -254,15 +254,15 @@ impl<'a> View {
}
}
}
let lines = vec![Spans::from(infos)];
let lines = vec![Line::from(infos)];
let names = ListItem::new(lines);
nodes.push(names);
}
}

if !&node.outbound.is_empty() {
let name = Span::styled(format!(" Outbound"), style);
let lines = vec![Spans::from(name)];
let lines = vec![Line::from(name)];
let names = ListItem::new(lines);
nodes.push(names);

Expand Down Expand Up @@ -292,7 +292,7 @@ impl<'a> View {
}
}
}
let lines = vec![Spans::from(infos)];
let lines = vec![Line::from(infos)];
let names = ListItem::new(lines);
nodes.push(names);
}
Expand All @@ -302,12 +302,12 @@ impl<'a> View {
}
SelectableObject::Lilith(lilith) => {
let name_span = Span::raw(&lilith.name);
let lines = vec![Spans::from(name_span)];
let lines = vec![Line::from(name_span)];
let names = ListItem::new(lines);
nodes.push(names);
for network in &lilith.networks {
let name = Span::styled(format!(" {}", network.name), style);
let lines = vec![Spans::from(name)];
let lines = vec![Line::from(name)];
let names = ListItem::new(lines);
nodes.push(names);
}
Expand All @@ -318,7 +318,7 @@ impl<'a> View {
let nodes =
List::new(nodes).block(Block::default().borders(Borders::ALL)).highlight_symbol(">> ");

f.render_stateful_widget(nodes, slice[0], &mut self.id_menu.state);
f.render_stateful_widget(nodes, slice, &mut self.id_menu.state);

Ok(())
}
Expand Down Expand Up @@ -358,7 +358,7 @@ impl<'a> View {
fn render_right<B: Backend>(
&mut self,
f: &mut Frame<'_, B>,
slice: Vec<Rect>,
slice: Rect,
selected: String,
) -> DnetViewResult<()> {
debug!(target: "dnetview", "render_right() selected ID: {}", selected.clone());
Expand All @@ -373,39 +373,39 @@ impl<'a> View {

match info {
Some(SelectableObject::Node(node)) => {
lines.push(Spans::from(Span::styled("Type: Normal", style)));
lines.push(Spans::from(Span::styled("Hosts:", style)));
lines.push(Line::from(Span::styled("Type: Normal", style)));
lines.push(Line::from(Span::styled("Hosts:", style)));
for host in &node.hosts {
lines.push(Spans::from(Span::styled(format!(" {}", host), style)));
lines.push(Line::from(Span::styled(format!(" {}", host), style)));
}
}
Some(SelectableObject::Session(session)) => {
let addr = Span::styled(format!("Addr: {}", session.addr), style);
lines.push(Spans::from(addr));
lines.push(Line::from(addr));

if session.state.is_some() {
let addr = Span::styled(
format!("State: {}", session.state.as_ref().unwrap()),
style,
);
lines.push(Spans::from(addr));
lines.push(Line::from(addr));
}
}
Some(SelectableObject::Slot(slot)) => {
let text = self.parse_msg_list(slot.dnet_id.clone())?;
f.render_stateful_widget(text, slice[1], &mut self.msg_list.state);
f.render_stateful_widget(text, slice, &mut self.msg_list.state);
}
Some(SelectableObject::Lilith(_lilith)) => {
lines.push(Spans::from(Span::styled("Type: Lilith", style)));
lines.push(Line::from(Span::styled("Type: Lilith", style)));
}
Some(SelectableObject::Network(network)) => {
lines.push(Spans::from(Span::styled("URLs:", style)));
lines.push(Line::from(Span::styled("URLs:", style)));
for url in &network.urls {
lines.push(Spans::from(Span::styled(format!(" {}", url), style)));
lines.push(Line::from(Span::styled(format!(" {}", url), style)));
}
lines.push(Spans::from(Span::styled("Hosts:", style)));
lines.push(Line::from(Span::styled("Hosts:", style)));
for node in &network.nodes {
lines.push(Spans::from(Span::styled(format!(" {}", node), style)));
lines.push(Line::from(Span::styled(format!(" {}", node), style)));
}
}
None => return Err(DnetViewError::NotSelectableObject),
Expand All @@ -416,7 +416,7 @@ impl<'a> View {
.block(Block::default().borders(Borders::ALL))
.style(Style::default());

f.render_widget(graph, slice[1]);
f.render_widget(graph, slice);

Ok(())
}
Expand Down

0 comments on commit 8e020cb

Please sign in to comment.