cargo fmt
This commit is contained in:
104
src/main.rs
104
src/main.rs
@@ -1,13 +1,13 @@
|
|||||||
use nix::fcntl::{fcntl, F_GETFL, F_SETFL, OFlag};
|
use nix::fcntl::{fcntl, OFlag, F_GETFL, F_SETFL};
|
||||||
use nix::pty::{ForkptyResult, forkpty, PtyMaster};
|
use nix::pty::{forkpty, ForkptyResult, PtyMaster};
|
||||||
use nix::unistd::{execv, read, write};
|
use nix::unistd::{execv, read, write};
|
||||||
|
|
||||||
use minifb::{Key, KeyRepeat, Window, WindowOptions};
|
use minifb::{Key, KeyRepeat, Window, WindowOptions};
|
||||||
|
|
||||||
use std::vec;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
use std::vec;
|
||||||
|
|
||||||
use ttf_parser::{Face, OutlineBuilder, Rect};
|
use ttf_parser::{Face, OutlineBuilder, Rect};
|
||||||
|
|
||||||
@@ -57,20 +57,13 @@ impl Model {
|
|||||||
fn new(cell: Rect, width: usize, height: usize, scale: f32) -> Self {
|
fn new(cell: Rect, width: usize, height: usize, scale: f32) -> Self {
|
||||||
Model {
|
Model {
|
||||||
screenbuffer: Buffer::new(
|
screenbuffer: Buffer::new(
|
||||||
0,
|
0,
|
||||||
(cell.width() as f32 * width as f32 * scale) as usize,
|
(cell.width() as f32 * width as f32 * scale) as usize,
|
||||||
(cell.height() as f32 * height as f32 * scale) as usize
|
(cell.height() as f32 * height as f32 * scale) as usize,
|
||||||
),
|
|
||||||
buffer: Buffer::new(
|
|
||||||
None,
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
),
|
),
|
||||||
|
buffer: Buffer::new(None, width, height),
|
||||||
cell: cell,
|
cell: cell,
|
||||||
cursor: Cursor {
|
cursor: Cursor { col: 0, row: 0 },
|
||||||
col: 0,
|
|
||||||
row: 0,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +111,10 @@ impl Perform for Model {
|
|||||||
|
|
||||||
// dispatch an operating system command
|
// dispatch an operating system command
|
||||||
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
|
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
|
||||||
println!("[osc_dispatch] params={:?} bell_terminated={}", params, bell_terminated);
|
println!(
|
||||||
|
"[osc_dispatch] params={:?} bell_terminated={}",
|
||||||
|
params, bell_terminated
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// a final character has arrived for a csi sequence
|
// a final character has arrived for a csi sequence
|
||||||
@@ -186,11 +182,16 @@ fn main() {
|
|||||||
model.screenbuffer.width,
|
model.screenbuffer.width,
|
||||||
model.screenbuffer.height,
|
model.screenbuffer.height,
|
||||||
WindowOptions::default(),
|
WindowOptions::default(),
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
window.set_target_fps(60);
|
window.set_target_fps(60);
|
||||||
|
|
||||||
let pty = spawn_pty(&SHELL).unwrap();
|
let pty = spawn_pty(&SHELL).unwrap();
|
||||||
fcntl(&pty, F_SETFL(OFlag::from_bits_truncate(fcntl(&pty, F_GETFL).unwrap()) | OFlag::O_NONBLOCK)).unwrap();
|
fcntl(
|
||||||
|
&pty,
|
||||||
|
F_SETFL(OFlag::from_bits_truncate(fcntl(&pty, F_GETFL).unwrap()) | OFlag::O_NONBLOCK),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut statemachine = Parser::new();
|
let mut statemachine = Parser::new();
|
||||||
|
|
||||||
@@ -205,17 +206,21 @@ fn main() {
|
|||||||
if let Some(c) = model.buffer.get(col, row) {
|
if let Some(c) = model.buffer.get(col, row) {
|
||||||
Mask::new(font.get(&c).map_or("", |v| v)) // get svg
|
Mask::new(font.get(&c).map_or("", |v| v)) // get svg
|
||||||
.transform(Some(
|
.transform(Some(
|
||||||
Transform::scale(model.scale(), -model.scale()) // scale it
|
Transform::scale(model.scale(), -model.scale()) // scale it
|
||||||
.then_translate(
|
.then_translate(
|
||||||
col as f32 * model.cell.width() as f32 * model.scale(),
|
col as f32 * model.cell.width() as f32 * model.scale(),
|
||||||
// shift right by the cell width * the scale
|
// shift right by the cell width * the scale
|
||||||
(1 + row) as f32 * model.scale() * model.cell.height() as f32,
|
(1 + row) as f32 * model.scale() * model.cell.height() as f32,
|
||||||
)
|
),
|
||||||
))
|
))
|
||||||
.size(model.screenbuffer.width as u32, model.screenbuffer.height as u32)
|
.size(
|
||||||
.render_into(&mut mask, None); }
|
model.screenbuffer.width as u32,
|
||||||
};
|
model.screenbuffer.height as u32,
|
||||||
};
|
)
|
||||||
|
.render_into(&mut mask, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// render in white/grayscale to screen
|
// render in white/grayscale to screen
|
||||||
for (p, m) in model.screenbuffer.buffer.iter_mut().zip(mask.iter()) {
|
for (p, m) in model.screenbuffer.buffer.iter_mut().zip(mask.iter()) {
|
||||||
@@ -224,8 +229,13 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update screen with buffer
|
// update screen with buffer
|
||||||
window.update_with_buffer(&model.screenbuffer.buffer, model.screenbuffer.width, model.screenbuffer.height).unwrap();
|
window
|
||||||
|
.update_with_buffer(
|
||||||
|
&model.screenbuffer.buffer,
|
||||||
|
model.screenbuffer.width,
|
||||||
|
model.screenbuffer.height,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// other stuff
|
// other stuff
|
||||||
match read(&pty, &mut buf) {
|
match read(&pty, &mut buf) {
|
||||||
@@ -236,7 +246,8 @@ fn main() {
|
|||||||
|
|
||||||
let keys = window.get_keys_pressed(KeyRepeat::No);
|
let keys = window.get_keys_pressed(KeyRepeat::No);
|
||||||
if !keys.is_empty() {
|
if !keys.is_empty() {
|
||||||
let bytes: Vec<u8> = keys.iter()
|
let bytes: Vec<u8> = keys
|
||||||
|
.iter()
|
||||||
// TODO apply modifiers
|
// TODO apply modifiers
|
||||||
.map(|key| key_to_u8(*key, false, false))
|
.map(|key| key_to_u8(*key, false, false))
|
||||||
.collect();
|
.collect();
|
||||||
@@ -248,14 +259,13 @@ fn main() {
|
|||||||
// forks a new pty and returns file descriptor of the master
|
// forks a new pty and returns file descriptor of the master
|
||||||
fn spawn_pty(shell: &str) -> Option<PtyMaster> {
|
fn spawn_pty(shell: &str) -> Option<PtyMaster> {
|
||||||
// SAFETY safe unless os out of PTYs; incredibly unlikely
|
// SAFETY safe unless os out of PTYs; incredibly unlikely
|
||||||
match unsafe {forkpty(None, None)} {
|
match unsafe { forkpty(None, None) } {
|
||||||
Ok(fork_pty_res) => match fork_pty_res {
|
Ok(fork_pty_res) => match fork_pty_res {
|
||||||
ForkptyResult::Parent {child:_, master} => {
|
ForkptyResult::Parent { child: _, master } => {
|
||||||
|
|
||||||
// SAFETY `master` is a valid PtyMaster
|
// SAFETY `master` is a valid PtyMaster
|
||||||
return Some(unsafe{PtyMaster::from_owned_fd(master)});
|
return Some(unsafe { PtyMaster::from_owned_fd(master) });
|
||||||
}
|
}
|
||||||
ForkptyResult::Child =>{
|
ForkptyResult::Child => {
|
||||||
let _ = execv::<CString>(&CString::new(shell).unwrap(), &[]);
|
let _ = execv::<CString>(&CString::new(shell).unwrap(), &[]);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -403,32 +413,32 @@ fn key_to_u8(key: Key, shift: bool, ctrl: bool) -> u8 {
|
|||||||
b'/' => b'?',
|
b'/' => b'?',
|
||||||
_ => base,
|
_ => base,
|
||||||
}
|
}
|
||||||
} else { base };
|
} else {
|
||||||
|
base
|
||||||
|
};
|
||||||
|
|
||||||
let base_shift_ctrl = if ctrl {
|
let base_shift_ctrl = if ctrl { base_shift & 0x1F } else { base_shift };
|
||||||
base_shift & 0x1F
|
|
||||||
} else { base_shift };
|
|
||||||
|
|
||||||
return base_shift_ctrl;
|
return base_shift_ctrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// creats a mapping from a `char` to its svg spec for a selection of characters
|
// creats a mapping from a `char` to its svg spec for a selection of characters
|
||||||
fn generate_font(face: &Face) -> HashMap<char, String> {
|
fn generate_font(face: &Face) -> HashMap<char, String> {
|
||||||
let chars =
|
let chars = vec![
|
||||||
vec![
|
'\'', '`', '\\', ',', '=', '[', '-', '.', ']', ';', '/', ')', '!', '@', '#', '$', '%', '^',
|
||||||
'\'', '`', '\\', ',', '=', '[', '-', '.', ']', ';', '/',
|
'&', '*', '(', '"', '~', '|', '<', '+', '{', '_', '>', '}', ':', '?',
|
||||||
')', '!', '@', '#', '$', '%', '^', '&', '*', '(', '"', '~', '|', '<', '+', '{', '_', '>', '}', ':', '?'
|
]
|
||||||
]
|
.into_iter()
|
||||||
.into_iter()
|
.chain('a'..='z')
|
||||||
.chain('a'..='z')
|
.chain('A'..='Z')
|
||||||
.chain('A'..='Z')
|
.chain('0'..='9');
|
||||||
.chain('0'..='9');
|
|
||||||
|
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
|
|
||||||
for c in chars {
|
for c in chars {
|
||||||
let mut builder = Builder(String::new());
|
let mut builder = Builder(String::new());
|
||||||
face.outline_glyph(face.glyph_index(c).unwrap(), &mut builder).unwrap();
|
face.outline_glyph(face.glyph_index(c).unwrap(), &mut builder)
|
||||||
|
.unwrap();
|
||||||
hm.entry(c).insert_entry(builder.0);
|
hm.entry(c).insert_entry(builder.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user