summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs104
1 files changed, 57 insertions, 47 deletions
diff --git a/src/main.rs b/src/main.rs
index df6e45a..a0f78bc 100644
--- a/src/main.rs
+++ b/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};
@@ -57,20 +57,13 @@ impl Model {
fn new(cell: Rect, width: usize, height: usize, scale: f32) -> Self {
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
+ 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: 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();
@@ -205,17 +206,21 @@ fn main() {
if let Some(c) = model.buffer.get(col, row) {
Mask::new(font.get(&c).map_or("", |v| v)) // get svg
.transform(Some(
- Transform::scale(model.scale(), -model.scale()) // scale it
+ Transform::scale(model.scale(), -model.scale()) // scale it
.then_translate(
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,32 +413,32 @@ 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![
- '\'', '`', '\\', ',', '=', '[', '-', '.', ']', ';', '/',
- ')', '!', '@', '#', '$', '%', '^', '&', '*', '(', '"', '~', '|', '<', '+', '{', '_', '>', '}', ':', '?'
- ]
- .into_iter()
- .chain('a'..='z')
- .chain('A'..='Z')
- .chain('0'..='9');
+ let chars = vec![
+ '\'', '`', '\\', ',', '=', '[', '-', '.', ']', ';', '/', ')', '!', '@', '#', '$', '%', '^',
+ '&', '*', '(', '"', '~', '|', '<', '+', '{', '_', '>', '}', ':', '?',
+ ]
+ .into_iter()
+ .chain('a'..='z')
+ .chain('A'..='Z')
+ .chain('0'..='9');
let mut hm = HashMap::new();
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);
}