on master help sign in
enable starting hx with a working directory (#8223)

* added working path arg to cli and help menu * improve working path cli arg handling * enable hx to set the working path * applied cargo formatting * improved code from cargo clippy suggestion * improved code from follow up review * fix for -w <path> is set but args.files is empty * improved formatting of --help output

commit75c0a5ceb32d8a503915a93ccc1b64c8ad1cba8b authorLloyd Bond <lbond@sailthru.com> dateMon 2 Oct 2023, 21:18 (3 years ago) parent1756ba4
3 files changed+19
helix-term/src/application.rs+3 helix-term/src/args.rs+15 helix-term/src/main.rs+1
helix-term/src/application.rs+3
@@ -156,6 +156,9 @@ impl Application {
156156 let editor_view = Box::new(ui::EditorView::new(Keymaps::new(keys)));
157157 compositor.push(editor_view);
158158
159+ if let Some(path) = args.working_directory {
160+ helix_loader::set_current_working_dir(path)?
161+ }
159162 if args.load_tutor {
160163 let path = helix_loader::runtime_file(Path::new("tutor"));
161164 editor.open(&path, Action::VerticalSplit)?;
helix-term/src/args.rs+15
@@ -17,6 +17,7 @@ pub struct Args {
1717 pub log_file: Option<PathBuf>,
1818 pub config_file: Option<PathBuf>,
1919 pub files: Vec<(PathBuf, Position)>,
20+ pub working_directory: Option<PathBuf>,
2021}
2122
2223impl Args {
@@ -59,6 +60,20 @@ impl Args {
5960 Some(path) => args.log_file = Some(path.into()),
6061 None => anyhow::bail!("--log must specify a path to write"),
6162 },
63+ "-w" | "--working-dir" => match argv.next().as_deref() {
64+ Some(path) => {
65+ args.working_directory = if Path::new(path).is_dir() {
66+ Some(PathBuf::from(path))
67+ } else {
68+ anyhow::bail!(
69+ "--working-dir specified does not exist or is not a directory"
70+ )
71+ }
72+ }
73+ None => {
74+ anyhow::bail!("--working-dir must specify an initial working directory")
75+ }
76+ },
6277 arg if arg.starts_with("--") => {
6378 anyhow::bail!("unexpected double dash argument: {}", arg)
6479 }
helix-term/src/main.rs+1
@@ -66,6 +66,7 @@ FLAGS:
6666 -V, --version Prints version information
6767 --vsplit Splits all given files vertically into different windows
6868 --hsplit Splits all given files horizontally into different windows
69+ -w, --working-dir <path> Specify an initial working directory
6970",
7071 env!("CARGO_PKG_NAME"),
7172 VERSION_AND_GIT_HASH,