mirror of
https://github.com/nushell/nushell.git
synced 2025-06-20 09:58:15 +02:00
feat adds support for history
This commit is contained in:
parent
bb3759be7f
commit
6fe5851176
@ -2,7 +2,7 @@ use crate::platform::input::legacy_input::LegacyInput;
|
|||||||
use crate::platform::input::reedline_prompt::ReedlinePrompt;
|
use crate::platform::input::reedline_prompt::ReedlinePrompt;
|
||||||
use nu_engine::command_prelude::*;
|
use nu_engine::command_prelude::*;
|
||||||
use nu_protocol::shell_error::io::IoError;
|
use nu_protocol::shell_error::io::IoError;
|
||||||
use reedline::{FileBackedHistory, Reedline, Signal, HISTORY_SIZE};
|
use reedline::{FileBackedHistory, History, HistoryItem, Reedline, Signal, HISTORY_SIZE};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Input;
|
pub struct Input;
|
||||||
@ -48,8 +48,8 @@ impl Command for Input {
|
|||||||
.named(
|
.named(
|
||||||
"history",
|
"history",
|
||||||
SyntaxShape::OneOf(vec![
|
SyntaxShape::OneOf(vec![
|
||||||
SyntaxShape::Filepath,
|
|
||||||
SyntaxShape::List(Box::new(SyntaxShape::String)),
|
SyntaxShape::List(Box::new(SyntaxShape::String)),
|
||||||
|
SyntaxShape::Filepath,
|
||||||
]),
|
]),
|
||||||
"path to a file to read/write history or a list of history entries",
|
"path to a file to read/write history or a list of history entries",
|
||||||
None,
|
None,
|
||||||
@ -57,7 +57,7 @@ impl Command for Input {
|
|||||||
.named(
|
.named(
|
||||||
"max-history",
|
"max-history",
|
||||||
SyntaxShape::Int,
|
SyntaxShape::Int,
|
||||||
"The maximum number of entries to keep in the history",
|
"The maximum number of entries to keep in the history, defaults to reedline {HISTORY_SIZE}",
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.switch("suppress-output", "don't print keystroke values", Some('s'))
|
.switch("suppress-output", "don't print keystroke values", Some('s'))
|
||||||
@ -90,6 +90,10 @@ impl Command for Input {
|
|||||||
let prompt_str: Option<String> = call.opt(engine_state, stack, 0)?;
|
let prompt_str: Option<String> = call.opt(engine_state, stack, 0)?;
|
||||||
let default_val: Option<String> = call.get_flag(engine_state, stack, "default")?;
|
let default_val: Option<String> = call.get_flag(engine_state, stack, "default")?;
|
||||||
let history_val: Option<Value> = call.get_flag(engine_state, stack, "history")?;
|
let history_val: Option<Value> = call.get_flag(engine_state, stack, "history")?;
|
||||||
|
let max_history: usize = call
|
||||||
|
.get_flag::<i64>(engine_state, stack, "max-history")?
|
||||||
|
.map(|l| if l < 0 { 0 } else { l as usize })
|
||||||
|
.unwrap_or(HISTORY_SIZE);
|
||||||
|
|
||||||
let from_io_error = IoError::factory(call.head, None);
|
let from_io_error = IoError::factory(call.head, None);
|
||||||
|
|
||||||
@ -101,10 +105,20 @@ impl Command for Input {
|
|||||||
// Here we will render the default prompt to the right
|
// Here we will render the default prompt to the right
|
||||||
let history = match history_val {
|
let history = match history_val {
|
||||||
None => None,
|
None => None,
|
||||||
Some(Value::String { val, .. }) => Some(Box::new(
|
Some(Value::String { val, .. }) => Some(
|
||||||
FileBackedHistory::with_file(HISTORY_SIZE, val.into())
|
FileBackedHistory::with_file(max_history, val.into())
|
||||||
.expect("Error creating history file"),
|
.expect("Error creating history file"),
|
||||||
)),
|
),
|
||||||
|
Some(Value::List { vals, .. }) => {
|
||||||
|
let mut history =
|
||||||
|
FileBackedHistory::new(max_history).expect("Error creating history file");
|
||||||
|
vals.iter().for_each(|val| {
|
||||||
|
if let Value::String { val, .. } = val {
|
||||||
|
let _ = history.save(HistoryItem::from_command_line(val.clone()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Some(history)
|
||||||
|
}
|
||||||
Some(h) => {
|
Some(h) => {
|
||||||
return Err(ShellError::UnsupportedInput {
|
return Err(ShellError::UnsupportedInput {
|
||||||
msg: "Unsupported history type".to_string(),
|
msg: "Unsupported history type".to_string(),
|
||||||
@ -123,7 +137,7 @@ impl Command for Input {
|
|||||||
let mut line_editor = Reedline::create();
|
let mut line_editor = Reedline::create();
|
||||||
line_editor = line_editor.with_ansi_colors(false);
|
line_editor = line_editor.with_ansi_colors(false);
|
||||||
line_editor = match history {
|
line_editor = match history {
|
||||||
Some(h) => line_editor.with_history(h),
|
Some(h) => line_editor.with_history(Box::new(h)),
|
||||||
None => line_editor,
|
None => line_editor,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user