Skip to content

Commit

Permalink
Add Store trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Aug 17, 2024
1 parent 2de9baa commit b0eabd4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
31 changes: 11 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,10 @@ use tokio::{
use tracing_subscriber::field::debug;

use crate::{
action::{self, Action, DiffMode},
cli::Cli,
components::{fps::FpsCounter, home::Home, Component},
config::{Config, RuntimeConfig},
diff::{diff_and_mark, diff_and_mark_delete},
mode::Mode,
old_config::OldConfig,
runner::{run_executor, run_executor_precise},
search::search_and_mark,
termtext, tui,
types::ExecutionId,
action::{self, Action, DiffMode}, cli::Cli, components::{fps::FpsCounter, home::Home, Component}, config::{Config, RuntimeConfig}, diff::{diff_and_mark, diff_and_mark_delete}, mode::Mode, old_config::OldConfig, runner::{run_executor, run_executor_precise}, search::search_and_mark, store::Store, termtext, tui, types::ExecutionId
};

pub struct App {
pub struct App<S: Store + Clone + Send + 'static> {
pub config: Config,
pub runtime_config: RuntimeConfig,
pub tick_rate: f64,
Expand All @@ -38,6 +28,7 @@ pub struct App {
pub last_tick_key_events: Vec<KeyEvent>,
pub timemachine_mode: bool,
pub search_query: Option<String>,
store: S,
is_precise: bool,
diff_mode: Option<DiffMode>,
is_suspend: Arc<Mutex<bool>>,
Expand All @@ -49,8 +40,8 @@ pub struct App {
shell: Option<(String, Vec<String>)>,
}

impl App {
pub fn new(cli: Cli) -> Result<Self> {
impl<S: Store + Clone + Send> App<S> {
pub fn new(cli: Cli, store: S) -> Result<Self> {
let runtime_config = RuntimeConfig {
interval: cli.interval,
command: cli.command,
Expand Down Expand Up @@ -108,6 +99,7 @@ impl App {
let is_skip_empty_diffs = cli.is_skip_empty_diffs || default_skip_empty_diffs;

Ok(Self {
store,
tick_rate: 1.0,
frame_rate: 20.0,
components,
Expand Down Expand Up @@ -138,19 +130,18 @@ impl App {
pub async fn run(&mut self) -> Result<()> {
let (action_tx, mut action_rx) = mpsc::unbounded_channel();

let store = crate::store::MemoryStore::new();
let executor_handle = if self.is_precise {
tokio::spawn(run_executor_precise(
action_tx.clone(),
store.clone(),
self.store.clone(),
self.runtime_config.clone(),
self.shell.clone(),
self.is_suspend.clone(),
))
} else {
tokio::spawn(run_executor(
action_tx.clone(),
store.clone(),
self.store.clone(),
self.runtime_config.clone(),
self.shell.clone(),
self.is_suspend.clone(),
Expand Down Expand Up @@ -283,7 +274,7 @@ impl App {
Action::ShowExecution(id, end_id) => {
let style =
termtext::convert_to_anstyle(self.config.get_style("background"));
let record = store.get_record(id);
let record = self.store.get_record(id);
let mut string = "".to_string();
if let Some(record) = record {
action_tx.send(Action::SetClock(record.start_time))?;
Expand All @@ -301,7 +292,7 @@ impl App {
string = result.plain_text();
if let Some(diff_mode) = self.diff_mode {
if let Some(previous_id) = record.previous_id {
let previous_record = store.get_record(previous_id);
let previous_record = self.store.get_record(previous_id);
if let Some(previous_record) = previous_record {
let previous_result = termtext::Converter::new(style)
.convert(&previous_record.stdout);
Expand Down Expand Up @@ -346,7 +337,7 @@ impl App {
Action::SetTimemachineMode(timemachine_mode) => {
self.timemachine_mode = timemachine_mode;
if !timemachine_mode {
if let Some(latest_id) = store.get_latest_id() {
if let Some(latest_id) = self.store.get_latest_id() {
action_tx.send(Action::ShowExecution(latest_id, latest_id))?;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ async fn tokio_main() -> Result<()> {

let args = Cli::parse();
let interval = Duration::from(args.interval);
let mut app = App::new(args)?;
let store = store::MemoryStore::new();
let mut app = App::new(args, store)?;
app.run().await?;

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use crate::{
action::Action,
config::{Config, RuntimeConfig},
exec::exec,
store::{Record, MemoryStore},
store::{MemoryStore, Record, Store},
types::ExecutionId,
};

pub async fn run_executor(
pub async fn run_executor<S: Store + Send>(
actions: mpsc::UnboundedSender<Action>,
mut store: MemoryStore,
mut store: S,
runtime_config: RuntimeConfig,
shell: Option<(String, Vec<String>)>,
is_suspend: Arc<Mutex<bool>>,
Expand Down Expand Up @@ -88,9 +88,9 @@ pub async fn run_executor(
}
}

pub async fn run_executor_precise(
pub async fn run_executor_precise<S: Store + Send>(
actions: mpsc::UnboundedSender<Action>,
mut store: MemoryStore,
mut store: S,
runtime_config: RuntimeConfig,
shell: Option<(String, Vec<String>)>,
is_suspend: Arc<Mutex<bool>>,
Expand Down
14 changes: 11 additions & 3 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ use chrono::{DateTime, Local};

use crate::types::ExecutionId;

pub trait Store {
fn add_record(&mut self, record: Record);
fn get_record(&self, id: ExecutionId) -> Option<Record>;
fn get_latest_id(&self) -> Option<ExecutionId>;
}

#[derive(Debug, Clone)]
pub struct Record {
pub id: ExecutionId,
Expand Down Expand Up @@ -39,23 +45,25 @@ impl MemoryStore {
})),
}
}
}

pub fn add_record(&mut self, record: Record) {
impl Store for MemoryStore {
fn add_record(&mut self, record: Record) {
if let Ok(mut data) = self.data.write() {
data.latest_id = Some(record.id);
data.records.insert(record.id, record);
}
}

pub fn get_record(&self, id: ExecutionId) -> Option<Record> {
fn get_record(&self, id: ExecutionId) -> Option<Record> {
if let Ok(data) = self.data.read() {
data.records.get(&id).cloned()
} else {
None
}
}

pub fn get_latest_id(&self) -> Option<ExecutionId> {
fn get_latest_id(&self) -> Option<ExecutionId> {
if let Ok(data) = self.data.read() {
data.latest_id
} else {
Expand Down

0 comments on commit b0eabd4

Please sign in to comment.