Skip to content

Commit

Permalink
refactor(git): remove top level git module
Browse files Browse the repository at this point in the history
  • Loading branch information
florentinl committed Mar 10, 2024
1 parent 32cfc39 commit 7c25edf
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 356 deletions.
252 changes: 0 additions & 252 deletions src/editor/git.rs

This file was deleted.

44 changes: 14 additions & 30 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
//! This logic is handled by the `signal` module, which is called in the (`Editor::init_resize_listener` method).
//!
mod command;
mod git;
mod signal;
mod terminal;
mod view;
Expand All @@ -80,11 +79,10 @@ use std::{

use termion::input::TermRead;

use self::{git::Patch, view::FileView};
use self::view::FileView;

use {
command::Command,
git::{compute_diff, Diff},
terminal::{termion::TermionTerminalDrawer, StatusBarInfos, TerminalDrawer},
view::View,
};
Expand All @@ -103,9 +101,6 @@ pub struct Editor {
view: Arc<Mutex<View>>,
/// The mode of the editor
mode: Arc<Mutex<Mode>>,

/// Git diff since last commit if any
pub diff: Arc<Mutex<Option<Diff>>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -155,7 +150,6 @@ impl Editor {
Self {
view: arc_mutex!(View::new(path)),
mode: arc_mutex!(Mode::Normal),
diff: Arc::new(Mutex::new(None)),
}
}

Expand Down Expand Up @@ -297,9 +291,6 @@ impl Editor {

/// Initialize git operations
fn init_git_thread(&mut self, sender: Sender<RefreshOrder>) {
// Initialize the diff
self.diff = Arc::new(Mutex::new(None));

// Spawn a thread to compute the diff in background
let view = self.view.clone();
let locked_view = view.lock().unwrap();
Expand All @@ -309,22 +300,16 @@ impl Editor {
};
drop(locked_view);

let diff = self.diff.clone();
thread::spawn({
move || loop {
let view = view.lock().unwrap();
let file_name = view.file_name();
let file_dir = view.file_dir();
let new_diff = compute_diff(&view.dump_file(), &file_dir, &file_name).ok();
let mut current_diff = diff.lock().unwrap();

// If the diff has changed, redraw the diff markers
if new_diff != *current_diff {
*current_diff = new_diff;
sender.send(RefreshOrder::GitIndicators).unwrap();
let mut view = view.lock().unwrap();
let _ = view.refresh_diff();

if let Err(SendError(_)) = sender.send(RefreshOrder::GitIndicators) {
break;
}

// Drop the lock before sleeping
drop(current_diff);
drop(view);
thread::sleep(Duration::from_millis(250));
}
Expand All @@ -350,7 +335,6 @@ impl Editor {
fn refresh_tui(
tui: &mut TermionTerminalDrawer,
view: &mut View,
diff: &Arc<Mutex<Option<Vec<Patch>>>>,
status_bar_infos: &StatusBarInfos,
refresh_order: RefreshOrder,
) {
Expand All @@ -366,22 +350,24 @@ impl Editor {
tui.move_cursor(view.cursor)
}
RefreshOrder::GitIndicators => {
let locked_diff = diff.lock().unwrap();
tui.draw_diff_markers(locked_diff.as_ref().unwrap(), view);
if let Some(diff) = view.diff() {
tui.draw_diff_markers(diff, view);
}
}
RefreshOrder::Lines(lines) => tui.draw_lines(view, lines),
RefreshOrder::AllLines => {
tui.draw(view, status_bar_infos);
let locked_diff = diff.lock().unwrap();
tui.draw_diff_markers(locked_diff.as_ref().unwrap(), view);
if let Some(diff) = view.diff() {
tui.draw_diff_markers(diff, view);
}
}
RefreshOrder::Resize => {
let (width, height) = tui.get_term_size();

view.width = width;
view.height = height;

Self::refresh_tui(tui, view, diff, status_bar_infos, RefreshOrder::AllLines);
Self::refresh_tui(tui, view, status_bar_infos, RefreshOrder::AllLines);
}
}
}
Expand Down Expand Up @@ -412,7 +398,6 @@ impl Editor {

// Spawn a thread to draw the TUI in background
let view = self.view.clone();
let diff = self.diff.clone();
let mode = self.mode.clone();
thread::spawn({
move || loop {
Expand All @@ -427,7 +412,6 @@ impl Editor {
Self::refresh_tui(
&mut tui,
locked_view.deref_mut(),
&diff,
&status_bar_infos,
refresh_order,
);
Expand Down
Loading

0 comments on commit 7c25edf

Please sign in to comment.