Skip to content

Commit

Permalink
Merge pull request #3 from honhimW/develop
Browse files Browse the repository at this point in the history
Fix: initial error & tui restore
  • Loading branch information
honhimW authored Sep 14, 2024
2 parents 7765161 + 57981c1 commit 1e41749
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 49 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ once_cell = "1"

[build-dependencies]
cc="*"

[profile.release]
lto = true
opt-level = "z"
codegen-units = 1
strip = "debuginfo"
55 changes: 10 additions & 45 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::time::Duration;
use tokio::time::{interval};
use crate::app::AppState::Closed;
use crate::bus::{publish_msg, try_take_msg, Message};
use crate::tui::TerminalBackEnd;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -57,7 +58,9 @@ async fn main() -> Result<()> {
}
}

render(App::new(db_config), app_config).await?;
let terminal = tui::init()?;
let app = App::new(db_config);
let app_result = run(app, terminal, app_config).await;

if let Err(e) = tui::restore() {
eprintln!(
Expand All @@ -66,11 +69,14 @@ async fn main() -> Result<()> {
);
}

if let Err(e) = app_result {
eprintln!("{}", e);
}

Ok(())
}

async fn render(mut app: App, config: Configuration) -> Result<()> {
let mut terminal = tui::init()?;
async fn run(mut app: App, mut terminal: TerminalBackEnd, config: Configuration) -> Result<()> {
let fps = cmp::min(config.fps.unwrap_or(30) as usize, 60);
let delay_millis = 1000 / fps;
let delay_duration = Duration::from_millis(delay_millis as u64);
Expand Down Expand Up @@ -144,45 +150,4 @@ async fn render(mut app: App, config: Configuration) -> Result<()> {
}
app.state = Closed;
Ok(())
}

// async fn handle_events(app_arc: Arc<RwLock<App>>) -> Result<()> {
// loop {
// let app_read_guard = app_arc.read().await;
// if !app_read_guard.health() {
// break;
// }
// if app_read_guard.state == AppState::Preparing {
// let mut context_write_guard = app_read_guard.context.write().await;
// context_write_guard.on_app_event(AppEvent::Init).await?;
// drop(context_write_guard);
// drop(app_read_guard);
// let mut app_write_guard = app_arc.write().await;
// app_write_guard.state = AppState::Running;
// continue;
// }
// let event_result = app_read_guard.input.receiver().try_recv();
// drop(app_read_guard);
// if let Ok(input_event) = event_result {
// if let InputEvent::Input(event) = input_event {
// if let Event::Key(key_event) = event {
// if key_event.kind == KeyEventKind::Press {
// if key_event.modifiers == KeyModifiers::CONTROL && key_event.code == KeyCode::Char('c') {
// let mut app_write_guard = app_arc.write().await;
// app_write_guard.state = AppState::Closing;
// drop(app_write_guard);
// } else {
// let app_read_guard = app_arc.read().await;
// let mut context_write_guard = app_read_guard.context.write().await;
// let _ = context_write_guard.handle_key_event(key_event).await?;
// drop(context_write_guard);
// drop(app_read_guard);
// }
// }
// }
// }
// }
// }
//
// Ok(())
// }
}
26 changes: 24 additions & 2 deletions src/redis_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ where
});
Ok(())
} else {
Err(anyhow!(""))
Ok(())
}
}

/// ```
/// let value = async_redis_opt(|operations| async move {
/// Ok(operations.get::<_, String>("key_to_get").await?)
/// }).await?;
///
/// let value: String = async_redis_opt(|operations| async move {
/// Ok(operations.get("key_to_get").await?)
/// }).await?;
/// ```
pub async fn async_redis_opt<F, FUT, R>(opt: F) -> Result<R>
where
F: FnOnce(RedisOperations) -> FUT,
Expand All @@ -50,7 +59,7 @@ where
if let Some(c) = x {
opt(c.clone()).await
} else {
Err(anyhow!(""))
Err(anyhow!("redis operations not initialized"))
}
}

Expand Down Expand Up @@ -532,6 +541,19 @@ impl RedisOperations {
}
}

pub async fn xlen<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<usize> {
if self.is_cluster() {
let pool = &self.cluster_pool.clone().context("should be cluster")?;
let mut connection = pool.get().await?;
let v: usize = connection.xlen(key).await?;
Ok(v)
} else {
let mut connection = self.pool.get().await?;
let v: usize = connection.xlen(key).await?;
Ok(v)
}
}

// pub async fn sscan<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<Vec<String>> {
// if self.is_cluster() {
// let pool = &self.cluster_pool.clone().context("should be cluster")?;
Expand Down
5 changes: 3 additions & 2 deletions src/tabs/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ impl ExplorerTab {
"hash" => Ok(op.hlen(key_name_clone).await?),
"set" => Ok(op.scard(key_name_clone).await?),
"zset" => Ok(op.zcard(key_name_clone).await?),
"stream" => Ok(op.xlen(key_name_clone).await?),
_ => Ok(0)
}
}).await;
Expand Down Expand Up @@ -1189,15 +1190,15 @@ impl Listenable for ExplorerTab {
fn on_app_event(&mut self, _app_event: AppEvent) -> Result<()> {
if _app_event == AppEvent::Init {
if let Some(first_line) = self.get_filter_text() {
self.do_scan(first_line.clone())?;
self.do_scan(first_line)?;
}
}
if _app_event == AppEvent::Reset {
self.show_delete_popup = false;
self.show_filter = false;
self.filter_text_area = TextArea::default();
if let Some(first_line) = self.get_filter_text() {
self.do_scan(first_line.clone())?;
self.do_scan(first_line)?;
}
}
Ok(())
Expand Down

0 comments on commit 1e41749

Please sign in to comment.