Skip to content

Commit

Permalink
fix(tty): improve terminal size detection (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored Feb 1, 2023
1 parent 47540ae commit 16cf566
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
libc = "0.2"
unicode-width = "0.1.10"
unicode-segmentation = "1.10.0"
chrono = "0.4.23"

[lib]
name = "carbonyl"
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ This should produce the following outputs:

```console
# Build arm64 Docker image using binaries from the Default target
$ ./scripts/docker.sh arm64 Default
$ ./scripts/docker-build.sh Default arm64
# Build amd64 Docker image using binaries from the Default target
$ ./scripts/docker.sh amd64 Default
$ ./scripts/docker-build.sh Default amd64
```

### Run
Expand Down
9 changes: 8 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ export CARBONYL_ROOT=$(cd $(dirname -- "$0") && dirname -- $(pwd))

source "$CARBONYL_ROOT/scripts/env.sh"

target="$1"
cpu="$2"
platform="linux"
cpu=$(uname -m)

if [ -z "$cpu" ]; then
cpu="$(uname -m)"
fi

if [[ "$cpu" == "arm64" ]]; then
cpu="aarch64"
elif [[ "$cpu" == "amd64" ]]; then
cpu="x86_64"
fi

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
Expand Down
4 changes: 2 additions & 2 deletions scripts/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export CARBONYL_ROOT=$(cd $(dirname -- "$0") && dirname -- $(pwd))

source "$CARBONYL_ROOT/scripts/env.sh"

cpu="$1"
target="$2"
target="$1"
cpu="$2"

build_dir="$CARBONYL_ROOT/build/browser/$cpu"

Expand Down
2 changes: 1 addition & 1 deletion scripts/docker-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ docker manifest create "$tag:$version" \
--amend "$tag:$version-arm64" \
--amend "$tag:$version-amd64"

docker manifest push "$tag:$version"
docker manifest push "$tag:$version" --purge
8 changes: 6 additions & 2 deletions src/browser/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use libc::{c_char, c_int, c_uchar, c_uint, size_t};

use crate::gfx::{Cast, Color, Point, Rect, Size};
use crate::output::Renderer;
use crate::{input, output};
use crate::{input, output, utils::log};

/// This file bridges the C++ code with Rust.
/// "C-unwind" combined with .unwrap() is used to allow catching Rust panics
Expand Down Expand Up @@ -126,6 +126,8 @@ pub extern "C-unwind" fn carbonyl_renderer_create() -> *mut Renderer {
let mut renderer = Box::new(Renderer::new());
let src = output::size().unwrap();

log::debug!("Terminal size: {:?}", src);

renderer.set_size(Size::new(7, 14), src);

Box::into_raw(renderer)
Expand Down Expand Up @@ -197,6 +199,8 @@ pub extern "C-unwind" fn carbonyl_output_get_size(size: *mut CSize) {
let dst = unsafe { &mut *size };
let src = output::size().unwrap().cast::<c_uint>();

log::debug!("Terminal size: {:?}", src);

dst.width = src.width * 7;
dst.height = src.height * 14;
}
Expand Down Expand Up @@ -243,7 +247,7 @@ pub extern "C-unwind" fn carbonyl_input_listen(
mouse_move(col as c_uint * char_width, row as c_uint * char_height)
}
Terminal(terminal) => match terminal {
TerminalEvent::Name(name) => eprintln!("Terminal name: {name}"),
TerminalEvent::Name(name) => log::debug!("Terminal name: {name}"),
TerminalEvent::TrueColorSupported => renderer.enable_true_color(),
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/input/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::BitAnd;

use crate::control_flow;
use crate::{control_flow, utils::log};

use super::{Event, ParseControlFlow};

Expand Down Expand Up @@ -38,7 +38,7 @@ impl Mouse {
(_, None, _) => self.col = num,
(_, _, None) => self.row = num,
_ => {
eprintln!("Misformed mouse sequence");
log::warning!("Misformed mouse sequence");

return None;
}
Expand Down
10 changes: 6 additions & 4 deletions src/input/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::mem::MaybeUninit;
use std::os::fd::RawFd;
use std::os::unix::prelude::AsRawFd;

use crate::utils::log;

pub struct Terminal {
settings: Option<TerminalSettings>,
alt_screen: bool,
Expand All @@ -24,13 +26,13 @@ impl Terminal {
settings: match TerminalSettings::open_raw() {
Ok(settings) => Some(settings),
Err(error) => {
eprintln!("Failed to setup terminal: {error}");
log::error!("Failed to setup terminal: {error}");

None
}
},
alt_screen: if let Err(error) = TTY::enter_alt_screen() {
eprintln!("Failed to enter alternative screen: {error}");
log::error!("Failed to enter alternative screen: {error}");

false
} else {
Expand All @@ -42,15 +44,15 @@ impl Terminal {
pub fn teardown(&mut self) {
if let Some(ref settings) = self.settings {
if let Err(error) = settings.apply() {
eprintln!("Failed to revert terminal settings: {error}");
log::error!("Failed to revert terminal settings: {error}");
}

self.settings = None;
}

if self.alt_screen {
if let Err(error) = TTY::quit_alt_screen() {
eprintln!("Failed to quit alternative screen: {error}");
log::error!("Failed to quit alternative screen: {error}");
}

self.alt_screen = false;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub mod browser;
pub mod gfx;
pub mod input;
pub mod output;

mod utils;
26 changes: 23 additions & 3 deletions src/output/size.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
use core::mem::MaybeUninit;
use std::io;
use std::{io, str::FromStr};

use crate::gfx::Size;
use crate::{gfx::Size, utils::log};

pub fn size() -> io::Result<Size> {
let mut ptr = MaybeUninit::<libc::winsize>::uninit();

unsafe {
if libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ, ptr.as_mut_ptr()) == 0 {
let size = ptr.assume_init();
let mut size = ptr.assume_init();

if size.ws_col == 0 || size.ws_row == 0 {
let cols = parse_var("COLUMNS").unwrap_or(80);
let rows = parse_var("LINES").unwrap_or(24);

log::warning!(
"TIOCGWINSZ returned an empty size ({}x{}), defaulting to {}x{}",
size.ws_col,
size.ws_row,
cols,
rows
);

size.ws_col = cols;
size.ws_row = rows;
}

Ok(Size::new(size.ws_col as u32, size.ws_row as u32))
} else {
Err(io::Error::last_os_error())
}
}
}

fn parse_var<T: FromStr>(var: &str) -> Option<T> {
std::env::var(var).ok()?.parse().ok()
}
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod log;
mod try_block;

use try_block::*;
58 changes: 58 additions & 0 deletions src/utils/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::path::Path;

use chrono::prelude::*;

use crate::utils::try_block;

macro_rules! debug {
($($args:expr),+) => {
crate::utils::log::write(
"DEBUG",
file!(),
line!(),
&format!($($args),*)
)
};
}
macro_rules! warning {
($($args:expr),+) => {
crate::utils::log::write(
"WARNING",
file!(),
line!(),
&format!($($args),*)
)
};
}
macro_rules! error {
($($args:expr),+) => {
crate::utils::log::write(
"ERROR",
file!(),
line!(),
&format!($($args),*)
)
};
}

pub(crate) use debug;
pub(crate) use error;
pub(crate) use warning;

pub fn write(level: &str, file: &str, line: u32, message: &str) {
let date = Utc::now();

eprintln!(
"[{:02}{:02}/{:02}{:02}{:02}.{:06}:{}:{}({})] {}",
date.month(),
date.day(),
date.hour(),
date.minute(),
date.second(),
date.nanosecond() / 1000,
level,
try_block!(Path::new(file).file_name()?.to_str()).unwrap_or("default"),
line,
message
);
}
7 changes: 7 additions & 0 deletions src/utils/try_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro_rules! try_block {
($block:expr) => {
(|| $block)()
};
}

pub(crate) use try_block;

0 comments on commit 16cf566

Please sign in to comment.