feat: signed friend profile frames and TUI /profile /who
prf frames are ed25519-signed, friend-visible only (unknown pubkey is ignored). /profile edits self; F5 and /who show the selected friend's last profile.
This commit is contained in:
parent
ef78be4035
commit
d304cc4c3b
7 changed files with 732 additions and 22 deletions
253
src/tui.rs
253
src/tui.rs
|
|
@ -9,7 +9,7 @@ use ratatui::{DefaultTerminal, Frame};
|
|||
|
||||
use crate::node::Node;
|
||||
use crate::qr::{self, QrPayload};
|
||||
use crate::store::Friend;
|
||||
use crate::store::{Friend, FriendProfile};
|
||||
|
||||
const C_ACCENT: Color = Color::Rgb(232, 165, 75);
|
||||
const C_TEXT: Color = Color::Rgb(196, 214, 176);
|
||||
|
|
@ -59,17 +59,47 @@ OnionWire keys\n\
|
|||
1 / 2 / 3 focus roster / chat / composer\n\
|
||||
j k or Up Down move or scroll focused pane\n\
|
||||
g / G jump to top / bottom\n\
|
||||
Enter /wipe /wipe-all from composer\n\
|
||||
Enter /wipe /wipe-all /profile /who\n\
|
||||
Esc close overlay, go back, clear composer\n\
|
||||
F2 share QR\n\
|
||||
F3 paste a friend QR\n\
|
||||
F4 rotate onion locator\n\
|
||||
F5 selected friend's profile (/who)\n\
|
||||
/profile edit name, bio, Monero address\n\
|
||||
? this help\n\
|
||||
Ctrl-Q quit\n\
|
||||
\n\
|
||||
Esc closes this overlay"
|
||||
}
|
||||
|
||||
pub fn who_overlay_text(fingerprint: &str, onion: &str, profile: Option<&FriendProfile>) -> String {
|
||||
match profile {
|
||||
None => format!(
|
||||
"friend profile\n\nfp {fingerprint}\nonion {onion}\n\nno profile yet\n\nEsc closes"
|
||||
),
|
||||
Some(p) => {
|
||||
let name = if p.display_name.is_empty() {
|
||||
"(none)"
|
||||
} else {
|
||||
p.display_name.as_str()
|
||||
};
|
||||
let bio = if p.bio.is_empty() {
|
||||
"(none)"
|
||||
} else {
|
||||
p.bio.as_str()
|
||||
};
|
||||
let xmr = if p.xmr_addr.is_empty() {
|
||||
"(none)"
|
||||
} else {
|
||||
p.xmr_addr.as_str()
|
||||
};
|
||||
format!(
|
||||
"friend profile\n\nname {name}\nbio {bio}\nxmr {xmr}\nfp {fingerprint}\nonion {onion}\n\nEsc closes"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn banner_for_width(width: u16) -> &'static str {
|
||||
let full = wordmark_banner();
|
||||
let max_line = full.lines().map(|l| l.chars().count()).max().unwrap_or(0);
|
||||
|
|
@ -111,6 +141,90 @@ pub enum WipeKind {
|
|||
All,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum SlashCmd {
|
||||
Wipe(WipeKind),
|
||||
Profile,
|
||||
Who,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ProfileDraft {
|
||||
pub display_name: String,
|
||||
pub bio: String,
|
||||
pub xmr_addr: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProfileEditor {
|
||||
name: String,
|
||||
bio: String,
|
||||
xmr: String,
|
||||
field: usize,
|
||||
}
|
||||
|
||||
impl ProfileEditor {
|
||||
pub fn new(display_name: &str, bio: &str, xmr_addr: &str) -> Self {
|
||||
Self {
|
||||
name: display_name.to_string(),
|
||||
bio: bio.to_string(),
|
||||
xmr: xmr_addr.to_string(),
|
||||
field: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_enter(&self) -> ProfileDraft {
|
||||
ProfileDraft {
|
||||
display_name: self.name.clone(),
|
||||
bio: self.bio.clone(),
|
||||
xmr_addr: self.xmr.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_tab(&mut self) {
|
||||
self.field = (self.field + 1) % 3;
|
||||
}
|
||||
|
||||
pub fn on_backtab(&mut self) {
|
||||
self.field = (self.field + 2) % 3;
|
||||
}
|
||||
|
||||
pub fn on_char(&mut self, c: char) {
|
||||
if c == '\n' {
|
||||
return;
|
||||
}
|
||||
self.field_mut().push(c);
|
||||
}
|
||||
|
||||
pub fn on_backspace(&mut self) {
|
||||
self.field_mut().pop();
|
||||
}
|
||||
|
||||
pub fn field(&self) -> usize {
|
||||
self.field
|
||||
}
|
||||
|
||||
pub fn display_name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn bio(&self) -> &str {
|
||||
&self.bio
|
||||
}
|
||||
|
||||
pub fn xmr_addr(&self) -> &str {
|
||||
&self.xmr
|
||||
}
|
||||
|
||||
fn field_mut(&mut self) -> &mut String {
|
||||
match self.field {
|
||||
0 => &mut self.name,
|
||||
1 => &mut self.bio,
|
||||
_ => &mut self.xmr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WipeDecision {
|
||||
Pending,
|
||||
|
|
@ -124,10 +238,19 @@ pub enum AppExit {
|
|||
WipeAll,
|
||||
}
|
||||
|
||||
pub fn parse_slash(raw: &str) -> Option<WipeKind> {
|
||||
pub fn parse_cmd(raw: &str) -> Option<SlashCmd> {
|
||||
match raw.trim() {
|
||||
"/wipe" => Some(WipeKind::Messages),
|
||||
"/wipe-all" => Some(WipeKind::All),
|
||||
"/wipe" => Some(SlashCmd::Wipe(WipeKind::Messages)),
|
||||
"/wipe-all" => Some(SlashCmd::Wipe(WipeKind::All)),
|
||||
"/profile" => Some(SlashCmd::Profile),
|
||||
"/who" => Some(SlashCmd::Who),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_slash(raw: &str) -> Option<WipeKind> {
|
||||
match parse_cmd(raw) {
|
||||
Some(SlashCmd::Wipe(k)) => Some(k),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
@ -262,6 +385,7 @@ struct App {
|
|||
composer: String,
|
||||
focus: Pane,
|
||||
help_open: bool,
|
||||
who_open: bool,
|
||||
chat_scroll: u16,
|
||||
}
|
||||
|
||||
|
|
@ -272,6 +396,7 @@ enum Screen {
|
|||
Approve { payload: QrPayload },
|
||||
Rotate { prompt: RotatePrompt },
|
||||
Wipe { kind: WipeKind, prompt: WipePrompt },
|
||||
Profile { editor: ProfileEditor },
|
||||
}
|
||||
|
||||
impl App {
|
||||
|
|
@ -290,6 +415,7 @@ impl App {
|
|||
composer: String::new(),
|
||||
focus: Pane::Composer,
|
||||
help_open: false,
|
||||
who_open: false,
|
||||
chat_scroll: 0,
|
||||
})
|
||||
}
|
||||
|
|
@ -320,12 +446,25 @@ impl App {
|
|||
}
|
||||
return Ok(None);
|
||||
}
|
||||
if self.who_open {
|
||||
match key.code {
|
||||
KeyCode::Esc | KeyCode::F(5) => self.who_open = false,
|
||||
KeyCode::Char('?') => {
|
||||
self.who_open = false;
|
||||
self.help_open = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
if key.code == KeyCode::Char('?') && !matches!(self.screen, Screen::Paste { .. }) {
|
||||
self.help_open = true;
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let mut wipe_confirm = None;
|
||||
let mut profile_save = None;
|
||||
let mut open_profile = false;
|
||||
match &mut self.screen {
|
||||
Screen::Main => match key.code {
|
||||
KeyCode::Tab => self.focus = self.focus.next(),
|
||||
|
|
@ -344,14 +483,15 @@ impl App {
|
|||
prompt: RotatePrompt::new(),
|
||||
};
|
||||
}
|
||||
KeyCode::F(5) => self.who_open = true,
|
||||
KeyCode::Esc => self.composer.clear(),
|
||||
KeyCode::Backspace => {
|
||||
if self.focus == Pane::Composer {
|
||||
self.composer.pop();
|
||||
}
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(kind) = parse_slash(&self.composer) {
|
||||
KeyCode::Enter => match parse_cmd(&self.composer) {
|
||||
Some(SlashCmd::Wipe(kind)) => {
|
||||
self.composer.clear();
|
||||
let prompt = match kind {
|
||||
WipeKind::Messages => WipePrompt::messages(),
|
||||
|
|
@ -359,7 +499,16 @@ impl App {
|
|||
};
|
||||
self.screen = Screen::Wipe { kind, prompt };
|
||||
}
|
||||
}
|
||||
Some(SlashCmd::Profile) => {
|
||||
self.composer.clear();
|
||||
open_profile = true;
|
||||
}
|
||||
Some(SlashCmd::Who) => {
|
||||
self.composer.clear();
|
||||
self.who_open = true;
|
||||
}
|
||||
None => {}
|
||||
},
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
self.handle_main_char(c);
|
||||
}
|
||||
|
|
@ -421,10 +570,27 @@ impl App {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
Screen::Profile { editor } => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Tab => editor.on_tab(),
|
||||
KeyCode::BackTab => editor.on_backtab(),
|
||||
KeyCode::Backspace => editor.on_backspace(),
|
||||
KeyCode::Enter => profile_save = Some(editor.on_enter()),
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
editor.on_char(c);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
if let Some(kind) = wipe_confirm {
|
||||
return self.confirm_wipe(kind);
|
||||
}
|
||||
if open_profile {
|
||||
self.open_profile()?;
|
||||
}
|
||||
if let Some(draft) = profile_save {
|
||||
self.save_profile(draft)?;
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
|
|
@ -573,6 +739,43 @@ impl App {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn open_profile(&mut self) -> Result<(), String> {
|
||||
let p = self.node.self_profile()?;
|
||||
self.screen = Screen::Profile {
|
||||
editor: ProfileEditor::new(&p.display_name, &p.bio, &p.xmr_addr),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn save_profile(&mut self, draft: ProfileDraft) -> Result<(), String> {
|
||||
if let Err(e) = self
|
||||
.node
|
||||
.set_self_profile(&draft.display_name, &draft.bio, &draft.xmr_addr)
|
||||
{
|
||||
self.alert = Some(e);
|
||||
return Ok(());
|
||||
}
|
||||
self.screen = Screen::Main;
|
||||
if let Some(friend) = self.friends.get(self.selected) {
|
||||
let pk = friend.pubkey.clone();
|
||||
match self.rt.block_on(self.node.push_self_profile(&pk)) {
|
||||
Ok(()) => self.status_note = Some("profile saved · sent".into()),
|
||||
Err(e) => self.status_note = Some(format!("profile send failed: {e}")),
|
||||
}
|
||||
} else {
|
||||
self.status_note = Some("profile saved".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn who_body(&self) -> String {
|
||||
let Some(friend) = self.friends.get(self.selected) else {
|
||||
return "no friend selected\n\nEsc closes".into();
|
||||
};
|
||||
let profile = self.node.friend_profile(&friend.pubkey).ok().flatten();
|
||||
who_overlay_text(&friend.fingerprint, &friend.onion, profile.as_ref())
|
||||
}
|
||||
|
||||
fn draw(&self, f: &mut Frame) {
|
||||
match &self.screen {
|
||||
Screen::Main => self.draw_main(f),
|
||||
|
|
@ -581,9 +784,12 @@ impl App {
|
|||
Screen::Approve { payload } => draw_approve(f, payload),
|
||||
Screen::Rotate { prompt } => draw_rotate(f, prompt.typed()),
|
||||
Screen::Wipe { kind, prompt } => draw_wipe(f, *kind, prompt.typed()),
|
||||
Screen::Profile { editor } => draw_profile(f, editor),
|
||||
}
|
||||
if self.help_open {
|
||||
draw_help_overlay(f);
|
||||
draw_text_overlay(f, "? help", help_overlay_text());
|
||||
} else if self.who_open {
|
||||
draw_text_overlay(f, "/who", &self.who_body());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -688,7 +894,7 @@ impl App {
|
|||
f.render_widget(chat, panes[1]);
|
||||
|
||||
let cmd = if self.composer.is_empty() {
|
||||
"/wipe /wipe-all".to_string()
|
||||
"/wipe /wipe-all /profile /who".to_string()
|
||||
} else {
|
||||
self.composer.clone()
|
||||
};
|
||||
|
|
@ -803,7 +1009,7 @@ fn join_footer(parts: &[&str]) -> String {
|
|||
|
||||
/// Main-screen key hints. Kept short so width 80 still shows TOR + identity + `? help`.
|
||||
pub fn main_footer_hints() -> &'static str {
|
||||
"Tab F2 F3 F4 ? help Ctrl-Q"
|
||||
"Tab F2 F3 F4 F5 ? help Ctrl-Q"
|
||||
}
|
||||
|
||||
fn footer_hints(screen: &Screen) -> &'static str {
|
||||
|
|
@ -817,6 +1023,7 @@ fn footer_hints(screen: &Screen) -> &'static str {
|
|||
WipeKind::Messages => "type WIPE Esc cancel ? help",
|
||||
WipeKind::All => "type WIPEALL Esc cancel ? help",
|
||||
},
|
||||
Screen::Profile { .. } => "Enter save Tab field Esc cancel ? help",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -898,9 +1105,27 @@ fn draw_wipe(f: &mut Frame, kind: WipeKind, typed: &str) {
|
|||
);
|
||||
}
|
||||
|
||||
fn draw_help_overlay(f: &mut Frame) {
|
||||
fn draw_profile(f: &mut Frame, editor: &ProfileEditor) {
|
||||
let labels = ["name", "bio", "xmr"];
|
||||
let values = [editor.display_name(), editor.bio(), editor.xmr_addr()];
|
||||
let mut body = String::from(
|
||||
"Your profile (friend-visible)\nEnter saves Esc cancels Tab next field\n",
|
||||
);
|
||||
for (i, (label, value)) in labels.iter().zip(values).enumerate() {
|
||||
let mark = if editor.field() == i { ">" } else { " " };
|
||||
body.push_str(&format!("\n{mark} {label}: {value}"));
|
||||
}
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.style(Style::default().fg(C_TEXT))
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(themed_block("(o) profile")),
|
||||
f.area(),
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) {
|
||||
let area = f.area();
|
||||
let text = help_overlay_text();
|
||||
let lines = text.lines().count() as u16 + 2;
|
||||
let text_w = text
|
||||
.lines()
|
||||
|
|
@ -920,7 +1145,7 @@ fn draw_help_overlay(f: &mut Frame) {
|
|||
f.render_widget(
|
||||
Paragraph::new(text)
|
||||
.style(Style::default().fg(C_TEXT))
|
||||
.block(themed_block("? help")),
|
||||
.block(themed_block(title)),
|
||||
popup,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue