Skip to content

Commit

Permalink
Merge pull request #4 from nathangavin/refactor-MVC
Browse files Browse the repository at this point in the history
Refactor mvc
  • Loading branch information
nathangavin authored Feb 15, 2024
2 parents 85ac02c + 8eef6f6 commit 2f77bc9
Show file tree
Hide file tree
Showing 10 changed files with 1,334 additions and 935 deletions.
927 changes: 0 additions & 927 deletions src/app.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod defaultController;
380 changes: 380 additions & 0 deletions src/controller/defaultController.rs

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ use crossterm::{
EnableMouseCapture,
DisableMouseCapture}};

mod app;
use app::RunningMode;
use app::App;
use model::{CsvModel::CsvModel, AppStateModel::AppStateModel, UtilsModel::RunningMode};
use controller::defaultController::run as run;

mod view;
mod model;
mod controller;

fn main() -> Result<(), io::Error>{
let args: Vec<String> = env::args().collect();
let app: App;
let mut app_data: CsvModel;
if args.len() > 1 {
let filename = match args.get(1) {
Some(name) => name,
Expand All @@ -29,7 +32,7 @@ fn main() -> Result<(), io::Error>{
return Ok(());
}
};
app = match App::load_file_into_app(String::from(filename)) {
app_data = match CsvModel::load_file(String::from(filename)) {
Ok(app) => app,
Err(_) => {
disable_raw_mode()?;
Expand All @@ -38,7 +41,7 @@ fn main() -> Result<(), io::Error>{
}
};
} else {
app = App::default();
app_data = CsvModel::default();
}
let running_mode = match args.get(2) {
Some(flag) => {
Expand All @@ -51,12 +54,14 @@ fn main() -> Result<(), io::Error>{
RunningMode::Normal
}
};
let mut app_state: AppStateModel;
app_state = AppStateModel::from_running_mode(&running_mode);
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let res = app.run(&mut terminal, running_mode);
let res = run(&mut app_data, &mut app_state, &mut terminal, running_mode);

disable_raw_mode()?;
execute!(
Expand All @@ -73,4 +78,3 @@ fn main() -> Result<(), io::Error>{

// TODO
// add button for showing commands
// split app model code and app view code, in order to add debugging viewmode
3 changes: 3 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod UtilsModel;
pub mod AppStateModel;
pub mod CsvModel;
130 changes: 130 additions & 0 deletions src/model/AppStateModel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use crate::model::UtilsModel::Position;
use crate::model::UtilsModel::InputMode;

use super::UtilsModel::RunningMode;

pub struct AppStateModel {
/// Current value of the input box
input: String,
/// Current input mode
input_mode: InputMode,
running_mode: RunningMode,
corner_pos: Position,
relative_pos: Position
}

impl Default for AppStateModel {
fn default() -> AppStateModel {
AppStateModel {
input: String::new(),
input_mode: InputMode::Normal,
running_mode: RunningMode::Normal,
corner_pos: Position { row: 0, col: 0 },
relative_pos: Position { row: 0, col: 0 },
}
}
}

impl AppStateModel {

pub fn from_running_mode(running_mode: &RunningMode) -> AppStateModel {
let mut state = AppStateModel::default();

match running_mode {
RunningMode::Normal => {},
RunningMode::Debug => {
state.running_mode = RunningMode::Debug;
}
};
state
}

pub fn get_relative_pos(&self) -> Position {
self.relative_pos
}

pub fn increment_relative_pos_row(&mut self) {
self.relative_pos.row += 1;
}

pub fn decrement_relative_pos_row(&mut self) {
if self.relative_pos.row > 0 {
self.relative_pos.row -= 1;
}
}

pub fn increment_relative_pos_col(&mut self) {
self.relative_pos.col += 1;
}

pub fn decrement_relative_pos_col(&mut self) {
if self.relative_pos.col > 0 {
self.relative_pos.col -= 1;
}

}

pub fn get_corner_pos(&self) -> Position {
self.corner_pos
}

pub fn add_to_corner_pos_row(&mut self, n: usize) {
self.corner_pos.row += n;
}

pub fn remove_from_corner_pos_row(&mut self, n: usize) {
if self.corner_pos.row > n {
self.corner_pos.row -= n;
} else {
self.corner_pos.row = 0;
}
}

pub fn add_to_corner_pos_col(&mut self, n: usize) {
self.corner_pos.col += n;
}

pub fn remove_from_corner_pos_col(&mut self, n: usize) {
if self.corner_pos.col > n {
self.corner_pos.col -= n;
} else {
self.corner_pos.col = 0;
}
}

pub fn append_str_current_input(&mut self, string_value : &str) {
self.input.push_str(&string_value);
}

pub fn append_char_current_input(&mut self, char_value : char) {
self.input.push(char_value);
}

pub fn pop_current_input(&mut self) -> Option<char> {
self.input.pop()
}

pub fn drain_input(&mut self) -> String {
self.input.drain(..).collect()
}

pub fn clear_input(&mut self) {
self.input.clear();
}

pub fn get_input(&self) -> &str {
self.input.as_str()
}

pub fn get_input_mode(&self) -> &InputMode {
&self.input_mode
}

pub fn set_input_mode(&mut self, input_node: InputMode) {
self.input_mode = input_node;
}

pub fn get_running_mode(&self) -> &RunningMode {
&self.running_mode
}
}
Loading

0 comments on commit 2f77bc9

Please sign in to comment.