Skip to content

Commit

Permalink
Select code, and adding tui-textarea as a submodule as it requires mo…
Browse files Browse the repository at this point in the history
…difications
  • Loading branch information
cosmikwolf committed Dec 19, 2023
1 parent 0dc87f2 commit 61ef0a7
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[submodule "lib/tui-textarea"]
path = lib/tui-textarea
url = https://github.com/cosmikwolf/tui-textarea
[submodule "lib/ansi-to-tui"]
path = lib/ansi-to-tui
url = https://github.com/cosmikwolf/ansi-to-tui
[submodule "lib/bat"]
path = lib/bat
url = [email protected]:sharkdp/bat.git
101 changes: 99 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Semantic GPT Programming Intelligence"
repository = "https://github.com/cosmikwolf/sazid"
authors = ["tenkai <[email protected]>"]
default-run = "sazid"

resolver = "2"
[[bin]]
name = "sazid"
path = "src/main.rs"
Expand All @@ -21,8 +21,12 @@ path = "bin/crossterm_character_test.rs"

[dependencies]
uuid = "1.5.0"
ansi-to-tui = { version = "3.1.0" }
tui-textarea = { version = "0.4.0", path = "./lib/tui-textarea" }
ansi-to-tui = { path = "./lib/ansi-to-tui" }
tui-textarea = { path = "./lib/tui-textarea", features = [
"crossterm",
"ratatui",
"ansi-escapes",
] }
bwrap = { version = "1.3.0", features = ["use_std"] }
async-openai = "0.16.3"
async-recursion = "1.0.5"
Expand Down Expand Up @@ -115,6 +119,7 @@ dsync = { version = "0.0.16", features = ["async"] }
tree-sitter = "0.20.10"
tree-sitter-rust = "0.20.4"
rust-sitter = "0.4.1"
clipboard = "0.5.0"

[dev-dependencies]
insta = { version = "1.34.0", features = [
Expand Down
1 change: 1 addition & 0 deletions lib/ansi-to-tui
Submodule ansi-to-tui added at cc377f
2 changes: 1 addition & 1 deletion lib/tui-textarea
52 changes: 44 additions & 8 deletions src/app/session_view.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use bat::{assets::HighlightingAssets, config::Config, controller::Controller, style::StyleComponents, Input};
use bat::{assets::HighlightingAssets, config::Config, controller::Controller, style::StyleComponents};
use color_eyre::owo_colors::OwoColorize;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use nu_ansi_term::Color::*;
use nu_ansi_term::Style;
use ratatui::style::Modifier;
use ratatui::widgets::Block;
use ratatui::widgets::Borders;
use std::default::Default;
use std::path::Path;
use textwrap::{self, Options, WordSeparator, WordSplitter, WrapAlgorithm};
use tui_textarea::TextArea;

use crate::action::Action;
use crate::trace_dbg;
use tui_textarea::{CursorMove, Input, Key, Scrolling, TextArea};

use super::errors::SazidError;
use super::{messages::MessageContainer, session_data::SessionData};
use ropey::Rope;

Expand All @@ -24,13 +33,36 @@ pub struct SessionView<'a> {
}

impl<'a> SessionView<'a> {
pub fn unfocus_textarea(&mut self) {
use ratatui::style::{Color, Style};
self.text_area.set_cursor_line_style(Style::default());
self.text_area.set_cursor_style(Style::default());
self.text_area.set_block(
Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(Color::DarkGray))
.title(" Inactive (^X to switch) "),
);
}

pub fn focus_textarea(&mut self) {
use ratatui::style::{Color, Style};
self.text_area.move_cursor(CursorMove::Top);
self.text_area.move_cursor(CursorMove::Head);
self
.text_area
.set_cursor_line_style(Style::default().add_modifier(Modifier::UNDERLINED).add_modifier(Modifier::SLOW_BLINK));
self.text_area.set_cursor_style(Style::default().bg(Color::Yellow));
self.text_area.set_block(Block::default().borders(Borders::ALL).style(Style::default()).title(" Active "));
}

pub fn set_window_width(&mut self, width: usize, _messages: &mut [MessageContainer]) {
let new_value = width - 6;
if self.window_width != new_value {
trace_dbg!("setting window width to {}", new_value);

self.window_width = new_value;
self.renderer.config.term_width = 10000;
self.renderer.config.term_width = new_value;
//self.renderer.config.term_width = new_value;
}
}
Expand Down Expand Up @@ -74,7 +106,7 @@ impl<'a> SessionView<'a> {
let left_padding = self.window_width.saturating_sub(text_width) / 2;
trace_dbg!("left_padding: {}\ttext_width: {}, window_width: {}", left_padding, text_width, self.window_width);
let stylized = self.renderer.render_message_bat(format!("{}", &message).as_str());
let options = Options::new(text_width)
let options = Options::new(text_width-10)
//.break_words(false)
.word_splitter(WordSplitter::NoHyphenation)
.word_separator(WordSeparator::AsciiSpace)
Expand Down Expand Up @@ -104,7 +136,10 @@ impl<'a> SessionView<'a> {

self.new_data = true;
self.text_area.replace_at_end(message.stylized.to_string(), original_message_length);
trace_dbg!("stylized {}\n\n{}", message.stylized.red(), self.rendered_text.len_chars());

message.stylized.to_string().lines().for_each(|l| {
trace_dbg!("line: {:#?}", l);
});

self.rendered_text.remove(rendered_text_message_start_index..);
self.rendered_text.append(message.stylized.clone());
Expand Down Expand Up @@ -149,7 +184,7 @@ impl<'a> BatRenderer<'a> {
]);
let config: Config<'static> = Config {
colored_output: true,
language: Some("markdown"),
language: Some("Markdown Extended"),
style_components,
show_nonprintable: false,
tab_width: 2,
Expand All @@ -161,7 +196,8 @@ impl<'a> BatRenderer<'a> {
use_custom_assets: true,
..Default::default()
};
let assets = HighlightingAssets::from_binary();
// let assets = HighlightingAssets::from_binary();
let assets = HighlightingAssets::from_cache(&Path::new("./lib/bat/assets")).unwrap();
let _buffer: Vec<u8> = Vec::new();
BatRenderer { config, assets }
}
Expand All @@ -181,7 +217,7 @@ impl<'a> BatRenderer<'a> {
//trace_dbg!("render_message_bat: {:?}", self.rendered_bytes);

//text.bytes().skip(self.buffer.len()).for_each(|b| self.buffer.push(b));
let input = Input::from_bytes(text.as_bytes());
let input = bat::Input::from_bytes(text.as_bytes());
//trace_dbg!("render_message_bat: {:?}", self.rendered_bytes);
controller.run_with_error_handler(vec![input.into()], Some(&mut buffer), Self::render_error).unwrap();
//trace_dbg!("render_message_bat: {:?}", buffer);
Expand Down
6 changes: 0 additions & 6 deletions src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,16 @@ impl Component for Home<'static> {
},
Action::EnterCommand => {
self.mode = Mode::Command;
self.session.mode = Mode::Command;
},
Action::EnterNormal => {
self.mode = Mode::Normal;
self.session.mode = Mode::Normal;
},
Action::EnterVisual => {
self.mode = Mode::Visual;
self.session.mode = Mode::Visual;
},
Action::EnterInsert => {
trace_dbg!("enter insert mode");
self.mode = Mode::Insert;
self.session.mode = Mode::Insert;
},
Action::CommandResult(result) => {
self.replace_input(result);
Expand All @@ -137,12 +133,10 @@ impl Component for Home<'static> {
Action::EnterProcessing => {
self.clear_input();
self.mode = Mode::Processing;
self.session.mode = Mode::Processing;
},
Action::ExitProcessing => {
// TODO: Make this go to previous mode instead
self.mode = Mode::Normal;
self.session.mode = Mode::Normal;
},
_ => (),
}
Expand Down
Loading

0 comments on commit 61ef0a7

Please sign in to comment.