Skip to content

Commit

Permalink
query functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
rhasanm committed Jul 1, 2024
1 parent 4098a3a commit 6f1f401
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "csbee"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
Expand Down
25 changes: 25 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct App {
pub schema_scroller: Scroller,
pub table_scroller: Scroller,
pub filter_input: String,
pub query_input: String,
pub input: String,
pub character_index: usize,
pub input_mode: InputMode,
Expand All @@ -43,6 +44,7 @@ impl App {
table_scroller: Scroller::default(),
input: String::new(),
filter_input: String::new(),
query_input: String::new(),
input_mode: InputMode::Normal,
character_index: 0,
}
Expand All @@ -64,6 +66,9 @@ impl App {
InputMode::Filter => {
self.filter_input.insert(index, new_char);
}
InputMode::Query => {
self.query_input.insert(index, new_char);
}
_ => {
self.input.insert(index, new_char);
}
Expand All @@ -80,6 +85,13 @@ impl App {
.nth(self.character_index)
.unwrap_or(self.filter_input.len())
}
InputMode::Query => {
self.query_input
.char_indices()
.map(|(i, _)| i)
.nth(self.character_index)
.unwrap_or(self.query_input.len())
}
_ => {
self.input
.char_indices()
Expand All @@ -97,6 +109,12 @@ impl App {
let from_left_to_current_index = current_index - 1;

match self.input_mode {
InputMode::Query => {
let before_char_to_delete = self.query_input.chars().take(from_left_to_current_index);
let after_char_to_delete = self.query_input.chars().skip(current_index);

self.query_input = before_char_to_delete.chain(after_char_to_delete).collect();
}
InputMode::Filter => {
let before_char_to_delete = self.filter_input.chars().take(from_left_to_current_index);
let after_char_to_delete = self.filter_input.chars().skip(current_index);
Expand All @@ -116,6 +134,9 @@ impl App {

pub fn clamp_cursor(&self, new_cursor_pos: usize) -> usize {
match self.input_mode {
InputMode::Query => {
new_cursor_pos.clamp(0, self.query_input.chars().count())
}
InputMode::Filter => {
new_cursor_pos.clamp(0, self.filter_input.chars().count())
}
Expand All @@ -131,6 +152,10 @@ impl App {

pub fn submit_message(&mut self) {
match self.input_mode {
InputMode::Query => {
self.df = self.sql_ctx.execute(&self.query_input).and_then(LazyFrame::collect).unwrap();
self.query_input.clear();
}
InputMode::Filter => {
self.df = self.sql_ctx.execute(format!("select * from df where {}", self.filter_input).as_str()).and_then(LazyFrame::collect).unwrap();
self.filter_input.clear();
Expand Down
7 changes: 5 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
use std::io::Result;

use crate::{app::{App, InputMode}, handler::{handle_filter_key_event, handle_schema_key_event, handle_table_key_event}};
use crate::{app::{App, InputMode}, handler::{handle_filter_key_event, handle_query_key_event, handle_schema_key_event, handle_table_key_event}};

#[derive(Debug, Default)]
pub struct EventHandler {}
Expand Down Expand Up @@ -45,7 +45,10 @@ impl EventHandler {
_ => handle_filter_key_event(key_event, app)
}
InputMode::Filter => {}
InputMode::Query => todo!(),
InputMode::Query if key_event.kind == KeyEventKind::Press => match key_event.code {
_ => handle_query_key_event(key_event, app)
}
InputMode::Query => {}
InputMode::Order => todo!(),
InputMode::Table => handle_table_key_event(key_event, app),
InputMode::Schema => handle_schema_key_event(key_event, app),
Expand Down
22 changes: 22 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ pub fn handle_table_key_event(key_event: KeyEvent, app: &mut App) {
}
_ => {}
}
}

pub fn handle_query_key_event(key_event: KeyEvent, app: &mut App) {
match key_event.code {
KeyCode::Enter => app.submit_message(),
KeyCode::Char(to_insert) => {
app.enter_char(to_insert);
}
KeyCode::Backspace => {
app.delete_char();
}
KeyCode::Left => {
app.move_cursor_left();
}
KeyCode::Right => {
app.move_cursor_right();
}
KeyCode::Esc => {
app.input_mode = InputMode::Normal;
}
_ => {}
}
}
7 changes: 7 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ pub fn ui(frame: &mut Frame, app: &mut App) {

// ==================== FUNCTIONS
match app.input_mode {
InputMode::Query => {
#[allow(clippy::cast_possible_truncation)]
frame.set_cursor(
fn_chunks[0].x + app.character_index as u16 + 1,
fn_chunks[0].y + 1,
);
}
InputMode::Filter => {
#[allow(clippy::cast_possible_truncation)]
frame.set_cursor(
Expand Down
2 changes: 1 addition & 1 deletion src/ui/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn functions_schema(app: &mut App) -> (Paragraph, Paragraph, Paragraph) {
InputMode::Filter => Style::default().fg(Color::Yellow),
_ => Style::default()
};
let query = Paragraph::new(app.input.as_str())
let query = Paragraph::new(app.query_input.as_str())
.style(Style::default())
.block(Block::bordered().title("Query"));
let filter = Paragraph::new(app.filter_input.as_str())
Expand Down

0 comments on commit 6f1f401

Please sign in to comment.