four-word-networking 2.7.0

Convert IP addresses to memorable, family-friendly word groups. Interactive TUI with real-time autocomplete. IPv4 = 4 words, IPv6 = 6-12 words. Perfect reconstruction with progressive hints.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! 4wn - Four-Word Networking CLI with Improved Interactive Mode
//!
//! Much better UX with real-time conversion and intelligent input detection

use clap::Parser;
use four_word_networking::{FourWordAdaptiveEncoder, FourWordError, Result};
use std::io::{self, Write};
use std::process;

// TUI imports for real-time interactive mode
use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyEventKind},
    execute, queue,
    style::{Color, Print, ResetColor, SetForegroundColor},
    terminal::{self, ClearType},
};

#[derive(Parser)]
#[command(
    name = "4wn",
    about = "Four-Word Networking - Smart IP/word converter",
    long_about = "Smart interactive CLI that auto-detects whether you're typing an IP address or words.\n\
                  Just start typing - it figures out what you mean and shows live conversion.",
    version
)]
struct Cli {
    /// Input to convert (IP:port or words) - if provided, performs direct conversion
    /// If no input provided, starts interactive mode
    input: Vec<String>,

    /// Show detailed information
    #[arg(short, long)]
    verbose: bool,

    /// Output format for scripting (minimal output)
    #[arg(short, long)]
    quiet: bool,
}

#[derive(Debug, Clone, PartialEq)]
enum InputType {
    IpAddress,
    Words,
    Unknown,
    Command,
}

fn main() {
    let cli = Cli::parse();

    if let Err(e) = run(cli) {
        eprintln!("Error: {e}");
        process::exit(1);
    }
}

fn run(cli: Cli) -> Result<()> {
    let encoder = FourWordAdaptiveEncoder::new()?;

    // If no input provided, start interactive mode
    if cli.input.is_empty() {
        return interactive_mode(&encoder, cli.verbose);
    }

    // Direct conversion mode with arguments
    let input = if cli.input.len() == 1 {
        cli.input[0].trim().to_string()
    } else {
        // Multiple arguments - treat as space-separated words
        cli.input.join(" ")
    };

    // Detect and process
    if looks_like_ip(&input) {
        encode_address(&encoder, &input, cli.verbose, cli.quiet)
    } else {
        decode_words(&encoder, &input, cli.verbose, cli.quiet)
    }
}

/// Improved IP detection
fn looks_like_ip(input: &str) -> bool {
    // Check for IP patterns
    input.contains(':')
        || input.contains('[')
        || input.parse::<std::net::IpAddr>().is_ok()
        || input.chars().filter(|c| *c == '.').count() == 3 && input.chars().any(|c| c.is_numeric())
}

/// Detect input type intelligently
fn detect_input_type(input: &str) -> InputType {
    let trimmed = input.trim();

    // Check for commands first
    if trimmed == "quit" || trimmed == "exit" || trimmed == "help" || trimmed == "clear" {
        return InputType::Command;
    }

    // Check if it's starting to look like an IP
    if input.contains(':') || input.contains('[') {
        return InputType::IpAddress;
    }

    // Check for IPv4 pattern (numbers and dots)
    let has_numbers = input.chars().any(|c| c.is_numeric());
    let dot_count = input.chars().filter(|c| *c == '.').count();

    if has_numbers && (dot_count > 0 || input.parse::<u8>().is_ok()) {
        return InputType::IpAddress;
    }

    // Check if it's words (alphabetic with spaces or dots)
    let mostly_alpha = input
        .chars()
        .filter(|c| c.is_alphabetic() || c.is_whitespace() || *c == '.' || *c == '-')
        .count()
        == input.len();

    if mostly_alpha && !input.is_empty() {
        return InputType::Words;
    }

    InputType::Unknown
}

/// Encode IP address to words
fn encode_address(
    encoder: &FourWordAdaptiveEncoder,
    address: &str,
    verbose: bool,
    quiet: bool,
) -> Result<()> {
    let words = encoder.encode(address)?;

    if quiet {
        println!("{words}");
    } else if verbose {
        println!("Input: {address}");
        println!("Words: {words}");
        println!(
            "Type: {}",
            if words.contains('-') { "IPv6" } else { "IPv4" }
        );
    } else {
        println!("{words}");
    }

    Ok(())
}

/// Decode words to IP address
fn decode_words(
    encoder: &FourWordAdaptiveEncoder,
    words: &str,
    verbose: bool,
    quiet: bool,
) -> Result<()> {
    let address = encoder.decode(words)?;

    if quiet {
        println!("{address}");
    } else if verbose {
        println!("Input: {words}");
        println!("Address: {address}");
    } else {
        println!("{address}");
    }

    Ok(())
}

/// Improved interactive mode
fn interactive_mode(encoder: &FourWordAdaptiveEncoder, verbose: bool) -> Result<()> {
    // Enable raw mode for character-by-character input
    terminal::enable_raw_mode()
        .map_err(|e| FourWordError::InvalidInput(format!("Failed to enable raw mode: {e}")))?;

    // Clean up terminal on exit
    let cleanup = || {
        let _ = terminal::disable_raw_mode();
        let _ = execute!(io::stdout(), cursor::Show, ResetColor);
    };

    // Set up panic hook to cleanup terminal
    let original_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        let _ = terminal::disable_raw_mode();
        let _ = execute!(io::stdout(), cursor::Show, ResetColor);
        original_hook(info);
    }));

    let result = run_interactive_tui(encoder, verbose);

    // Always cleanup
    cleanup();

    result
}

/// Run the improved TUI
fn run_interactive_tui(encoder: &FourWordAdaptiveEncoder, _verbose: bool) -> Result<()> {
    let mut stdout = io::stdout();

    // Clear screen and show better header
    execute!(
        stdout,
        terminal::Clear(ClearType::All),
        cursor::MoveTo(0, 0),
        SetForegroundColor(Color::Cyan),
        Print("🌐 Four-Word Networking\n"),
        ResetColor,
        Print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n"),
        Print("Just start typing! I'll figure out if it's an IP or words.\n"),
        Print("• IP addresses → instant word conversion\n"),
        Print("• Words → instant IP reconstruction\n"),
        Print("• Tab completes partial words\n"),
        Print("• Type 'help' for more commands\n\n"),
    )
    .map_err(|e| FourWordError::InvalidInput(format!("Terminal error: {e}")))?;

    let mut current_input = String::new();
    let mut cursor_pos = 0;
    let mut _last_result = String::new();

    loop {
        // Detect what the user is typing
        let input_type = detect_input_type(&current_input);

        // Try live conversion
        let live_result = try_live_conversion(encoder, &current_input, input_type.clone());

        // Render the UI
        render_smart_ui(
            &mut stdout,
            &current_input,
            cursor_pos,
            &input_type,
            &live_result,
            encoder,
        )?;

        // Read next event
        if let Ok(event) = event::read() {
            match event {
                Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
                    match key_event.code {
                        KeyCode::Char('c')
                            if key_event.modifiers.contains(event::KeyModifiers::CONTROL) =>
                        {
                            break;
                        }
                        KeyCode::Enter => {
                            // Process the input
                            let result = process_input(encoder, &current_input);
                            match result {
                                Ok(Some(output)) => {
                                    _last_result = output;
                                    if current_input.trim() == "quit"
                                        || current_input.trim() == "exit"
                                    {
                                        break;
                                    }
                                }
                                Ok(None) => {
                                    // Command handled
                                }
                                Err(e) => {
                                    _last_result = format!("Error: {e}");
                                }
                            }
                            current_input.clear();
                            cursor_pos = 0;
                        }
                        KeyCode::Tab => {
                            // Smart tab completion
                            if let Some(completed) =
                                smart_complete(encoder, &current_input, cursor_pos)
                            {
                                current_input = completed;
                                cursor_pos = current_input.len();
                            }
                        }
                        KeyCode::Backspace => {
                            if cursor_pos > 0 {
                                current_input.remove(cursor_pos - 1);
                                cursor_pos -= 1;
                            }
                        }
                        KeyCode::Delete => {
                            if cursor_pos < current_input.len() {
                                current_input.remove(cursor_pos);
                            }
                        }
                        KeyCode::Left => {
                            cursor_pos = cursor_pos.saturating_sub(1);
                        }
                        KeyCode::Right => {
                            if cursor_pos < current_input.len() {
                                cursor_pos += 1;
                            }
                        }
                        KeyCode::Home => {
                            cursor_pos = 0;
                        }
                        KeyCode::End => {
                            cursor_pos = current_input.len();
                        }
                        KeyCode::Char(c) => {
                            current_input.insert(cursor_pos, c);
                            cursor_pos += 1;
                        }
                        _ => {}
                    }
                }
                _ => {}
            }
        }
    }

    // Clear and say goodbye
    execute!(
        stdout,
        terminal::Clear(ClearType::All),
        cursor::MoveTo(0, 0),
        Print("Thanks for using 4wn! 👋\n")
    )
    .map_err(|e| FourWordError::InvalidInput(format!("Terminal error: {e}")))?;

    Ok(())
}

/// Smart UI rendering
fn render_smart_ui(
    stdout: &mut io::Stdout,
    input: &str,
    cursor_pos: usize,
    input_type: &InputType,
    live_result: &Option<String>,
    encoder: &FourWordAdaptiveEncoder,
) -> Result<()> {
    // Move to input line (line 10)
    queue!(stdout, cursor::MoveTo(0, 9))?;
    queue!(stdout, terminal::Clear(ClearType::FromCursorDown))?;

    // Show input type detection
    match input_type {
        InputType::IpAddress => {
            queue!(
                stdout,
                SetForegroundColor(Color::Blue),
                Print("📍 IP Address Mode\n"),
                ResetColor
            )?;
        }
        InputType::Words => {
            queue!(
                stdout,
                SetForegroundColor(Color::Green),
                Print("📝 Word Mode\n"),
                ResetColor
            )?;
        }
        InputType::Command => {
            queue!(
                stdout,
                SetForegroundColor(Color::Yellow),
                Print("⚡ Command\n"),
                ResetColor
            )?;
        }
        InputType::Unknown => {
            queue!(
                stdout,
                SetForegroundColor(Color::Grey),
                Print("💭 Type an IP or words...\n"),
                ResetColor
            )?;
        }
    }

    // Show the input prompt
    queue!(stdout, Print("\n> "), Print(input))?;

    // Show live conversion result
    if let Some(result) = live_result {
        queue!(
            stdout,
            Print("\n\n"),
            SetForegroundColor(Color::Cyan),
            Print(""),
            ResetColor,
            Print(result)
        )?;
    }

    // Show hints for words mode
    if matches!(input_type, InputType::Words) && !input.is_empty() {
        // Get the last partial word
        let words: Vec<&str> = input.split_whitespace().collect();
        if let Some(last_word) = words.last().filter(|w| !w.is_empty()) {
            let hints = encoder.get_word_hints(last_word);
            if !hints.is_empty() && hints.len() <= 10 {
                queue!(stdout, Print("\n\nHints: "))?;
                for (i, hint) in hints.iter().take(5).enumerate() {
                    if i > 0 {
                        queue!(stdout, Print(", "))?;
                    }
                    queue!(
                        stdout,
                        SetForegroundColor(Color::DarkGrey),
                        Print(hint),
                        ResetColor
                    )?;
                }
            }
        }
    }

    // Position cursor correctly
    let cursor_col = 2 + cursor_pos; // "> " = 2 chars
    queue!(stdout, cursor::MoveTo(cursor_col as u16, 11))?;

    stdout
        .flush()
        .map_err(|e| FourWordError::InvalidInput(format!("Flush error: {e}")))?;
    Ok(())
}

/// Try live conversion as user types
fn try_live_conversion(
    encoder: &FourWordAdaptiveEncoder,
    input: &str,
    input_type: InputType,
) -> Option<String> {
    if input.trim().is_empty() {
        return None;
    }

    match input_type {
        InputType::IpAddress => {
            // Try to encode partial or complete IP
            encoder.encode(input).ok()
        }
        InputType::Words => {
            // Try to decode if we have complete words
            let words: Vec<&str> = input.split_whitespace().collect();
            if words.len() == 4 || words.len() == 6 || words.len() == 9 || words.len() == 12 {
                encoder.decode(input).ok()
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Smart completion
fn smart_complete(
    encoder: &FourWordAdaptiveEncoder,
    input: &str,
    _cursor_pos: usize,
) -> Option<String> {
    // Find the word at cursor position
    let words: Vec<&str> = input.split_whitespace().collect();

    if words.is_empty() {
        return None;
    }

    // Get the last word (partial)
    if let Some(last_word) = words.last().filter(|w| !w.is_empty()) {
        let hints = encoder.get_word_hints(last_word);
        if hints.len() == 1 {
            // Complete with the single match
            let mut result = words[..words.len() - 1].join(" ");
            if !result.is_empty() {
                result.push(' ');
            }
            result.push_str(&hints[0]);
            return Some(result);
        } else if !hints.is_empty() {
            // Use the shortest match
            if let Some(shortest) = hints.iter().min_by_key(|s| s.len()) {
                let mut result = words[..words.len() - 1].join(" ");
                if !result.is_empty() {
                    result.push(' ');
                }
                result.push_str(shortest);
                return Some(result);
            }
        }
    }

    None
}

/// Process the completed input
fn process_input(encoder: &FourWordAdaptiveEncoder, input: &str) -> Result<Option<String>> {
    let trimmed = input.trim();

    // Handle commands
    match trimmed {
        "quit" | "exit" => return Ok(Some("quit".to_string())),
        "help" => {
            return Ok(Some(
                "Commands:\n\
                • Type any IP address to see its words\n\
                • Type words to see the IP address\n\
                • Tab - complete partial word\n\
                • clear - clear the screen\n\
                • quit/exit - leave the program"
                    .to_string(),
            ));
        }
        "clear" => {
            let _ = execute!(io::stdout(), terminal::Clear(ClearType::All));
            return Ok(None);
        }
        "" => return Ok(None),
        _ => {}
    }

    // Try as IP first
    if let Ok(encoded) = encoder.encode(trimmed) {
        return Ok(Some(format!("{trimmed}{encoded}")));
    }

    // Try as words
    if let Ok(decoded) = encoder.decode(trimmed) {
        return Ok(Some(format!("{trimmed}{decoded}")));
    }

    // Check if it's partial words that need completion
    let words: Vec<&str> = trimmed.split_whitespace().collect();
    if !words.is_empty() {
        // Try to validate each word
        for word in &words {
            if encoder.get_word_hints(word).is_empty()
                && !encoder.get_word_hints(word).contains(&word.to_string())
            {
                return Err(FourWordError::InvalidInput(format!(
                    "'{word}' is not a valid word"
                )));
            }
        }
    }

    Err(FourWordError::InvalidInput("Invalid input".to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_input_type_detection() {
        assert_eq!(detect_input_type("192.168"), InputType::IpAddress);
        assert_eq!(detect_input_type("ocean blue"), InputType::Words);
        assert_eq!(detect_input_type("quit"), InputType::Command);
        assert_eq!(detect_input_type(""), InputType::Unknown);
        assert_eq!(detect_input_type("127.0"), InputType::IpAddress);
        assert_eq!(detect_input_type("::1"), InputType::IpAddress);
        assert_eq!(detect_input_type("a"), InputType::Words);
    }
}