##// END OF EJS Templates
rhg: pass `ui` to `Command` `run`...
Antoine Cezar -
r46009:ed95ccc9 default
parent child Browse files
Show More
@@ -1,10 +1,11 b''
1 pub mod files;
1 pub mod files;
2 pub mod root;
2 pub mod root;
3 use crate::error::CommandError;
3 use crate::error::CommandError;
4 use crate::ui::Ui;
4
5
5 /// The common trait for rhg commands
6 /// The common trait for rhg commands
6 ///
7 ///
7 /// Normalize the interface of the commands provided by rhg
8 /// Normalize the interface of the commands provided by rhg
8 pub trait Command<'a> {
9 pub trait Command {
9 fn run(&self) -> Result<(), CommandError>;
10 fn run(&self, ui: &Ui) -> Result<(), CommandError>;
10 }
11 }
@@ -11,18 +11,16 b' List tracked files.'
11 Returns 0 on success.
11 Returns 0 on success.
12 ";
12 ";
13
13
14 pub struct FilesCommand<'a> {
14 pub struct FilesCommand {}
15 ui: &'a Ui,
16 }
17
15
18 impl<'a> FilesCommand<'a> {
16 impl FilesCommand {
19 pub fn new(ui: &'a Ui) -> Self {
17 pub fn new() -> Self {
20 FilesCommand { ui }
18 FilesCommand {}
21 }
19 }
22 }
20 }
23
21
24 impl<'a> Command<'a> for FilesCommand<'a> {
22 impl Command for FilesCommand {
25 fn run(&self) -> Result<(), CommandError> {
23 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
26 let operation_builder = ListTrackedFiles::new()?;
24 let operation_builder = ListTrackedFiles::new()?;
27 let operation = operation_builder.load().map_err(|err| {
25 let operation = operation_builder.load().map_err(|err| {
28 CommandErrorKind::Abort(Some(
26 CommandErrorKind::Abort(Some(
@@ -47,7 +45,7 b" impl<'a> Command<'a> for FilesCommand<'a"
47 .expect("cwd was already checked within the repository");
45 .expect("cwd was already checked within the repository");
48 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));
46 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));
49
47
50 let mut stdout = self.ui.stdout_buffer();
48 let mut stdout = ui.stdout_buffer();
51
49
52 for file in files {
50 for file in files {
53 stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?;
51 stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?;
@@ -10,24 +10,22 b' Print the root directory of the current '
10 Returns 0 on success.
10 Returns 0 on success.
11 ";
11 ";
12
12
13 pub struct RootCommand<'a> {
13 pub struct RootCommand {}
14 ui: &'a Ui,
15 }
16
14
17 impl<'a> RootCommand<'a> {
15 impl RootCommand {
18 pub fn new(ui: &'a Ui) -> Self {
16 pub fn new() -> Self {
19 RootCommand { ui }
17 RootCommand {}
20 }
18 }
21 }
19 }
22
20
23 impl<'a> Command<'a> for RootCommand<'a> {
21 impl Command for RootCommand {
24 fn run(&self) -> Result<(), CommandError> {
22 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
25 let path_buf = FindRoot::new().run()?;
23 let path_buf = FindRoot::new().run()?;
26
24
27 let bytes = get_bytes_from_path(path_buf);
25 let bytes = get_bytes_from_path(path_buf);
28
26
29 // TODO use formating macro
27 // TODO use formating macro
30 self.ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?;
28 ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?;
31
29
32 Ok(())
30 Ok(())
33 }
31 }
@@ -29,8 +29,8 b' fn main() {'
29
29
30 let command_result = match matches.subcommand_name() {
30 let command_result = match matches.subcommand_name() {
31 Some(name) => match name {
31 Some(name) => match name {
32 "root" => commands::root::RootCommand::new(&ui).run(),
32 "root" => commands::root::RootCommand::new().run(&ui),
33 "files" => commands::files::FilesCommand::new(&ui).run(),
33 "files" => commands::files::FilesCommand::new().run(&ui),
34 _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND),
34 _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND),
35 },
35 },
36 _ => {
36 _ => {
General Comments 0
You need to be logged in to leave comments. Login now