ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: extract a function for creating a default Ui

This will also be reused by binaries that want to add custom commands.
This commit is contained in:
Martin von Zweigbergk 2022-09-22 11:13:46 -07:00 committed by Martin von Zweigbergk
parent a0dfd4a4a8
commit 7a9f9a14db
2 changed files with 22 additions and 24 deletions

View file

@ -42,6 +42,7 @@ use jujutsu_lib::working_copy::{
use jujutsu_lib::workspace::{Workspace, WorkspaceInitError, WorkspaceLoadError};
use jujutsu_lib::{dag_walk, git, revset};
use crate::config::read_config;
use crate::diff_edit::DiffEditError;
use crate::ui;
use crate::ui::{ColorChoice, FilePathParseError, Ui};
@ -1128,6 +1129,20 @@ pub struct GlobalArgs {
pub color: Option<ColorChoice>,
}
pub fn create_ui() -> Ui<'static> {
// TODO: We need to do some argument parsing here, at least for things like
// --config, and for reading user configs from the repo pointed to by
// -R.
match read_config() {
Ok(user_settings) => Ui::for_terminal(user_settings),
Err(err) => {
let mut ui = Ui::for_terminal(UserSettings::default());
ui.write_error(&format!("Config error: {}\n", err)).unwrap();
std::process::exit(1);
}
}
}
fn string_list_from_config(value: config::Value) -> Option<Vec<String>> {
match value {
config::Value {

View file

@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use jujutsu::cli_util::{parse_args, report_command_error, CommandError};
use jujutsu::cli_util::{create_ui, parse_args, report_command_error, CommandError};
use jujutsu::commands::{default_app, run_command};
use jujutsu::config::read_config;
use jujutsu::ui::Ui;
use jujutsu_lib::settings::UserSettings;
fn run(ui: &mut Ui) -> Result<(), CommandError> {
let app = default_app();
@ -25,25 +23,10 @@ fn run(ui: &mut Ui) -> Result<(), CommandError> {
}
fn main() {
// TODO: We need to do some argument parsing here, at least for things like
// --config, and for reading user configs from the repo pointed to by
// -R.
match read_config() {
Ok(user_settings) => {
let mut ui = Ui::for_terminal(user_settings);
match run(&mut ui) {
Ok(()) => {
std::process::exit(0);
}
Err(err) => {
std::process::exit(report_command_error(&mut ui, err));
}
}
}
Err(err) => {
let mut ui = Ui::for_terminal(UserSettings::default());
ui.write_error(&format!("Config error: {}\n", err)).unwrap();
std::process::exit(1);
}
}
let mut ui = create_ui();
let exit_code = match run(&mut ui) {
Ok(()) => 0,
Err(err) => report_command_error(&mut ui, err),
};
std::process::exit(exit_code);
}