Giter Site home page Giter Site logo

init --theme about mdbook HOT 3 CLOSED

rust-lang avatar rust-lang commented on July 29, 2024
init --theme

from mdbook.

Comments (3)

azerupi avatar azerupi commented on July 29, 2024

@kbknapp sorry to bother you.

For this feature I would like warn users and ask them if they want to continue. Something like this:

Copying the theme to 'src/theme' could overwrite files already present. 
Are you sure you want to continue? (y/n)

Is that functionality in clap, or do you know an easy way of doing this? :)

from mdbook.

kbknapp avatar kbknapp commented on July 29, 2024

No worries πŸ˜„

There isn't anything like a confirm() in clap (although that's not a bad idea!). I've found it's usually also a good idea to give the user a chance to skip the confirmation if they know what they're doing (by adding --noconfirm or --force, etc.)

The best way to do this is:

use std::io::{self, Write, Read};

fn main() {
    let m = // everything normal 
    // just add an arg that uses --theme
    // and one for skipping such as --noconfirm
    .get_matches();

    if m.is_present("theme") && !m.is_present("noconfirm")  {
        if !confirm() {
            println!("exiting...");
            ::std::process::exit(0);
        }
    }
}

fn confirm() -> bool {
    print!("Copying the theme to 'src/theme' could overwrite files already present.\n\n\
            Are you sure you want to continue? (y/n): ");
    io::stdout().flush().unwrap();
    let mut s = String::new();
    io::stdin().read_line(&mut s).ok();
    match &*s.trim() {
        "Y" | "y" => true,
        _         => false
    }
}

Edit: Fixed formatting

Edit 2: Also, I've vote πŸ‘ on doing all the init when a user does init --theme. I'd find it confusing to run

$ mdbook init --theme sometheme
$ mdbook init

from mdbook.

azerupi avatar azerupi commented on July 29, 2024

Nice! Thank you for that :)

There isn't anything like a confirm() in clap (although that's not a bad idea!).

That would be a neat thing to have ;)

Also, I've vote πŸ‘ on doing all the init when a user does init --theme. I'd find it confusing to run...

I agree, the only reason I was hesitating is that you may want to copy the theme in an already existing project. And at that moment you don't want init to mess up what you already have.

But I it's a better idea to implement init so that it does not mess up an existing project when runned.

from mdbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.