Skip to content

Commit

Permalink
Add StatusBar
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jan 11, 2024
1 parent b987466 commit 82aa9b6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ pub enum Action {
SubmitTextNote,
SendTextNote(String),
Key(KeyEvent),
MetadataUpdated(Metadata),
}
6 changes: 4 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::sync::mpsc;

use crate::{
action::Action,
components::{Component, FpsCounter, Home},
components::{Component, FpsCounter, Home, StatusBar},
config::Config,
mode::Mode,
nostr::Connection,
Expand All @@ -31,11 +31,13 @@ impl App {
let home = Home::new();
let fps = FpsCounter::default();
let config = Config::new()?;
let pubkey = Keys::from_sk_str(config.privatekey.as_str())?.public_key();
let status_bar = StatusBar::new(pubkey, None);
let mode = Mode::Home;
Ok(Self {
tick_rate,
frame_rate,
components: vec![Box::new(home), Box::new(fps)],
components: vec![Box::new(home), Box::new(fps), Box::new(status_bar)],
should_quit: false,
should_suspend: false,
config,
Expand Down
2 changes: 2 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use crate::{

mod fps;
mod home;
mod status_bar;

pub use fps::FpsCounter;
pub use home::Home;
pub use status_bar::StatusBar;

/// `Component` is a trait that represents a visual and interactive element of the user interface.
/// Implementors of this trait can be registered with the main application loop and will be able to receive events,
Expand Down
2 changes: 1 addition & 1 deletion src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<'a> Component for Home<'a> {
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
let padding = Padding::new(1, 1, 1, 1);
let padding = Padding::new(1, 1, 1, 2);
let items: Vec<TextNote> = self
.notes
.iter()
Expand Down
76 changes: 76 additions & 0 deletions src/components/status_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use color_eyre::eyre::Result;
use nostr_sdk::prelude::*;
use ratatui::{prelude::*, widgets::*};

use crate::action::Action;
use crate::components::Component;
use crate::nostr::Metadata;
use crate::tui::Frame;
use crate::widgets::PublicKey;

pub struct StatusBar {
pubkey: XOnlyPublicKey,
metadata: Option<Metadata>,
}

impl StatusBar {
pub fn new(pubkey: XOnlyPublicKey, metadata: Option<Metadata>) -> Self {
Self { pubkey, metadata }
}

pub fn set_metadata(&mut self, metadata: Option<Metadata>) {
self.metadata = metadata;
}

pub fn name(&self) -> String {
self.metadata
.clone()
.and_then(|metadata| match (metadata.name, metadata.display_name) {
(Some(name), _) if !name.is_empty() => Some(format!("@{name}")),
(_, Some(display_name)) if !display_name.is_empty() => Some(display_name),
(_, _) => None,
})
.unwrap_or(PublicKey::new(self.pubkey).shortened())
}
}

impl Component for StatusBar {
fn update(&mut self, action: Action) -> Result<Option<Action>> {
if let Action::ReceiveEvent(ev) = action {
match ev.kind {
Kind::Metadata if ev.pubkey == self.pubkey => {
let maybe_metadata = Metadata::from_json(ev.content);
if let Ok(metadata) = maybe_metadata {
self.set_metadata(Some(metadata));
}
}
_ => {}
};
}

Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
let layout = Layout::new(
Direction::Vertical,
[
Constraint::Min(0),
Constraint::Length(1),
// Constraint::Length(1),
],
)
.split(area);
f.render_widget(Clear, layout[1]);
// f.render_widget(Clear, layout[2]);

let name = Span::styled(self.name(), Style::default().fg(Color::Gray).italic());
let status_line = Paragraph::new(name).style(Style::default().bg(Color::Black));
f.render_widget(status_line, layout[1]);

// TODO
// f.render_widget(notification_line, layout[1]);

Ok(())
}
}

0 comments on commit 82aa9b6

Please sign in to comment.