app.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. use crate::application::mode::command::CommandData;
  2. use crate::application::mode::{ModeData, ModeKey};
  3. use crate::application::Application;
  4. use crate::errors::*;
  5. pub fn exit(app: &mut Application) -> Result<()> {
  6. app.switch_mode(ModeKey::Exit);
  7. Ok(())
  8. }
  9. pub fn exit_with_check(app: &mut Application) -> Result<()> {
  10. if let Some(ref buf) = app.workspace.current_buffer {
  11. if buf.modified() {
  12. // 输出提示(todo)
  13. if let ModeData::Command(ref mut command_data) = app.mode {
  14. command_data.reset();
  15. }
  16. app.switch_mode(ModeKey::Normal);
  17. } else {
  18. app.switch_mode(ModeKey::Exit);
  19. }
  20. }
  21. Ok(())
  22. }
  23. pub fn to_insert_mode(app: &mut Application) -> Result<()> {
  24. app.switch_mode(ModeKey::Insert);
  25. Ok(())
  26. }
  27. pub fn to_normal_mode(app: &mut Application) -> Result<()> {
  28. app.switch_mode(ModeKey::Normal);
  29. Ok(())
  30. }
  31. pub fn to_workspace_mode(app: &mut Application) -> Result<()> {
  32. app.switch_mode(ModeKey::Workspace);
  33. Ok(())
  34. }
  35. pub fn to_command_mode(app: &mut Application) -> Result<()> {
  36. app.switch_mode(ModeKey::Command);
  37. Ok(())
  38. }
  39. pub fn to_search_mode(app: &mut Application) -> Result<()> {
  40. app.switch_mode(ModeKey::Search);
  41. Ok(())
  42. }
  43. pub fn to_delete_mode(app: &mut Application) -> Result<()> {
  44. app.switch_mode(ModeKey::Delete);
  45. Ok(())
  46. }