Skip to content

Commit

Permalink
Handle tab character when rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
huytd committed Oct 12, 2021
1 parent 92379ec commit 132e93f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Binary file added _meta/oct-12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/characters.rs
@@ -1,6 +1,7 @@
use winit::event::{ModifiersState, VirtualKeyCode::{self, *}};

pub const NEWLINE_CHAR: char = 10 as char;
pub const SPACE_CHAR: char = 32 as char;
pub const TAB_CHAR: char = 9 as char;
pub const BACK_CHAR: char = 8 as char;
pub const ESC_CHAR: char = 27 as char;
Expand Down
17 changes: 14 additions & 3 deletions src/terminal.rs
Expand Up @@ -4,8 +4,11 @@ use wgpu::{Backends, Color, CommandEncoderDescriptor, Device, DeviceDescriptor,
use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section, Text, ab_glyph::{self, Rect}};
use winit::{dpi::PhysicalSize, window::Window};

use crate::{characters::{BACK_CHAR, BELL_CHAR, CR_CHAR, ESC_CHAR, NEWLINE_CHAR}, constants::{TERMINAL_COLS, TERMINAL_ROWS, TITLEBAR_MARGIN}, cursor::Cursor};
use crate::{characters::{BACK_CHAR, BELL_CHAR, CR_CHAR, ESC_CHAR, NEWLINE_CHAR, SPACE_CHAR, TAB_CHAR}, constants::{TERMINAL_COLS, TERMINAL_ROWS, TITLEBAR_MARGIN}, cursor::Cursor};

// REF: https://www.vt100.net/docs/la100-rm/chapter2.html

const TAB_STOP: usize = 8;
const FONT_SIZE: f32 = 20.0;
const BG_CHAR: &str = "█";
const BGR_COLOR: [f32; 4] = [0.02, 0.02, 0.02, 1.0];
Expand Down Expand Up @@ -146,15 +149,23 @@ impl Terminal {
// everything on the same line has the same fg and bg.
// Fix this after we have the color parser.

let lines = (&self.buffer).split(|c| *c == 10);
let lines = (&self.buffer).split(|c| *c == NEWLINE_CHAR as u8);
let mut display_lines: Vec<Vec<u8>> = Vec::with_capacity(TERMINAL_ROWS as usize);
for line in lines {
let mut line_buffer: Vec<u8> = Vec::with_capacity(TERMINAL_COLS as usize);
for chr in line {
if line_buffer.len() >= TERMINAL_COLS as usize {
line_buffer.clear();
}
line_buffer.push(*chr);
if *chr == TAB_CHAR as u8 {
let cur = line_buffer.len();
let next_pos = (1 + cur / TAB_STOP) * TAB_STOP;
for _ in 0..(next_pos - cur) {
line_buffer.push(SPACE_CHAR as u8);
}
} else {
line_buffer.push(*chr);
}
}
if display_lines.len() >= TERMINAL_ROWS as usize {
display_lines.remove(0);
Expand Down

0 comments on commit 132e93f

Please sign in to comment.