Skip to content

Commit b029c60

Browse files
alexs-shmi2ebi
authored andcommitted
Use the default terminal size when runtime detection fails
This commit adds a fallback terminal size to use when runtime detection fails. Since calling `size` from `Terminal` will never fail now, it also changes the function’s signature. If the client code wants to know whether the call succeeded or failed, it’s still possible to call `size` via `Backend`.
1 parent fb690ac commit b029c60

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

helix-term/src/application.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Application {
125125

126126
let theme_mode = backend.get_theme_mode();
127127
let terminal = Terminal::new(backend)?;
128-
let area = terminal.size().expect("couldn't get terminal size");
128+
let area = terminal.size();
129129
let mut compositor = Compositor::new(area);
130130
let config = Arc::new(ArcSwap::from_pointee(config));
131131
let handlers = handlers::setup(config.clone());
@@ -542,7 +542,7 @@ impl Application {
542542
}
543543

544544
// redraw the terminal
545-
let area = self.terminal.size().expect("couldn't get terminal size");
545+
let area = self.terminal.size();
546546
self.compositor.resize(area);
547547
self.terminal.clear().expect("couldn't clear terminal");
548548

@@ -700,7 +700,7 @@ impl Application {
700700
.resize(Rect::new(0, 0, cols, rows))
701701
.expect("Unable to resize terminal");
702702

703-
let area = self.terminal.size().expect("couldn't get terminal size");
703+
let area = self.terminal.size();
704704

705705
self.compositor.resize(area);
706706

@@ -729,7 +729,7 @@ impl Application {
729729
.resize(Rect::new(0, 0, width, height))
730730
.expect("Unable to resize terminal");
731731

732-
let area = self.terminal.size().expect("couldn't get terminal size");
732+
let area = self.terminal.size();
733733

734734
self.compositor.resize(area);
735735

helix-tui/src/terminal.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,22 @@ where
7373
viewport: Viewport,
7474
}
7575

76+
/// Default terminal size: 80 columns, 24 lines
77+
pub const DEFAULT_TERMINAL_SIZE: Rect = Rect {
78+
x: 0,
79+
y: 0,
80+
width: 80,
81+
height: 24,
82+
};
83+
7684
impl<B> Terminal<B>
7785
where
7886
B: Backend,
7987
{
8088
/// Wrapper around Terminal initialization. Each buffer is initialized with a blank string and
8189
/// default colors for the foreground and the background
8290
pub fn new(backend: B) -> io::Result<Terminal<B>> {
83-
let size = backend.size()?;
91+
let size = backend.size().unwrap_or(DEFAULT_TERMINAL_SIZE);
8492
Terminal::with_options(
8593
backend,
8694
TerminalOptions {
@@ -159,7 +167,7 @@ where
159167

160168
/// Queries the backend for size and resizes if it doesn't match the previous size.
161169
pub fn autoresize(&mut self) -> io::Result<Rect> {
162-
let size = self.size()?;
170+
let size = self.size();
163171
if size != self.viewport.area {
164172
self.resize(size)?;
165173
};
@@ -235,7 +243,7 @@ where
235243
}
236244

237245
/// Queries the real size of the backend.
238-
pub fn size(&self) -> io::Result<Rect> {
239-
self.backend.size()
246+
pub fn size(&self) -> Rect {
247+
self.backend.size().unwrap_or(DEFAULT_TERMINAL_SIZE)
240248
}
241249
}

0 commit comments

Comments
 (0)