command.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use held_core::{
  2. utils::position::Position,
  3. view::{colors::Colors, style::CharStyle},
  4. };
  5. use crate::view::status_data::StatusLineData;
  6. use super::{ModeData, ModeRenderer};
  7. const EDITED_NO_STORE: &'static str = "Changes have not been saved";
  8. const NOT_FOUNT_CMD: &'static str = "Command Not Fount";
  9. pub(super) struct CommandRenderer;
  10. impl ModeRenderer for CommandRenderer {
  11. fn render(
  12. workspace: &mut crate::workspace::Workspace,
  13. monitor: &mut crate::view::monitor::Monitor,
  14. mode: &mut super::ModeData,
  15. ) -> super::Result<()> {
  16. let line = monitor.height()? - 1;
  17. let mut presenter = monitor.build_presenter()?;
  18. if let Some(buffer) = &workspace.current_buffer {
  19. let data = buffer.data();
  20. presenter.print_buffer(buffer, &data, &workspace.syntax_set, None, None)?;
  21. let mode_name_data = StatusLineData {
  22. content: " COMMAND ".to_string(),
  23. color: Colors::Inverted,
  24. style: CharStyle::Bold,
  25. };
  26. let cmd_str = if let ModeData::Command(command_data) = mode {
  27. command_data.input.clone()
  28. } else {
  29. String::new()
  30. };
  31. let command_line_str = ":".to_owned() + &cmd_str;
  32. let command_data = StatusLineData {
  33. content: command_line_str.clone(),
  34. color: Colors::Default,
  35. style: CharStyle::Default,
  36. };
  37. presenter.print_status_line(&[mode_name_data, command_data])?;
  38. let offset = " COMMAND ".len() + command_line_str.len();
  39. presenter.set_cursor(Position { line, offset });
  40. presenter.present()?;
  41. } else {
  42. }
  43. Ok(())
  44. }
  45. }
  46. #[derive(Debug)]
  47. pub struct CommandData {
  48. pub input: String,
  49. }
  50. impl CommandData {
  51. pub fn new() -> Self {
  52. CommandData {
  53. input: String::new(),
  54. }
  55. }
  56. pub fn reset(&mut self) {
  57. self.input.clear();
  58. }
  59. }