nu-explore/ A few fixes. (#7334)

close #7322
close #7323

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2022-12-03 01:12:33 +03:00 committed by GitHub
parent d28624796c
commit e6cf18ea43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 10 deletions

View File

@ -48,4 +48,15 @@ impl UIEvents {
Err(err) => Err(err), Err(err) => Err(err),
} }
} }
pub fn try_next(&self) -> Result<Option<KeyEvent>> {
match poll(Duration::default()) {
Ok(true) => match read()? {
Event::Key(event) => Ok(Some(event)),
_ => Ok(None),
},
Ok(false) => Ok(None),
Err(err) => Err(err),
}
}
} }

View File

@ -11,8 +11,8 @@ pub fn run_nu_command(
cmd: &str, cmd: &str,
current: PipelineData, current: PipelineData,
) -> std::result::Result<PipelineData, ShellError> { ) -> std::result::Result<PipelineData, ShellError> {
let engine_state = engine_state.clone(); let mut engine_state = engine_state.clone();
eval_source2(&engine_state, stack, cmd.as_bytes(), "", current) eval_source2(&mut engine_state, stack, cmd.as_bytes(), "", current)
} }
pub fn is_ignored_command(command: &str) -> bool { pub fn is_ignored_command(command: &str) -> bool {
@ -20,13 +20,13 @@ pub fn is_ignored_command(command: &str) -> bool {
} }
fn eval_source2( fn eval_source2(
engine_state: &EngineState, engine_state: &mut EngineState,
stack: &mut Stack, stack: &mut Stack,
source: &[u8], source: &[u8],
fname: &str, fname: &str,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let (mut block, _) = { let (mut block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let (output, err) = parse( let (output, err) = parse(
&mut working_set, &mut working_set,
@ -35,6 +35,7 @@ fn eval_source2(
false, false,
&[], &[],
); );
if let Some(err) = err { if let Some(err) = err {
return Err(ShellError::IOError(err.to_string())); return Err(ShellError::IOError(err.to_string()));
} }
@ -42,6 +43,11 @@ fn eval_source2(
(output, working_set.render()) (output, working_set.render())
}; };
// We need to merge different info other wise things like PIPEs etc will not work.
if let Err(err) = engine_state.merge_delta(delta) {
return Err(ShellError::IOError(err.to_string()));
}
// eval_block outputs all expressions expept the last to STDOUT; // eval_block outputs all expressions expept the last to STDOUT;
// we don't wont that. // we don't wont that.
// //

View File

@ -432,7 +432,7 @@ fn highlight_search_results(f: &mut Frame, pager: &Pager, layout: &Layout, style
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn handle_events<V>( fn handle_events<V: View>(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
events: &UIEvents, events: &UIEvents,
@ -441,15 +441,64 @@ fn handle_events<V>(
search: &mut SearchBuf, search: &mut SearchBuf,
command: &mut CommandBuf, command: &mut CommandBuf,
mut view: Option<&mut V>, mut view: Option<&mut V>,
) -> (bool, bool) ) -> (bool, bool) {
where
V: View,
{
let key = match events.next() { let key = match events.next() {
Ok(Some(key)) => key, Ok(Some(key)) => key,
_ => return (false, false), _ => return (false, false),
}; };
let result = handle_event(
engine_state,
stack,
layout,
info,
search,
command,
view.as_deref_mut(),
key,
);
if let (true, _) = result {
return result;
}
// Sometimes we get a BIG list of events;
// for example when someone scrolls via a mouse either UP or DOWN.
// This MIGHT causes freeses as we have a 400 delay for a next command read.
//
// To eliminate that we are trying ot read all possible commands which we should action upon.
while let Ok(Some(key)) = events.try_next() {
let result = handle_event(
engine_state,
stack,
layout,
info,
search,
command,
view.as_deref_mut(),
key,
);
if let (true, _) = result {
return result;
}
}
result
}
#[allow(clippy::too_many_arguments)]
fn handle_event<V: View>(
engine_state: &EngineState,
stack: &mut Stack,
layout: &Layout,
info: &mut ViewInfo,
search: &mut SearchBuf,
command: &mut CommandBuf,
mut view: Option<&mut V>,
key: KeyEvent,
) -> (bool, bool) {
if handle_exit_key_event(&key) { if handle_exit_key_event(&key) {
return (true, true); return (true, true);
} }
@ -472,7 +521,6 @@ where
} }
// was not handled so we must check our default controlls // was not handled so we must check our default controlls
handle_general_key_events2(&key, search, command, view, info); handle_general_key_events2(&key, search, command, view, info);
(false, false) (false, false)

View File

@ -84,6 +84,12 @@ impl View for Preview {
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Up => { KeyCode::Up => {
let is_start = self.i_row == 0;
if is_start {
// noop
return Some(Transition::Ok);
}
let page_size = layout.data.len(); let page_size = layout.data.len();
let max = self.lines.len().saturating_sub(page_size); let max = self.lines.len().saturating_sub(page_size);
let was_end = self.i_row == max; let was_end = self.i_row == max;
@ -99,6 +105,13 @@ impl View for Preview {
KeyCode::Down => { KeyCode::Down => {
let page_size = layout.data.len(); let page_size = layout.data.len();
let max = self.lines.len().saturating_sub(page_size); let max = self.lines.len().saturating_sub(page_size);
let is_end = self.i_row == max;
if is_end {
// noop
return Some(Transition::Ok);
}
self.i_row = min(self.i_row + 1, max); self.i_row = min(self.i_row + 1, max);
let is_end = self.i_row == max; let is_end = self.i_row == max;