cargo fmt
This commit is contained in:
86
src/main.rs
86
src/main.rs
@@ -1,13 +1,13 @@
|
||||
use nix::fcntl::{fcntl, F_GETFL, F_SETFL, OFlag};
|
||||
use nix::pty::{ForkptyResult, forkpty, PtyMaster};
|
||||
use nix::fcntl::{fcntl, OFlag, F_GETFL, F_SETFL};
|
||||
use nix::pty::{forkpty, ForkptyResult, PtyMaster};
|
||||
use nix::unistd::{execv, read, write};
|
||||
|
||||
use minifb::{Key, KeyRepeat, Window, WindowOptions};
|
||||
|
||||
use std::vec;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::CString;
|
||||
use std::fmt::Write;
|
||||
use std::vec;
|
||||
|
||||
use ttf_parser::{Face, OutlineBuilder, Rect};
|
||||
|
||||
@@ -59,18 +59,11 @@ impl Model {
|
||||
screenbuffer: Buffer::new(
|
||||
0,
|
||||
(cell.width() as f32 * width as f32 * scale) as usize,
|
||||
(cell.height() as f32 * height as f32 * scale) as usize
|
||||
),
|
||||
buffer: Buffer::new(
|
||||
None,
|
||||
width,
|
||||
height
|
||||
(cell.height() as f32 * height as f32 * scale) as usize,
|
||||
),
|
||||
buffer: Buffer::new(None, width, height),
|
||||
cell: cell,
|
||||
cursor: Cursor {
|
||||
col: 0,
|
||||
row: 0,
|
||||
},
|
||||
cursor: Cursor { col: 0, row: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +111,10 @@ impl Perform for Model {
|
||||
|
||||
// dispatch an operating system command
|
||||
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
|
||||
@@ -186,11 +182,16 @@ fn main() {
|
||||
model.screenbuffer.width,
|
||||
model.screenbuffer.height,
|
||||
WindowOptions::default(),
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
window.set_target_fps(60);
|
||||
|
||||
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();
|
||||
|
||||
@@ -210,12 +211,16 @@ fn main() {
|
||||
col as f32 * model.cell.width() as f32 * model.scale(),
|
||||
// shift right by the cell width * the scale
|
||||
(1 + row) as f32 * model.scale() * model.cell.height() as f32,
|
||||
)
|
||||
),
|
||||
))
|
||||
.size(model.screenbuffer.width as u32, model.screenbuffer.height as u32)
|
||||
.render_into(&mut mask, None); }
|
||||
};
|
||||
};
|
||||
.size(
|
||||
model.screenbuffer.width as u32,
|
||||
model.screenbuffer.height as u32,
|
||||
)
|
||||
.render_into(&mut mask, None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// render in white/grayscale to screen
|
||||
for (p, m) in model.screenbuffer.buffer.iter_mut().zip(mask.iter()) {
|
||||
@@ -224,8 +229,13 @@ fn main() {
|
||||
}
|
||||
|
||||
// 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
|
||||
match read(&pty, &mut buf) {
|
||||
@@ -236,7 +246,8 @@ fn main() {
|
||||
|
||||
let keys = window.get_keys_pressed(KeyRepeat::No);
|
||||
if !keys.is_empty() {
|
||||
let bytes: Vec<u8> = keys.iter()
|
||||
let bytes: Vec<u8> = keys
|
||||
.iter()
|
||||
// TODO apply modifiers
|
||||
.map(|key| key_to_u8(*key, false, false))
|
||||
.collect();
|
||||
@@ -248,14 +259,13 @@ fn main() {
|
||||
// forks a new pty and returns file descriptor of the master
|
||||
fn spawn_pty(shell: &str) -> Option<PtyMaster> {
|
||||
// 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 {
|
||||
ForkptyResult::Parent {child:_, master} => {
|
||||
|
||||
ForkptyResult::Parent { child: _, master } => {
|
||||
// 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(), &[]);
|
||||
return None;
|
||||
}
|
||||
@@ -403,21 +413,20 @@ fn key_to_u8(key: Key, shift: bool, ctrl: bool) -> u8 {
|
||||
b'/' => b'?',
|
||||
_ => base,
|
||||
}
|
||||
} else { base };
|
||||
} else {
|
||||
base
|
||||
};
|
||||
|
||||
let base_shift_ctrl = if ctrl {
|
||||
base_shift & 0x1F
|
||||
} else { base_shift };
|
||||
let base_shift_ctrl = if ctrl { base_shift & 0x1F } else { base_shift };
|
||||
|
||||
return base_shift_ctrl;
|
||||
}
|
||||
|
||||
// creats a mapping from a `char` to its svg spec for a selection of characters
|
||||
fn generate_font(face: &Face) -> HashMap<char, String> {
|
||||
let chars =
|
||||
vec![
|
||||
'\'', '`', '\\', ',', '=', '[', '-', '.', ']', ';', '/',
|
||||
')', '!', '@', '#', '$', '%', '^', '&', '*', '(', '"', '~', '|', '<', '+', '{', '_', '>', '}', ':', '?'
|
||||
let chars = vec![
|
||||
'\'', '`', '\\', ',', '=', '[', '-', '.', ']', ';', '/', ')', '!', '@', '#', '$', '%', '^',
|
||||
'&', '*', '(', '"', '~', '|', '<', '+', '{', '_', '>', '}', ':', '?',
|
||||
]
|
||||
.into_iter()
|
||||
.chain('a'..='z')
|
||||
@@ -428,7 +437,8 @@ fn generate_font(face: &Face) -> HashMap<char, String> {
|
||||
|
||||
for c in chars {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user