7 Commits

11 changed files with 579 additions and 500 deletions

17
AGENTS.md Normal file
View File

@@ -0,0 +1,17 @@
## Agent Instructions
### Build, Lint, and Test
- Build: `cargo build`
- Lint: `cargo clippy`
- Test: `cargo test`
- Run a single test: `cargo test --test <test_name>`
### Code Style
- **Formatting**: Use `cargo fmt` to format the code.
- **Imports**: Group imports by standard library, external crates, and project modules.
- **Types**: Use explicit types.
- **Naming Conventions**: Follow Rust's naming conventions (e.g., `snake_case` for variables and functions, `PascalCase` for types).
- **Error Handling**: Use `Result` and `?` for error propagation. Use `panic!` only for unrecoverable errors.
- **Logging**: Use the `log` crate for logging.

184
Cargo.lock generated
View File

@@ -91,6 +91,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anyhow"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]] [[package]]
name = "arrayref" name = "arrayref"
version = "0.3.9" version = "0.3.9"
@@ -271,6 +277,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg_aliases"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]] [[package]]
name = "cfg_aliases" name = "cfg_aliases"
version = "0.2.1" version = "0.2.1"
@@ -370,6 +382,15 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
] ]
[[package]]
name = "crossbeam-channel"
version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
dependencies = [
"crossbeam-utils",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.21" version = "0.8.21"
@@ -472,6 +493,17 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "filedescriptor"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e40758ed24c9b2eeb76c35fb0aebc66c626084edd827e07e1552279814c6682d"
dependencies = [
"libc",
"thiserror 1.0.69",
"winapi",
]
[[package]] [[package]]
name = "foldhash" name = "foldhash"
version = "0.1.5" version = "0.1.5"
@@ -802,6 +834,12 @@ version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.172" version = "0.2.172"
@@ -937,7 +975,7 @@ dependencies = [
"arrayvec", "arrayvec",
"bit-set", "bit-set",
"bitflags 2.9.0", "bitflags 2.9.0",
"cfg_aliases", "cfg_aliases 0.2.1",
"codespan-reporting", "codespan-reporting",
"half", "half",
"hashbrown", "hashbrown",
@@ -992,6 +1030,18 @@ dependencies = [
"jni-sys", "jni-sys",
] ]
[[package]]
name = "nix"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
dependencies = [
"bitflags 2.9.0",
"cfg-if",
"cfg_aliases 0.1.1",
"libc",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.19" version = "0.2.19"
@@ -1023,12 +1073,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "numtoa"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6aa2c4e539b869820a2b82e1aef6ff40aa85e65decdd5185e83fb4b1249cd00f"
[[package]] [[package]]
name = "objc" name = "objc"
version = "0.2.7" version = "0.2.7"
@@ -1365,12 +1409,39 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "pollster"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
[[package]] [[package]]
name = "portable-atomic" name = "portable-atomic"
version = "1.11.0" version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e"
[[package]]
name = "portable-pty"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4a596a2b3d2752d94f51fac2d4a96737b8705dddd311a32b9af47211f08671e"
dependencies = [
"anyhow",
"bitflags 1.3.2",
"downcast-rs",
"filedescriptor",
"lazy_static",
"libc",
"log",
"nix",
"serial2",
"shared_library",
"shell-words",
"winapi",
"winreg",
]
[[package]] [[package]]
name = "presser" name = "presser"
version = "0.3.1" version = "0.3.1"
@@ -1471,12 +1542,6 @@ dependencies = [
"bitflags 2.9.0", "bitflags 2.9.0",
] ]
[[package]]
name = "redox_termios"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20145670ba436b55d91fc92d25e71160fbfbdd57831631c8d7d36377a476f1cb"
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"
@@ -1632,6 +1697,33 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "serial2"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7d1d08630509d69f90eff4afcd02c3bd974d979225cbd815ff5942351b14375"
dependencies = [
"cfg-if",
"libc",
"winapi",
]
[[package]]
name = "shared_library"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11"
dependencies = [
"lazy_static",
"libc",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"
@@ -1651,11 +1743,14 @@ dependencies = [
name = "simplicitty" name = "simplicitty"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"crossbeam-channel",
"env_logger", "env_logger",
"glyphon", "glyphon",
"log", "log",
"termion", "pollster",
"portable-pty",
"tokio", "tokio",
"vte",
"wgpu", "wgpu",
"winit", "winit",
] ]
@@ -1827,18 +1922,6 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "termion"
version = "4.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3669a69de26799d6321a5aa713f55f7e2cd37bd47be044b50f2acafc42c122bb"
dependencies = [
"libc",
"libredox",
"numtoa",
"redox_termios",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.69" version = "1.0.69"
@@ -2059,6 +2142,16 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "vte"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5924018406ce0063cd67f8e008104968b74b563ee1b85dde3ed1f7cb87d3dbd"
dependencies = [
"arrayvec",
"memchr",
]
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.5.0" version = "2.5.0"
@@ -2292,7 +2385,7 @@ checksum = "ca6049eb2014a0e0d8689f9b787605dd71d5bbfdc74095ead499f3cff705c229"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"bitflags 2.9.0", "bitflags 2.9.0",
"cfg_aliases", "cfg_aliases 0.2.1",
"document-features", "document-features",
"hashbrown", "hashbrown",
"js-sys", "js-sys",
@@ -2322,7 +2415,7 @@ dependencies = [
"bit-set", "bit-set",
"bit-vec", "bit-vec",
"bitflags 2.9.0", "bitflags 2.9.0",
"cfg_aliases", "cfg_aliases 0.2.1",
"document-features", "document-features",
"hashbrown", "hashbrown",
"indexmap", "indexmap",
@@ -2384,7 +2477,7 @@ dependencies = [
"block", "block",
"bytemuck", "bytemuck",
"cfg-if", "cfg-if",
"cfg_aliases", "cfg_aliases 0.2.1",
"core-graphics-types", "core-graphics-types",
"glow", "glow",
"glutin_wgl_sys", "glutin_wgl_sys",
@@ -2431,6 +2524,22 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]] [[package]]
name = "winapi-util" name = "winapi-util"
version = "0.1.9" version = "0.1.9"
@@ -2440,6 +2549,12 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows" name = "windows"
version = "0.58.0" version = "0.58.0"
@@ -2722,7 +2837,7 @@ dependencies = [
"block2", "block2",
"bytemuck", "bytemuck",
"calloop", "calloop",
"cfg_aliases", "cfg_aliases 0.2.1",
"concurrent-queue", "concurrent-queue",
"core-foundation", "core-foundation",
"core-graphics", "core-graphics",
@@ -2770,6 +2885,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "winreg"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "wit-bindgen-rt" name = "wit-bindgen-rt"
version = "0.39.0" version = "0.39.0"

View File

@@ -9,8 +9,13 @@ tokio = { version = "1.44", features = ["full"] }
winit = { version = "0.30", features = ["wayland", "x11"] } winit = { version = "0.30", features = ["wayland", "x11"] }
wgpu = "25" wgpu = "25"
glyphon = "0.9" glyphon = "0.9"
portable-pty = "0.9"
vte = "0.15"
termion = "4" # For reading from the PTY in a non-blocking way
crossbeam-channel = "0.5"
# For logging and running the event loop
env_logger = "0.10" env_logger = "0.10"
log = "0.4" log = "0.4"
pollster = "0.3"

11
README.md Normal file
View File

@@ -0,0 +1,11 @@
# Simplicitty
## Roadmap
1. [ ] Complete keyboard input handling (arrow keys, backspace, etc.)
2. [ ] Implement a PTY interface to spawn and communicate with processes
3. [ ] Add support for terminal protocols (ANSI escape sequences)
4. [ ] Implement scrollback buffer and viewport scrolling
5. [ ] Add color and text styling support
6. [ ] Implement clipboard integration
7. [ ] Add configuration options

84
project.md Normal file
View File

@@ -0,0 +1,84 @@
# Module Breakdown and Responsibilities
## 1. src/pty.rs (The Shell Interface)
Responsibilities:
- Use portable-pty to create a new PTY.
- Spawn a default shell process (e.g., bash) connected to the PTY.
- Provide a way to write user input (from winit) to the PTY.
- Provide a way to read shell output from the PTY.
Key Implementation Detail: Reading from the PTY is a blocking operation. You
should do this on a separate thread. This thread will read data and send it back
to the main UI thread using a channel (like crossbeam-channel). This prevents
the UI from freezing while waiting for the shell to output something.
## 2. src/terminal.rs (The Brains / State Model)
Responsibilities:
- Hold the state of the terminal grid: a 2D array of cells, where each cell
contains a character, foreground color, background color, and style flags
(bold, italic, etc.).
- Contain a vte::Parser.
- Implement the vte::Perform trait. This is the core of this module. The vte
parser will call methods on your implementation as it processes the byte
stream from the PTY. For example:
- print(char): You update the character at the current cursor position in your
grid and advance the cursor.
- execute(byte): You handle control characters like newline (\n) or backspace
(\b).
- csi_dispatch(...): You handle complex ANSI escape sequences for changing
colors, moving the cursor, clearing the screen, etc.
- Keep track of the cursor's position and the current color/style settings.
## 3. src/renderer.rs (The Painter)
Responsibilities:
- Initialize wgpu: get an adapter and device, configure a surface on the winit
window.
- Initialize glyphon: create a FontSystem, Cache, Atlas, and TextRenderer.
- Contain the main render() function. This function will be called on every
frame.
- Inside render():
- Get the current terminal state.
- Iterate through the terminal grid.
- For each line of text, create a glyphon::Buffer. Set its text content,
colors, and styles based on the data in your grid.
- Use the glyphon::TextRenderer to prepare and draw all the buffers to the
wgpu surface.
- Draw the cursor as a solid block or underline.
## 4. src/main.rs (The Conductor)
Responsibilities:
- The main function.
- Initialize logging (env_logger).
- Create the winit event loop and window.
- Initialize your Pty, Terminal, and Renderer modules.
- Start the PTY reader thread.
- Run the main event loop.
### The Main Event Loop (winit::event_loop::run)
This is where everything comes together. You'll have a match statement to handle
different events:
- Event::WindowEvent::Resized: Tell the renderer about the new size. You'll also
need to inform the PTY of the new dimensions so that applications running in
the shell (like vim) can reflow correctly.
- Event::WindowEvent::KeyboardInput: Translate the keyboard event into
characters or escape sequences and write them to the pty module.
- Event::UserEvent: This is how your PTY thread will communicate with the main
thread. When the PTY thread reads new data, it sends it through the channel,
and you receive it here. You then feed this data into the terminal.vte_parser.
- Event::WindowEvent::RedrawRequested: This is the signal to render a new frame.
You call your renderer.render() function here. After processing PTY data, you
should always request a redraw.
- Event::WindowEvent::CloseRequested: Terminate the application.

View File

@@ -1,28 +1,61 @@
use crossbeam_channel::Receiver;
use log::{debug, error, info, trace, warn};
use std::sync::Arc; use std::sync::Arc;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use vte::Parser;
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use winit::event::WindowEvent; use winit::{
use winit::window::Window; event::WindowEvent,
event_loop::{ActiveEventLoop, EventLoopProxy},
window::{Window, WindowId},
};
mod rendering; mod pty;
mod renderer;
mod terminal;
#[derive(Debug)]
pub enum UserEvent {
PtyOutput(String),
}
struct TermState {
pty: pty::Pty,
terminal: terminal::Terminal,
renderer: renderer::Renderer,
vte_parser: vte::Parser,
}
pub struct Application { pub struct Application {
window_state: Option<rendering::State>,
runtime: Runtime, runtime: Runtime,
event_loop_proxy: EventLoopProxy<UserEvent>,
term_state: Option<TermState>,
} }
impl Application { impl Application {
pub fn new() -> Self { pub fn new(event_loop_proxy: EventLoopProxy<UserEvent>) -> Self {
trace!("Creating new Application");
Self { Self {
window_state: None,
runtime: Runtime::new().expect("Failed to create tokio runtime"), runtime: Runtime::new().expect("Failed to create tokio runtime"),
event_loop_proxy,
term_state: None,
} }
} }
fn start_pty_output_recv_thread(&self, recv: Receiver<String>) {
let proxy = self.event_loop_proxy.clone();
std::thread::spawn(move || {
while let Ok(output) = recv.recv() {
trace!("Read from the PTY output: {}", output);
proxy.send_event(UserEvent::PtyOutput(output)).ok();
}
});
}
} }
impl winit::application::ApplicationHandler for Application { impl winit::application::ApplicationHandler<UserEvent> for Application {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.window_state.is_some() { if self.term_state.is_some() {
return; return;
} }
@@ -31,39 +64,48 @@ impl winit::application::ApplicationHandler for Application {
let window_attributes = Window::default_attributes() let window_attributes = Window::default_attributes()
.with_inner_size(LogicalSize::new(width as f64, height as f64)) .with_inner_size(LogicalSize::new(width as f64, height as f64))
.with_title("Terminal emulator"); .with_title("Terminal emulator");
debug!("Creating a 800x600 window");
let window = Arc::new(event_loop.create_window(window_attributes).unwrap()); let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
self.window_state = Some(self.runtime.block_on(rendering::State::new(window))); trace!("Creating crossbeam channel for PTY output rendering");
let (send, recv) = crossbeam_channel::unbounded();
self.term_state = Some(TermState {
pty: pty::Pty::new(send),
terminal: terminal::Terminal::new(),
renderer: self.runtime.block_on(renderer::Renderer::new(window)),
vte_parser: vte::Parser::new(),
});
self.start_pty_output_recv_thread(recv);
}
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) {
if let Some(state) = &mut self.term_state {
match event {
UserEvent::PtyOutput(output) => {
for byte in output.bytes() {
state.vte_parser.advance(&mut state.terminal, &[byte]);
}
state.renderer.request_redraw();
}
}
}
} }
fn window_event( fn window_event(
&mut self, &mut self,
event_loop: &winit::event_loop::ActiveEventLoop, _event_loop: &ActiveEventLoop,
_window_id: winit::window::WindowId, _window_id: WindowId,
event: WindowEvent, event: WindowEvent,
) { ) {
let Some(state) = &mut self.window_state else { if let Some(state) = &mut self.term_state {
return; match event {
}; WindowEvent::RedrawRequested => {
state.renderer.render(&state.terminal);
match event {
WindowEvent::Resized(size) => {
state.resize(size);
}
WindowEvent::RedrawRequested => {
state.render().unwrap();
}
WindowEvent::KeyboardInput {
device_id: _,
event,
is_synthetic: _,
} => {
if event.state.is_pressed() {
state.handle_keyboard_input(&event);
} }
_ => (),
} }
WindowEvent::CloseRequested => event_loop.exit(),
_ => {}
} }
} }
} }

90
src/app/pty/mod.rs Normal file
View File

@@ -0,0 +1,90 @@
use crossbeam_channel::Sender;
use portable_pty::{Child, CommandBuilder, PtySize, native_pty_system};
use std::io::{Read, Write};
use std::thread;
pub struct Pty {
writer: Box<dyn Write + Send + 'static>,
child: Box<dyn Child>,
}
pub enum PtyError {
Io(std::io::Error),
}
//
// You need this `From` implementation!
impl From<std::io::Error> for PtyError {
fn from(err: std::io::Error) -> Self {
PtyError::Io(err)
}
}
impl Pty {
pub fn new(output_sender: crossbeam_channel::Sender<String>) -> Self {
let pty_system = native_pty_system();
// Create a new pty
let pair = pty_system
.openpty(PtySize {
rows: 24,
cols: 80,
// Not all systems support pixel_width, pixel_height,
// but it is good practice to set it to something
// that matches the size of the selected font. That
// is more complex than can be shown here in this
// brief example though!
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
// Spawn a shell into the pty
let cmd = CommandBuilder::new_default_prog();
// The `slave` end of the PTY is given to the shell process.
// The shell thinks it's talking to a real terminal.
let mut child = pair.slave.spawn_command(cmd).unwrap();
// We can now drop the slave, as it's been consumed by the child process.
drop(pair.slave);
// Our application will use the `master` end to communicate with the shell.
// We are cloning the master so we can use it in a separate thread for reading.
let writer = pair.master.take_writer().unwrap();
let reader = pair.master.try_clone_reader().unwrap();
start_reader_thread(output_sender.clone(), reader);
Self { writer, child }
}
pub fn send_command(&mut self, cmd: String) -> Result<(), PtyError> {
self.writer.write_all(cmd.as_bytes())?;
Ok(())
}
}
fn start_reader_thread(sender: Sender<String>, mut reader: Box<dyn Read + Send + 'static>) {
thread::spawn(move || {
let mut buffer = [0u8; 1024];
loop {
match reader.read(&mut buffer) {
Ok(0) => {
// EOF, the child process has exited.
break;
}
Ok(n) => {
// Append the read data to our output string.
// We use from_utf8_lossy to handle potential invalid UTF-8 sequences.
sender
.send(String::from_utf8_lossy(&buffer[..n]).to_string())
.unwrap();
}
Err(e) => {
// An error occurred.
eprintln!("Error reading from PTY: {}", e);
break;
}
}
}
});
}

View File

@@ -1,3 +1,4 @@
use log::{debug, trace};
use std::sync::Arc; use std::sync::Arc;
use wgpu::{ use wgpu::{
CommandEncoderDescriptor, CompositeAlphaMode, Device, DeviceDescriptor, Instance, CommandEncoderDescriptor, CompositeAlphaMode, Device, DeviceDescriptor, Instance,
@@ -6,13 +7,9 @@ use wgpu::{
TextureViewDescriptor, TextureViewDescriptor,
}; };
use winit::dpi::PhysicalSize; use winit::dpi::PhysicalSize;
use winit::event::KeyEvent;
use winit::keyboard::{Key, NamedKey};
use winit::window::Window; use winit::window::Window;
mod terminal_text; pub struct Renderer {
pub struct State {
surface: Surface<'static>, surface: Surface<'static>,
device: Device, device: Device,
queue: Queue, queue: Queue,
@@ -20,64 +17,11 @@ pub struct State {
size: PhysicalSize<u32>, size: PhysicalSize<u32>,
text: terminal_text::TerminalText, /// text: terminal_text::TerminalText,
window: Arc<Window>, window: Arc<Window>,
} }
impl State { impl Renderer {
pub fn resize(&mut self, new_size: PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
self.text.resize(new_size.width, new_size.height);
self.window.request_redraw();
}
}
pub fn render(&mut self) -> Result<(), SurfaceError> {
// Prepare with current configuration (which includes the current window size)
self.text.prepare(&self.device, &self.queue, &self.config);
let output = self.surface.get_current_texture()?;
let view = output
.texture
.create_view(&TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
{
let mut _render_pass = encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
self.text.render(&mut _render_pass);
}
self.queue.submit(Some(encoder.finish()));
output.present();
self.text.trim();
Ok(())
}
pub async fn new(window: Arc<Window>) -> Self { pub async fn new(window: Arc<Window>) -> Self {
let size = window.inner_size(); let size = window.inner_size();
@@ -173,43 +117,65 @@ impl State {
}; };
surface.configure(&device, &config); surface.configure(&device, &config);
// Set up text renderer
let text = terminal_text::TerminalText::new(&device, &queue, surface_format);
Self { Self {
surface, surface,
device, device,
queue, queue,
config, config,
size, size,
text,
window, window,
} }
} }
pub fn handle_keyboard_input(&mut self, event: &KeyEvent) { pub fn request_redraw(&self) {
match &event.logical_key { self.window.request_redraw()
Key::Named(k) => match k { }
NamedKey::Enter => {
self.text.handle_enter(); // The renderer takes the terminal state as an argument to its render method.
} // It does not store a copy of the grid itself.
_ => todo!(), pub fn render(&mut self, terminal: &super::terminal::Terminal) -> Result<(), SurfaceError> {
}, let grid = terminal.get_grid();
Key::Character(s) => {
for c in s.chars() { // 1. Iterate through the `grid`.
self.text.insert_char(c); // 2. For each row, create a glyphon::Buffer.
} // 3. Set the text, colors, and styles for that buffer from the row's `Cell` data.
} // 4. Draw all the buffers to the screen.
Key::Dead(_) => { // 5. Draw the cursor at terminal.get_cursor_pos().
todo!() trace!("Rendering requested");
} // Prepare with current configuration (which includes the current window size)
Key::Unidentified(_) => {
todo!() let output = self.surface.get_current_texture()?;
} let view = output
.texture
.create_view(&TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&CommandEncoderDescriptor {
label: Some("Render Encoder"),
});
{
let mut _render_pass = encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
trace!("Rendering...");
} }
self.window.request_redraw();
self.queue.submit(Some(encoder.finish()));
output.present();
Ok(())
} }
} }

View File

@@ -1,346 +0,0 @@
use glyphon::{
Buffer, BufferLine, Cache, Color, Cursor, FontSystem, Metrics, Resolution, Shaping, SwashCache,
TextArea, TextAtlas, TextBounds, TextRenderer, Viewport, Wrap,
};
use wgpu::{Device, MultisampleState, Queue, RenderPass, SurfaceConfiguration, TextureFormat};
pub struct TerminalText {
font_system: FontSystem,
cache: SwashCache,
viewport: Viewport,
atlas: TextAtlas,
buffer: Buffer,
renderer: TextRenderer,
dirty_regions: Vec<TextBounds>,
cursor: Cursor,
}
use log::{debug, trace};
impl TerminalText {
pub fn new(device: &Device, queue: &Queue, surface_format: TextureFormat) -> TerminalText {
debug!("Creating a new TerminalText object");
let mut font_system = FontSystem::new();
let swash_cache = SwashCache::new();
let cache = Cache::new(device);
let viewport = Viewport::new(device, &cache);
let mut atlas = TextAtlas::new(device, queue, &cache, surface_format);
let renderer = TextRenderer::new(&mut atlas, device, MultisampleState::default(), None);
let mut buffer = Buffer::new(&mut font_system, Metrics::relative(13.0, 1.25));
buffer.set_wrap(&mut font_system, Wrap::Glyph);
trace!("Loading system fonts");
// Add a default font
font_system.db_mut().load_system_fonts();
Self {
font_system,
cache: swash_cache,
viewport,
atlas,
buffer,
renderer,
dirty_regions: vec![],
cursor: Cursor {
line: 0,
index: 0,
affinity: glyphon::Affinity::After,
},
}
}
pub fn resize(&mut self, width: u32, height: u32) {
trace!("Resizing window - Width: {width} Height: {height}");
self.buffer.set_size(
&mut self.font_system,
Some(width as f32),
Some(height as f32),
);
// Update the buffer's wrapping based on the new width
self.buffer.set_wrap(&mut self.font_system, Wrap::Glyph);
// Reshape the text with the new dimensions
self.buffer.shape_until_scroll(&mut self.font_system, false);
self.ensure_visible_text_rendered();
}
fn merge_dirty_regions(&self) -> TextBounds {
TextBounds {
left: self
.dirty_regions
.iter()
.min_by_key(|b| b.left)
.unwrap()
.left,
top: self.dirty_regions.iter().min_by_key(|b| b.top).unwrap().top,
right: self
.dirty_regions
.iter()
.max_by_key(|b| b.right)
.unwrap()
.right,
bottom: self
.dirty_regions
.iter()
.max_by_key(|b| b.bottom)
.unwrap()
.bottom,
}
}
pub fn prepare(&mut self, device: &Device, queue: &Queue, config: &SurfaceConfiguration) {
// Update viewport with new dimensions
self.viewport.update(
queue,
Resolution {
width: config.width,
height: config.height,
},
);
if !self.dirty_regions.is_empty() {
let region = self.merge_dirty_regions();
trace!("Preparing region {:?}", region);
self.renderer
.prepare(
device,
queue,
&mut self.font_system,
&mut self.atlas,
&self.viewport,
[TextArea {
buffer: &self.buffer,
left: 0.0,
top: 0.0,
scale: 1.0,
bounds: region,
default_color: Color::rgb(255, 255, 255),
custom_glyphs: &[],
}],
&mut self.cache,
)
.unwrap();
}
self.dirty_regions.clear();
}
pub fn render(&self, render_pass: &mut RenderPass) {
self.renderer
.render(&self.atlas, &self.viewport, render_pass)
.unwrap();
}
pub fn trim(&mut self) {
self.atlas.trim()
}
pub fn insert_char(&mut self, c: char) {
let line = self.cursor.line;
if line >= self.buffer.lines.len() {
// TODO
return;
}
let mut text = self.buffer.lines[line].text().to_string().clone();
let pos = self.cursor.index.min(text.len());
text.insert(pos, c);
let ending = self.buffer.lines[line].ending();
let attrs = self.buffer.lines[line].attrs_list().clone();
// Update the buffer with the new text
self.buffer.lines[line].set_text(&text, ending, attrs);
// Update cursor position
self.cursor.index = pos + 1;
self.buffer.shape_until_scroll(&mut self.font_system, false);
self.ensure_visible_text_rendered();
}
pub fn handle_enter(&mut self) {
let line = self.cursor.line;
// Ensure line is within bounds
if line >= self.buffer.lines.len() {
return;
}
// Get the current line text
let current_line = self.buffer.lines[line].text().to_string();
let pos = self.cursor.index.min(current_line.len());
// Split the line at cursor position
let text_before = &current_line[..pos];
let text_after = &current_line[pos..];
// Update the current line with text before cursor
let ending = self.buffer.lines[line].ending();
let attrs = self.buffer.lines[line].attrs_list().clone();
self.buffer.lines[line].set_text(text_before, ending, attrs.clone());
// Create a new line with text after cursor
let new_line = BufferLine::new(text_after, ending, attrs, Shaping::Advanced);
self.buffer.lines.insert(line + 1, new_line);
// Update cursor position to beginning of next line
self.cursor.line = line + 1;
self.cursor.index = 0;
// Reshape the buffer
self.buffer.shape_until_scroll(&mut self.font_system, false);
self.ensure_visible_text_rendered();
}
fn get_line_bounds(&self, line: usize) -> TextBounds {
// Calculate the bounds for a specific line
if line >= self.buffer.lines.len() {
// Return default bounds for invalid line
return TextBounds {
left: 0,
top: 0,
right: 0,
bottom: 0,
};
}
let line_height = self.buffer.metrics().line_height;
let viewport_width = self.viewport.resolution().width;
// Use layout_runs to get information about wrapped lines
let layout_iter = self.buffer.layout_runs();
let mut top_line = 0;
let mut bottom_line = 0;
let mut found_line = false;
// Iterate through layout runs to find our target line
for (visual_line_count, run) in layout_iter.enumerate() {
if run.line_i == line {
// This run belongs to our target line
if !found_line {
// First run of our target line
top_line = visual_line_count;
found_line = true;
}
// Update bottom line for each run we find for this line
bottom_line = visual_line_count + 1;
} else if found_line {
// We've processed all runs for our target line
break;
}
}
// Calculate bounds based on visual line positions
let top_f32 = (top_line as f32 * line_height).floor();
let bottom_f32 = (bottom_line as f32 * line_height).ceil();
// Safe conversions with overflow checks
let top = if top_f32 > i32::MAX as f32 {
i32::MAX
} else if top_f32 < i32::MIN as f32 {
i32::MIN
} else {
top_f32 as i32
};
let bottom = if bottom_f32 > i32::MAX as f32 {
i32::MAX
} else if bottom_f32 < i32::MIN as f32 {
i32::MIN
} else {
bottom_f32 as i32
};
// Ensure viewport width doesn't exceed i32::MAX
let right = viewport_width.min(i32::MAX as u32) as i32;
TextBounds {
left: 0,
top,
right,
bottom,
}
}
fn get_visible_line_range(&self) -> (usize, usize) {
let viewport_height = self.viewport.resolution().height as f32;
let line_height = self.buffer.metrics().line_height;
// Start from line 0 (no scrolling yet)
let start_line = 0;
// Calculate visible lines based on wrapped text
let layout_iter = self.buffer.layout_runs();
let mut last_logical_line = 0;
// Count how many visual lines we have and map to logical lines
for (visual_line_count, run) in layout_iter.enumerate() {
last_logical_line = run.line_i;
// If we've exceeded the viewport height, we can stop counting
if ((start_line + visual_line_count) as f32 * line_height) > viewport_height {
break;
}
}
// Add 1 to include partially visible lines at the bottom
let end_line = if self.buffer.lines.is_empty() {
0
} else {
// Make sure we include at least the last logical line that has content in the viewport
(last_logical_line + 1).min(self.buffer.lines.len())
};
trace!("visible line range goes from {start_line} to {end_line}");
(start_line, end_line)
}
pub fn ensure_visible_text_rendered(&mut self) {
let (start_line, end_line) = self.get_visible_line_range();
for i in start_line..end_line {
self.dirty_regions.push(self.get_line_bounds(i));
}
}
// pub fn mark_all_dirty(&mut self) {
// trace!("Marking all regions as dirty");
// // Create a single region covering the entire viewport
// let full_bounds = TextBounds {
// left: 0,
// top: 0,
// right: self.viewport.resolution().width as i32,
// bottom: self.viewport.resolution().height as i32,
// };
// self.dirty_regions = vec![full_bounds];
// }
// pub fn move_cursor_left(&mut self) {
// if self.cursor.index > 0 {
// self.cursor.index -= 1;
// } else if self.cursor.line > 0 {
// // Move to end of previous line
// self.cursor.line -= 1;
// let prev_line_len = self.buffer.lines[self.cursor.line].text().len();
// self.cursor.index = prev_line_len;
// }
// }
// pub fn move_cursor_right(&mut self) {
// if self.cursor.line < self.buffer.lines.len() {
// let current_line_len = self.buffer.lines[self.cursor.line].text().len();
// if self.cursor.index < current_line_len {
// self.cursor.index += 1;
// } else if self.cursor.line < self.buffer.lines.len() - 1 {
// // Move to beginning of next line
// self.cursor.line += 1;
// self.cursor.index = 0;
// }
// }
// }
}

78
src/app/terminal/mod.rs Normal file
View File

@@ -0,0 +1,78 @@
use vte::{Parser, Perform};
// A struct to represent a single cell on the screen
#[derive(Clone, Debug, Default)]
pub struct Cell {
pub char: char,
// pub fg_color: Color,
// pub bg_color: Color,
// pub flags: StyleFlags,
}
pub struct CellGrid(Vec<Vec<Cell>>);
impl CellGrid {
pub fn new(width: usize, height: usize) -> Self {
CellGrid(vec![vec![Cell::default(); width]; height])
}
}
pub struct Terminal {
grid: CellGrid,
cursor_x: usize,
cursor_y: usize,
}
impl Terminal {
pub fn new() -> Self {
let width = 80;
let height = 24;
Terminal {
grid: CellGrid::new(width, height),
cursor_x: 0,
cursor_y: 0,
}
}
pub fn get_grid(&self) -> &CellGrid {
&self.grid
}
}
impl Perform for Terminal {
// this method will update the CellGrid and the cursor position
fn print(&mut self, c: char) {
// todo!()
()
}
/// This method is called when an "execute" control character is seen.
/// These are single-byte control codes like newline, backspace, etc.
fn execute(&mut self, byte: u8) {
match byte {
b'\n' => {
// Newline
println!("Execute: Newline");
self.cursor_y += 1;
self.cursor_x = 0; // Carriage return is implicit with newline
}
_ => {
println!("Execute: Unhandled control character: 0x{:02x}", byte);
}
}
}
/// This method is called for "Control Sequence Introducer" (CSI) sequences.
/// These are the complex `ESC [ ...` sequences for moving the cursor,
/// changing colors, clearing the screen, etc.
fn csi_dispatch(
&mut self,
params: &vte::Params,
_intermediates: &[u8],
_ignore: bool,
action: char,
) {
//todo!()
()
}
}

View File

@@ -1,4 +1,4 @@
use log::info; use log::{debug, info};
use winit::event_loop::EventLoop; use winit::event_loop::EventLoop;
mod app; mod app;
@@ -10,9 +10,17 @@ fn main() {
// Log platform information // Log platform information
info!("Starting simplicitty terminal emulator"); info!("Starting simplicitty terminal emulator");
// Create event loop with explicit backend preference debug!("Creating winit event loop");
let event_loop = EventLoop::new().unwrap(); let event_loop = EventLoop::<app::UserEvent>::with_user_event()
.build()
.unwrap();
// Create event loop proxy to send pty output events to the window
let event_loop_proxy = event_loop.create_proxy();
debug!("Starting winit event loop");
// Run the application // Run the application
event_loop.run_app(&mut app::Application::new()).unwrap(); event_loop
.run_app(&mut app::Application::new(event_loop_proxy))
.unwrap();
} }