diff options
author | Omnikar | 2021-10-28 01:23:46 +0000 |
---|---|---|
committer | GitHub | 2021-10-28 01:23:46 +0000 |
commit | e2ed6915373adf27881325ebc4e2c6b98e207af3 (patch) | |
tree | 85c89248d6ff0cc76f52f7edfed9495fb8cd2da9 /helix-term | |
parent | 3b0c5e993a18a2d59582855784189995c7960d6f (diff) |
Implement `hx --tutor` and `:tutor` to load `tutor.txt` (#898)
* Implement `hx --tutor` and `:tutor` to load `tutor.txt`
* Document `hx --tutor` and `:tutor`
* Change `Document::set_path` to take an `Option`
* `Document::set_path` accepts an `Option<&Path>` instead of `&Path`.
* Remove `Editor::open_tutor` and make tutor-open functionality use
`Editor::open` and `Document::set_path`.
* Use `PathBuf::join`
Co-authored-by: Ivan Tham <pickfire@riseup.net>
* Add comments explaining unsetting tutor path
Co-authored-by: Ivan Tham <pickfire@riseup.net>
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/application.rs | 7 | ||||
-rw-r--r-- | helix-term/src/args.rs | 2 | ||||
-rw-r--r-- | helix-term/src/commands.rs | 24 |
3 files changed, 30 insertions, 3 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 662573c6..55b12c5a 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -97,7 +97,12 @@ impl Application { let editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys))); compositor.push(editor_view); - if !args.files.is_empty() { + if args.load_tutor { + let path = helix_core::runtime_dir().join("tutor.txt"); + editor.open(path, Action::VerticalSplit)?; + // Unset path to prevent accidentally saving to the original tutor file. + doc_mut!(editor).set_path(None)?; + } else if !args.files.is_empty() { let first = &args.files[0]; // we know it's not empty if first.is_dir() { std::env::set_current_dir(&first)?; diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index f0ef09eb..40113db9 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; pub struct Args { pub display_help: bool, pub display_version: bool, + pub load_tutor: bool, pub verbosity: u64, pub files: Vec<PathBuf>, } @@ -22,6 +23,7 @@ impl Args { "--" => break, // stop parsing at this point treat the remaining as files "--version" => args.display_version = true, "--help" => args.display_help = true, + "--tutor" => args.load_tutor = true, arg if arg.starts_with("--") => { return Err(Error::msg(format!( "unexpected double dash argument: {}", diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d6e5bfe7..b3be6d5b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1557,7 +1557,8 @@ mod cmd { let (_, doc) = current!(cx.editor); if let Some(path) = path { - doc.set_path(path.as_ref()).context("invalid filepath")?; + doc.set_path(Some(path.as_ref())) + .context("invalid filepath")?; } if doc.path().is_none() { bail!("cannot write a buffer without a filename"); @@ -2099,6 +2100,18 @@ mod cmd { Ok(()) } + fn tutor( + cx: &mut compositor::Context, + _args: &[&str], + _event: PromptEvent, + ) -> anyhow::Result<()> { + let path = helix_core::runtime_dir().join("tutor.txt"); + cx.editor.open(path, Action::Replace)?; + // Unset path to prevent accidentally saving to the original tutor file. + doc_mut!(cx.editor).set_path(None)?; + Ok(()) + } + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -2351,7 +2364,14 @@ mod cmd { doc: "Open the file in a horizontal split.", fun: hsplit, completer: Some(completers::filename), - } + }, + TypableCommand { + name: "tutor", + aliases: &[], + doc: "Open the tutorial.", + fun: tutor, + completer: None, + }, ]; pub static COMMANDS: Lazy<HashMap<&'static str, &'static TypableCommand>> = Lazy::new(|| { |