-
-
Notifications
You must be signed in to change notification settings - Fork 644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(main): Refactor src/main.rs by splitting one massive main function into commands.rs #829
Conversation
I implemented the confirmation prompt function as simply as possible. I agree that if there is a good library, it is better to use it 👍 |
@jaeheonji |
@ken-matsui Oh! No need to be sorry! rather, thank you for suggesting a better alternative. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@a-kenji I found one question regarding the Zellij lifecycle. The main function I refactored is: if let Some(Command::Sessions(Sessions::ListSessions)) = opts.command {
commands::list_sessions();
}
if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command {
commands::kill_all_sessions(yes);
}
if let Some(Command::Sessions(Sessions::KillSession { ref target_session })) = opts.command {
commands::kill_session(target_session);
}
if let Some(path) = opts.server {
commands::start_server(path);
} else {
commands::start_client(opts);
} I do not understand how the Zellij lifecycle works, but is there a possibility that Zellij sequentially executes multiple commands at the same lifecycle for example If the answer is no, we can remove all wasteful if-evaluations to improve performance like the following. if let Some(Command::Sessions(Sessions::ListSessions)) = opts.command {
commands::list_sessions();
} else if let Some(Command::Sessions(Sessions::KillAllSessions { yes })) = opts.command {
commands::kill_all_sessions(yes);
} else if let Some(Command::Sessions(Sessions::KillSession { ref target_session })) = opts.command {
commands::kill_session(target_session);
} else if let Some(path) = opts.server {
commands::start_server(path);
} else {
commands::start_client(opts);
} |
@ken-matsui |
} | ||
} | ||
|
||
pub(crate) fn kill_session(target_session: &Option<String>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently have session-related functionality in sessions.rs
.
So, how about changing the name of the function to execute_kill_session
, and the kill_session_impl
inside the function importing the function kill_session
of sessions.rs
?
In my personal opinion, I think this would make more sense for file names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jaeheonji
Thanks for your suggestion!
How do you think the naming convention won't be unified if renaming kill_session
to execute_kill_session
by seeing it from the main function? If you would do so, all such as start_server
should be renamed with execute_start_server
respectively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first idea was to prevent (more clarify) the name of
kill_session
from being used in two places(sessions.rs
and commands.rs
). But as you told me, it looks right to change the name of start_server
for unity. I want to respect your opinion here. What do you think it's better to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jaeheonji
I personally mainly do not care about duplication of function names. I would think that's why there is a module system and visibility. We can rename it with as
or import an upper namespace instead.
However, this is just my personal view. I think we need code owners' opinions. How do you think about this problem, @a-kenji ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ken-matsui
I currently prefer the simpler names here, especially since in the main function the commands are already called with:
commands::kill_session(target_session);
If it turns out to be confusing we can still change it in the future.
Thanks for the insight here @jaeheonji.
process::exit(1); | ||
} else if yes { | ||
kill_all_sessions_impl(sessions); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Don't mind about the name of the variable. just example)
if sessions.is_empty() {
...
} else {
let mut run = true
if !yes {
run = Confirm::new()
.with_prompt("...")
.interact()
.unwrap()
}
if !run {
println!("Abort.");
process:exit(1);
}
// run `kill_session` as a loop
}
I don't know if this works normally, it looks like you don't need any additional functions. I want to know your opinion :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jaeheonji
Thanks! That would be clearer. I will improve the kill_all_sessions
function 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this at dfb3b9d without bringing a new variable and mutable :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks really good 👍
@a-kenji |
@ken-matsui, |
@a-kenji |
…n into commands.rs * fix(main): Remove unnecessary pub visibility from the main function in `src/main.rs` * fix(main): Avoid unnecessary if-evaluations in the main function of `src/main.rs` * fix(commands): Simplify kill_all_sessions
Thank you @ken-matsui for taking the time in writing this awesome pr! |
What I did:
src/main.rs
into multiple functions insrc/commands.rs
.dialoguer
to bring a confirmation prompt ([y/n]
) for the following code. (I thought it is better to use a well-maintained library instead of implementing low-code in the same function.)zellij/src/main.rs
Lines 46 to 60 in 043a3cf
NOTE: This PR depends on:
--index
option forattach
sub-command to choose the session indexed by provided number in the active sessions ordered creation date #824