taskfast_cli/cmd/
init_tui.rs1use std::io::{self, IsTerminal};
14
15use dialoguer::theme::ColorfulTheme;
16use dialoguer::{Input, Password, Select};
17use taskfast_client::UserProfile;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum WalletMode {
24 Byow,
26 Generate,
28 Skip,
30}
31
32pub trait Prompter {
36 fn pat(&self, accounts_url: &str) -> io::Result<String>;
39 fn wallet_mode(&self) -> io::Result<WalletMode>;
41 fn wallet_address(&self) -> io::Result<String>;
43 fn wallet_password(&self) -> io::Result<String>;
45}
46
47pub struct DialoguerPrompter;
51
52impl Prompter for DialoguerPrompter {
53 fn pat(&self, accounts_url: &str) -> io::Result<String> {
54 eprintln!("Get a PAT at: {accounts_url}");
57 Password::with_theme(&ColorfulTheme::default())
58 .with_prompt("Paste your PAT")
59 .interact()
60 .map_err(dialoguer_err)
61 }
62
63 fn wallet_mode(&self) -> io::Result<WalletMode> {
64 let choices = [
65 "Use existing wallet (BYOW)",
66 "Generate new wallet",
67 "Skip for now",
68 ];
69 let idx = Select::with_theme(&ColorfulTheme::default())
70 .with_prompt("Wallet setup")
71 .items(&choices)
72 .default(0)
73 .interact()
74 .map_err(dialoguer_err)?;
75 Ok(match idx {
76 0 => WalletMode::Byow,
77 1 => WalletMode::Generate,
78 _ => WalletMode::Skip,
79 })
80 }
81
82 fn wallet_address(&self) -> io::Result<String> {
83 Input::<String>::with_theme(&ColorfulTheme::default())
84 .with_prompt("Wallet address")
85 .validate_with(|s: &String| validate_wallet_address(s))
86 .interact_text()
87 .map_err(dialoguer_err)
88 }
89
90 fn wallet_password(&self) -> io::Result<String> {
91 Password::with_theme(&ColorfulTheme::default())
92 .with_prompt("Keystore password")
93 .with_confirmation("Confirm password", "Passwords don't match")
94 .interact()
95 .map_err(dialoguer_err)
96 }
97}
98
99fn dialoguer_err(e: dialoguer::Error) -> io::Error {
100 match e {
101 dialoguer::Error::IO(io) => io,
102 }
103}
104
105fn validate_wallet_address(s: &str) -> Result<(), &'static str> {
108 let trimmed = s.trim();
109 let rest = trimmed
110 .strip_prefix("0x")
111 .ok_or("address must start with 0x")?;
112 if rest.len() != 40 {
113 return Err("address must be 0x + 40 hex characters");
114 }
115 if !rest.bytes().all(|b| b.is_ascii_hexdigit()) {
116 return Err("address must be hexadecimal");
117 }
118 Ok(())
119}
120
121pub fn is_interactive() -> bool {
125 io::stdin().is_terminal() && io::stdout().is_terminal()
126}
127
128pub fn greeting(profile: Option<&UserProfile>) -> String {
132 match profile {
133 Some(p) => format!("Hi {} <{}>", p.name, p.email),
134 None => "Hi there — authenticated.".to_string(),
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 #[test]
143 fn greeting_with_profile() {
144 let p = UserProfile {
145 name: "Alice Smith".into(),
146 email: "alice@example.com".into(),
147 };
148 assert_eq!(greeting(Some(&p)), "Hi Alice Smith <alice@example.com>");
149 }
150
151 #[test]
152 fn greeting_without_profile_is_neutral() {
153 assert_eq!(greeting(None), "Hi there — authenticated.");
154 }
155
156 #[test]
157 fn valid_wallet_address_accepted() {
158 assert!(validate_wallet_address("0x0123456789abcdef0123456789ABCDEF01234567").is_ok());
159 }
160
161 #[test]
162 fn wallet_address_missing_0x_rejected() {
163 assert!(validate_wallet_address("0123456789abcdef0123456789ABCDEF01234567").is_err());
164 }
165
166 #[test]
167 fn wallet_address_wrong_length_rejected() {
168 assert!(validate_wallet_address("0xabc").is_err());
169 }
170
171 #[test]
172 fn wallet_address_non_hex_rejected() {
173 assert!(validate_wallet_address("0xGGGG456789abcdef0123456789ABCDEF01234567").is_err());
174 }
175
176 #[test]
177 fn wallet_address_trims_whitespace() {
178 assert!(validate_wallet_address(" 0x0123456789abcdef0123456789ABCDEF01234567\n").is_ok());
179 }
180}