Skip to content

Commit

Permalink
Add editor
Browse files Browse the repository at this point in the history
  • Loading branch information
PakhomovAlexander committed Dec 5, 2024
1 parent f472d71 commit 692e3b3
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 87 deletions.
2 changes: 2 additions & 0 deletions agenda.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Today we are going to:
- [ ] write TUI for the database
- [x] cargo workspace
- [x] draw real table
- [ ] draw editor <-

```bash
> just-db
Expand Down
2 changes: 2 additions & 0 deletions db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.211", features = ["derive"] }
strum = { version = "0.26.3", features = ["derive"] }
4 changes: 4 additions & 0 deletions db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod analyzer;
mod catalog;
pub mod optimizer;
mod parser;
11 changes: 9 additions & 2 deletions db/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use core::panic;
use std::{collections::HashMap, rc::Rc};

use serde::{Deserialize, Serialize};
use strum::Display;

use crate::{
analyzer::{LogicalNode, LogicalPlan, Operator},
catalog::{Catalog, ColumnSchema, TableId},
Expand Down Expand Up @@ -190,7 +193,7 @@ struct FullScanIterator {
tuples: Vec<Tuple>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq, Serialize, Deserialize)]
pub struct Tuple {
data: HashMap<String, Val>,
}
Expand All @@ -207,9 +210,13 @@ impl Tuple {
pub fn get(&self, key: &str) -> &Val {
self.data.get(key).unwrap()
}

pub fn keys(&self) -> Vec<String> {
self.data.keys().cloned().collect()
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq, Display, Serialize, Deserialize)]
pub enum Val {
Int(i32),
String(String),
Expand Down
7 changes: 6 additions & 1 deletion tui/.config/config.json5
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"keybindings": {
"Home": {
"Normal": {
"<q>": "Quit", // Quit the application
"<a>": "D", // Quit the application
"<i>": "ToEditingMode",
"<Ctrl-d>": "Quit", // Another way to quit
"<Ctrl-c>": "Quit", // Yet another way to quit
"<Ctrl-z>": "Suspend" // Suspend the application
},
"Typing": {
"<esc>": "ToNormalMode", // Quit the application
},
}
}
2 changes: 2 additions & 0 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
db = { path = "../db" }
better-panic = "0.3.0"
clap = { version = "4.5.20", features = [
"derive",
Expand Down Expand Up @@ -41,6 +42,7 @@ tokio-util = "0.7.12"
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
edtui = "0.9.2"

[build-dependencies]
anyhow = "1.0.90"
Expand Down
11 changes: 11 additions & 0 deletions tui/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crossterm::event::KeyEvent;
use db::optimizer::Tuple;
use serde::{Deserialize, Serialize};
use strum::Display;

Expand All @@ -12,4 +14,13 @@ pub enum Action {
ClearScreen,
Error(String),
Help,

D,

ToEditingMode,
ToNormalMode,

Typed(KeyEvent),

QueryResultReceived(Vec<Tuple>),
}
58 changes: 50 additions & 8 deletions tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::borrow::Borrow;

use color_eyre::Result;
use color_eyre::{eyre, Result};
use crossterm::event::KeyEvent;
use db::optimizer::{Tuple, Val};
use ratatui::prelude::Rect;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tracing::{debug, info};

use crate::{
action::Action,
components::{fps::FpsCounter, home::Home, table::Table, Component},
action::{self, Action},
components::{editor::Editor, fps::FpsCounter, table::Table, Component},
config::Config,
layout::AppLayout,
tui::{Event, Tui},
Expand All @@ -30,8 +31,9 @@ pub struct App {

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Mode {
Typing,
#[default]
Home,
Normal,
}

impl App {
Expand All @@ -42,14 +44,14 @@ impl App {
tick_rate,
frame_rate,
components: vec![
Box::new(Home::new()),
Box::new(Editor::new()),
Box::new(Table::new()),
Box::new(FpsCounter::default()),
],
should_quit: false,
should_suspend: false,
config: Config::new()?,
mode: Mode::Home,
mode: Mode::Normal,
last_tick_key_events: Vec::new(),
action_tx,
action_rx,
Expand Down Expand Up @@ -116,12 +118,50 @@ impl App {
fn handle_key_event(&mut self, key: KeyEvent) -> Result<()> {
let action_tx = self.action_tx.clone();
let Some(keymap) = self.config.keybindings.get(&self.mode) else {
return Ok(());
eyre::bail!("No such mode in keymap");
};

if self.mode == Mode::Typing {
match keymap.get(&vec![key]) {
Some(Action::ToNormalMode) => {
self.mode = Mode::Normal;
}
_ => {}
};
action_tx.send(Action::Typed(key))?;
return Ok(());
}

match keymap.get(&vec![key]) {
Some(action) => {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
if *action == Action::D {
let a = Action::QueryResultReceived(vec![
Tuple::new(vec![
("col1", Val::Int(1)),
("col2", Val::String("Hii1".to_string())),
("col3", Val::Int(111)),
]),
Tuple::new(vec![
("col1", Val::Int(2)),
("col2", Val::String("Hii2".to_string())),
("col3", Val::Int(222)),
]),
Tuple::new(vec![
("col1", Val::Int(3)),
("col2", Val::String("Hii3".to_string())),
("col3", Val::Int(333)),
]),
Tuple::new(vec![
("col1", Val::Int(4)),
("col2", Val::String("Hii4".to_string())),
("col3", Val::Int(444)),
]),
]);
action_tx.send(a.clone())?;
} else {
action_tx.send(action.clone())?;
}
}
_ => {
// If the key was not handled as a single key action,
Expand Down Expand Up @@ -153,6 +193,8 @@ impl App {
Action::ClearScreen => tui.terminal.clear()?,
Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
Action::Render => self.render(tui)?,
Action::ToEditingMode => self.mode = Mode::Typing,
Action::ToNormalMode => self.mode = Mode::Normal,
_ => {}
}
for component in self.components.iter_mut() {
Expand Down
2 changes: 1 addition & 1 deletion tui/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use tokio::sync::mpsc::UnboundedSender;

use crate::{action::Action, config::Config, tui::Event};

pub mod editor;
pub mod fps;
pub mod home;
pub mod table;

/// `Component` is a trait that represents a visual and interactive element of the user interface.
Expand Down
63 changes: 0 additions & 63 deletions tui/src/components/home.rs

This file was deleted.

Loading

0 comments on commit 692e3b3

Please sign in to comment.