nanorust 0.1.0

A terminal-based text editor written in Rust, inspired by GNU nano
Documentation
//! Configuration module for the nano rust editor.
//!
//! This module handles command-line argument parsing and editor configuration.
//! It uses the `clap` crate for parsing command-line arguments.

use clap::Parser;

/// Nano-like text editor written in Rust
#[derive(Parser, Debug)]
#[command(name = "nanorust")]
#[command(author, version, about = "A nano-like text editor written in Rust", long_about = None)]
struct Cli {
    /// Enable mouse support
    #[arg(short = 'm', long = "mouse")]
    mouse: bool,

    /// Enable auto-indent
    #[arg(short = 'i', long = "autoindent")]
    autoindent: bool,

    /// Display line numbers
    #[arg(short = 'l', long = "linenumbers")]
    linenumbers: bool,

    /// Constantly show cursor position
    #[arg(short = 'c', long = "constantshow")]
    constantshow: bool,

    /// Enable soft wrapping
    #[arg(short = '$', long = "softwrap")]
    softwrap: bool,

    /// Disable line wrapping
    #[arg(short = 'w', long = "nowrap")]
    nowrap: bool,

    /// Open in view-only mode
    #[arg(short = 'v', long = "view")]
    view: bool,

    /// File to open (optional)
    #[arg(value_name = "FILE")]
    filename: Option<String>,
}

/// Configuration struct for editor settings.
///
/// This struct holds all configuration options for the editor, including
/// display settings, editing behavior, and feature flags.
///
/// # Examples
///
/// ```
/// use nanorust::Config;
///
/// // Create default configuration
/// let config = Config::default();
///
/// // Parse from command-line arguments
/// let (config, filename) = Config::from_args();
/// ```
#[derive(Debug, Clone)]
pub struct Config {
    /// Whether to enable auto-indent.
    ///
    /// When enabled, new lines will maintain the indentation level of the previous line.
    pub auto_indent: bool,
    
    /// Whether to display line numbers.
    ///
    /// When enabled, line numbers are shown in the left margin.
    pub show_line_numbers: bool,
    
    /// Whether to enable mouse support.
    ///
    /// When enabled, mouse clicks can be used to position the cursor.
    pub mouse_support: bool,
    
    /// Whether to enable soft wrapping.
    ///
    /// When enabled, long lines are wrapped at word boundaries.
    pub soft_wrap: bool,
    
    /// Whether to disable line wrapping.
    ///
    /// When enabled, long lines extend beyond the screen width.
    pub no_wrap: bool,
    
    /// Number of spaces per tab.
    ///
    /// This controls how tab characters are displayed and how many spaces
    /// are inserted when using the indent feature.
    pub tab_size: usize,
    
    /// Whether to enable syntax highlighting.
    ///
    /// When enabled, code is colorized based on the file type.
    pub syntax_highlighting: bool,
    
    /// Whether to open in view-only mode.
    ///
    /// When enabled, the buffer cannot be modified (read-only mode).
    pub view_only: bool,
    
    /// Whether to constantly show cursor position.
    ///
    /// When enabled, the cursor position is always displayed in the status bar.
    pub show_cursor_position: bool,
    
    /// Line width for paragraph justification.
    ///
    /// This controls the maximum line width when using the justify feature (Ctrl+J).
    pub line_width: usize,
    
    /// Whether to use spaces instead of tabs.
    ///
    /// When enabled, the indent feature inserts spaces instead of tab characters.
    pub use_spaces: bool,
}

impl Config {
    /// Create a new Config with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Create default configuration
    pub fn default() -> Self {
        Config {
            auto_indent: false,
            show_line_numbers: false,
            mouse_support: false,
            soft_wrap: false,
            no_wrap: false,
            tab_size: 4,
            syntax_highlighting: true,
            view_only: false,
            show_cursor_position: false,
            line_width: 80,
            use_spaces: false,
        }
    }

    /// Parse command-line arguments and create Config
    pub fn from_args() -> (Self, Option<String>) {
        let cli = Cli::parse();
        
        let config = Config {
            auto_indent: cli.autoindent,
            show_line_numbers: cli.linenumbers,
            mouse_support: cli.mouse,
            soft_wrap: cli.softwrap,
            no_wrap: cli.nowrap,
            tab_size: 4,
            syntax_highlighting: true,
            view_only: cli.view,
            show_cursor_position: cli.constantshow,
            line_width: 80,
            use_spaces: false,
        };
        
        (config, cli.filename)
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::default()
    }
}