summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-11-17 22:42:51 -0500
committerJesse Luehrs <doy@tozt.net>2021-11-17 22:42:51 -0500
commit7f65b6ae852ea4297fcddcd2e33965c2ddeab063 (patch)
treeca8f55ce390cb03beb0dbd7c107c46542b9da9ac /src
parent4025a5fa46ec24902ee1fb2d6764aed872a3d5b7 (diff)
downloadnbsh-7f65b6ae852ea4297fcddcd2e33965c2ddeab063.tar.gz
nbsh-7f65b6ae852ea4297fcddcd2e33965c2ddeab063.zip
reorder some functions
Diffstat (limited to 'src')
-rw-r--r--src/history.rs170
-rw-r--r--src/readline.rs8
-rw-r--r--src/state.rs154
3 files changed, 166 insertions, 166 deletions
diff --git a/src/history.rs b/src/history.rs
index 4421e20..e740e41 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -17,6 +17,65 @@ impl History {
}
}
+ pub async fn handle_key(&self, key: textmode::Key, idx: usize) {
+ let entry = self.entries[idx].lock_arc().await;
+ if entry.running() {
+ entry.input.send(key.into_bytes()).await.unwrap();
+ }
+ }
+
+ pub async fn render(
+ &self,
+ out: &mut textmode::Output,
+ repl_lines: usize,
+ focus: Option<usize>,
+ ) -> anyhow::Result<()> {
+ if let Some(idx) = focus {
+ let mut entry = self.entries[idx].lock_arc().await;
+ if entry.should_fullscreen() {
+ entry.render_fullscreen(out);
+ return Ok(());
+ }
+ }
+
+ let mut used_lines = repl_lines;
+ let mut pos = None;
+ for (idx, entry) in self.entries.iter().enumerate().rev() {
+ let mut entry = entry.lock_arc().await;
+ let focused = focus.map_or(false, |focus| idx == focus);
+ let last_row = entry.lines(self.size.1, focused);
+ used_lines += 1 + std::cmp::min(6, last_row);
+ if used_lines > self.size.0 as usize {
+ break;
+ }
+ if focused && used_lines == 1 && entry.running() {
+ used_lines = 2;
+ }
+ out.move_to(
+ (self.size.0 as usize - used_lines).try_into().unwrap(),
+ 0,
+ );
+ entry.render(out, self.size.1, focused);
+ if focused {
+ pos = Some(out.screen().cursor_position());
+ }
+ }
+ if let Some(pos) = pos {
+ out.move_to(pos.0, pos.1);
+ }
+ Ok(())
+ }
+
+ pub async fn resize(&mut self, size: (u16, u16)) {
+ self.size = size;
+ for entry in &self.entries {
+ let entry = entry.lock_arc().await;
+ if entry.running() {
+ entry.resize.send(size).await.unwrap();
+ }
+ }
+ }
+
pub async fn run(
&mut self,
cmd: &str,
@@ -121,65 +180,6 @@ impl History {
Ok(self.entries.len() - 1)
}
- pub async fn handle_key(&self, key: textmode::Key, idx: usize) {
- let entry = self.entries[idx].lock_arc().await;
- if entry.running() {
- entry.input.send(key.into_bytes()).await.unwrap();
- }
- }
-
- pub async fn render(
- &self,
- out: &mut textmode::Output,
- repl_lines: usize,
- focus: Option<usize>,
- ) -> anyhow::Result<()> {
- if let Some(idx) = focus {
- let mut entry = self.entries[idx].lock_arc().await;
- if entry.should_fullscreen() {
- entry.render_fullscreen(out);
- return Ok(());
- }
- }
-
- let mut used_lines = repl_lines;
- let mut pos = None;
- for (idx, entry) in self.entries.iter().enumerate().rev() {
- let mut entry = entry.lock_arc().await;
- let focused = focus.map_or(false, |focus| idx == focus);
- let last_row = entry.lines(self.size.1, focused);
- used_lines += 1 + std::cmp::min(6, last_row);
- if used_lines > self.size.0 as usize {
- break;
- }
- if focused && used_lines == 1 && entry.running() {
- used_lines = 2;
- }
- out.move_to(
- (self.size.0 as usize - used_lines).try_into().unwrap(),
- 0,
- );
- entry.render(out, self.size.1, focused);
- if focused {
- pos = Some(out.screen().cursor_position());
- }
- }
- if let Some(pos) = pos {
- out.move_to(pos.0, pos.1);
- }
- Ok(())
- }
-
- pub async fn resize(&mut self, size: (u16, u16)) {
- self.size = size;
- for entry in &self.entries {
- let entry = entry.lock_arc().await;
- if entry.running() {
- entry.resize.send(size).await.unwrap();
- }
- }
- }
-
pub async fn toggle_fullscreen(&mut self, idx: usize) {
self.entries[idx].lock_arc().await.toggle_fullscreen();
}
@@ -227,27 +227,6 @@ impl HistoryEntry {
}
}
- fn running(&self) -> bool {
- self.exit_info.is_none()
- }
-
- fn lines(&self, width: u16, focused: bool) -> usize {
- let screen = self.vt.screen();
- let mut last_row = 0;
- for (idx, row) in screen.rows(0, width).enumerate() {
- if !row.is_empty() {
- last_row = idx + 1;
- }
- }
- if focused && self.running() {
- last_row = std::cmp::max(
- last_row,
- screen.cursor_position().0 as usize + 1,
- );
- }
- last_row
- }
-
fn render(
&mut self,
out: &mut textmode::Output,
@@ -344,11 +323,6 @@ impl HistoryEntry {
out.reset_attributes();
}
- fn should_fullscreen(&self) -> bool {
- self.fullscreen
- .unwrap_or_else(|| self.vt.screen().alternate_screen())
- }
-
fn render_fullscreen(&mut self, out: &mut textmode::Output) {
let screen = self.vt.screen();
let new_audible_bell_state = screen.audible_bell_count();
@@ -376,6 +350,32 @@ impl HistoryEntry {
self.fullscreen = Some(!self.vt.screen().alternate_screen());
}
}
+
+ fn running(&self) -> bool {
+ self.exit_info.is_none()
+ }
+
+ fn lines(&self, width: u16, focused: bool) -> usize {
+ let screen = self.vt.screen();
+ let mut last_row = 0;
+ for (idx, row) in screen.rows(0, width).enumerate() {
+ if !row.is_empty() {
+ last_row = idx + 1;
+ }
+ }
+ if focused && self.running() {
+ last_row = std::cmp::max(
+ last_row,
+ screen.cursor_position().0 as usize + 1,
+ );
+ }
+ last_row
+ }
+
+ fn should_fullscreen(&self) -> bool {
+ self.fullscreen
+ .unwrap_or_else(|| self.vt.screen().alternate_screen())
+ }
}
#[derive(Copy, Clone)]
diff --git a/src/readline.rs b/src/readline.rs
index 529c102..365f5ec 100644
--- a/src/readline.rs
+++ b/src/readline.rs
@@ -48,10 +48,6 @@ impl Readline {
Some(crate::action::Action::Render)
}
- pub fn lines(&self) -> usize {
- 2 // XXX handle wrapping
- }
-
pub async fn render(
&self,
out: &mut textmode::Output,
@@ -112,6 +108,10 @@ impl Readline {
self.size = size;
}
+ pub fn lines(&self) -> usize {
+ 2 // XXX handle wrapping
+ }
+
fn input(&self) -> String {
self.input_line.clone()
}
diff --git a/src/state.rs b/src/state.rs
index 6f17e95..8b08fe4 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -22,6 +22,83 @@ impl State {
}
}
+ pub async fn handle_key(
+ &mut self,
+ key: textmode::Key,
+ ) -> Option<crate::action::Action> {
+ if self.escape {
+ self.escape = false;
+ let mut fallthrough = false;
+ match key {
+ textmode::Key::Ctrl(b'e') => {
+ fallthrough = true;
+ }
+ textmode::Key::Ctrl(b'l') => {
+ return Some(crate::action::Action::ForceRedraw);
+ }
+ textmode::Key::Char('f') => {
+ if let Focus::History(idx) = self.focus {
+ return Some(
+ crate::action::Action::ToggleFullscreen(idx),
+ );
+ }
+ }
+ textmode::Key::Char('j') => {
+ let new_focus = match self.focus {
+ Focus::History(idx) => {
+ if idx >= self.history.entry_count() - 1 {
+ Focus::Readline
+ } else {
+ Focus::History(idx + 1)
+ }
+ }
+ Focus::Readline => Focus::Readline,
+ };
+ return Some(crate::action::Action::UpdateFocus(
+ new_focus,
+ ));
+ }
+ textmode::Key::Char('k') => {
+ let new_focus = match self.focus {
+ Focus::History(idx) => {
+ if idx == 0 {
+ Focus::History(0)
+ } else {
+ Focus::History(idx - 1)
+ }
+ }
+ Focus::Readline => {
+ Focus::History(self.history.entry_count() - 1)
+ }
+ };
+ return Some(crate::action::Action::UpdateFocus(
+ new_focus,
+ ));
+ }
+ textmode::Key::Char('r') => {
+ return Some(crate::action::Action::UpdateFocus(
+ Focus::Readline,
+ ));
+ }
+ _ => {}
+ }
+ if !fallthrough {
+ return None;
+ }
+ } else if key == textmode::Key::Ctrl(b'e') {
+ self.escape = true;
+ return None;
+ }
+
+ match self.focus {
+ Focus::Readline => self.readline.handle_key(key).await,
+ Focus::History(idx) => {
+ self.history.handle_key(key, idx).await;
+ None
+ }
+ }
+ }
+
pub async fn render(
&self,
out: &mut textmode::Output,
@@ -99,83 +176,6 @@ impl State {
}
}
}
-
- pub async fn handle_key(
- &mut self,
- key: textmode::Key,
- ) -> Option<crate::action::Action> {
- if self.escape {
- self.escape = false;
- let mut fallthrough = false;
- match key {
- textmode::Key::Ctrl(b'e') => {
- fallthrough = true;
- }
- textmode::Key::Ctrl(b'l') => {
- return Some(crate::action::Action::ForceRedraw);
- }
- textmode::Key::Char('f') => {
- if let Focus::History(idx) = self.focus {
- return Some(
- crate::action::Action::ToggleFullscreen(idx),
- );
- }
- }
- textmode::Key::Char('j') => {
- let new_focus = match self.focus {
- Focus::History(idx) => {
- if idx >= self.history.entry_count() - 1 {
- Focus::Readline
- } else {
- Focus::History(idx + 1)
- }
- }
- Focus::Readline => Focus::Readline,
- };
- return Some(crate::action::Action::UpdateFocus(
- new_focus,
- ));
- }
- textmode::Key::Char('k') => {
- let new_focus = match self.focus {
- Focus::History(idx) => {
- if idx == 0 {
- Focus::History(0)
- } else {
- Focus::History(idx - 1)
- }
- }
- Focus::Readline => {
- Focus::History(self.history.entry_count() - 1)
- }
- };
- return Some(crate::action::Action::UpdateFocus(
- new_focus,
- ));
- }
- textmode::Key::Char('r') => {
- return Some(crate::action::Action::UpdateFocus(
- Focus::Readline,
- ));
- }
- _ => {}
- }
- if !fallthrough {
- return None;
- }
- } else if key == textmode::Key::Ctrl(b'e') {
- self.escape = true;
- return None;
- }
-
- match self.focus {
- Focus::Readline => self.readline.handle_key(key).await,
- Focus::History(idx) => {
- self.history.handle_key(key, idx).await;
- None
- }
- }
- }
}
#[derive(Copy, Clone, Debug)]