Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit ebda937

Browse files
author
Hendrik van Antwerpen
committed
Provide list of common licenses
1 parent 9eaf8ca commit ebda937

File tree

1 file changed

+42
-6
lines changed
  • tree-sitter-stack-graphs/src/cli

1 file changed

+42
-6
lines changed

tree-sitter-stack-graphs/src/cli/init.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use anyhow::anyhow;
99
use clap::Args;
1010
use clap::ValueHint;
11+
use dialoguer::Input;
1112
use dialoguer::Select;
12-
use dialoguer::{Input, Validator};
13+
use dialoguer::Validator;
1314
use indoc::printdoc;
1415
use indoc::writedoc;
1516
use lazy_static::lazy_static;
@@ -22,6 +23,8 @@ use std::path::PathBuf;
2223

2324
const TSSG_VERSION: &str = env!("CARGO_PKG_VERSION");
2425

26+
const DEFAULT_LICENSES: &[&str] = &["APACHE-2.0", "BSD-2-Clause", "BSD-3-Clause", "ISC", "MIT"];
27+
2528
lazy_static! {
2629
static ref VALID_CRATE_NAME: Regex = Regex::new(r"^[a-zA-Z_-][a-zA-Z0-9_-]*$").unwrap();
2730
static ref VALID_CRATE_VERSION: Regex = Regex::new(r"^[0-9]+\.[0-9]+\.[0-9]+$").unwrap();
@@ -202,14 +205,47 @@ impl ProjectSettings {
202205

203206
printdoc! {r#"
204207
205-
Give the project license as an SPDX expression. Leave empty to omit.
208+
Give the project license as an SPDX expression. Choose "Other" to input
209+
manually. Press ESC to deselect. See https://spdx.org/licenses/ for possible
210+
license identifiers.
206211
"#
207212
};
208-
self.license = Input::new()
213+
let other_index = DEFAULT_LICENSES.len();
214+
let none_index = other_index + 1;
215+
let selected = DEFAULT_LICENSES
216+
.iter()
217+
.position(|l| l == &self.license)
218+
.unwrap_or_else(|| {
219+
if self.license.is_empty() {
220+
none_index
221+
} else {
222+
other_index
223+
}
224+
});
225+
let (other, other_default) = if selected == other_index {
226+
(format!("Other ({})", self.license), self.license.as_ref())
227+
} else {
228+
("Other".to_string(), "")
229+
};
230+
let mut selector = Select::new();
231+
let selected = selector
209232
.with_prompt("License")
210-
.with_initial_text(&self.license)
211-
.allow_empty(true)
212-
.interact_text()?;
233+
.items(DEFAULT_LICENSES)
234+
.item(&other)
235+
.item("None")
236+
.default(selected)
237+
.interact()?;
238+
self.license = if selected == none_index {
239+
"".to_string()
240+
} else if selected == other_index {
241+
Input::new()
242+
.with_prompt("Other license")
243+
.with_initial_text(other_default)
244+
.allow_empty(true)
245+
.interact_text()?
246+
} else {
247+
DEFAULT_LICENSES[selected].to_string()
248+
};
213249

214250
printdoc! {r#"
215251

0 commit comments

Comments
 (0)