mirror of
https://github.com/nushell/nushell.git
synced 2025-01-20 05:09:42 +01:00
f2f4b83886
Configuration in `explore` has always been confusing to me. This PR overhauls (and simplifies, I think) how configuration is done. # Details 1. Configuration is now strongly typed. In `Explore::run()` we create an `ExploreConfig` struct from the more general Nu configuration and arguments to `explore`, then pass that struct to other parts of `explore` that need configuration. IMO this is a lot easier to reason about and trace than the previous approach of creating a `HashMap<String, Value>` and then using that to make various structs elsewhere. 2. We now inherit more configuration from the config used for regular Nu tables 1. Border/line styling now uses the `separator` style used for regular Nu tables, the special `explore.split_line` config point has been retired. 2. Cell padding in tables is now controlled by `table.padding` instead of the undocumented `column_padding_left`/`column_padding_right` config 3. The (optional, previously not enabled by default) `selected_row` and `selected_column` configuration has been removed. We now only highlight the selected cell. I could re-add this if people really like the feature but I'm guessing nobody uses it. The interface still looks the same with a default/empty config.nu: ![image](https://github.com/nushell/nushell/assets/26268125/e40161ba-a8ec-407a-932d-5ece6f4dc616)
131 lines
3.7 KiB
Rust
131 lines
3.7 KiB
Rust
mod commands;
|
|
mod default_context;
|
|
mod explore;
|
|
mod nu_common;
|
|
mod pager;
|
|
mod registry;
|
|
mod views;
|
|
|
|
use anyhow::Result;
|
|
use commands::{ExpandCmd, HelpCmd, NuCmd, QuitCmd, TableCmd, TryCmd};
|
|
pub use default_context::add_explore_context;
|
|
pub use explore::Explore;
|
|
use nu_common::{collect_pipeline, has_simple_value, CtrlC};
|
|
use nu_protocol::{
|
|
engine::{EngineState, Stack},
|
|
PipelineData, Value,
|
|
};
|
|
use pager::{Page, Pager, PagerConfig};
|
|
use registry::CommandRegistry;
|
|
use terminal_size::{Height, Width};
|
|
use views::{BinaryView, Orientation, Preview, RecordView};
|
|
|
|
mod util {
|
|
pub use super::nu_common::{create_lscolors, create_map};
|
|
}
|
|
|
|
fn run_pager(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
ctrlc: CtrlC,
|
|
input: PipelineData,
|
|
config: PagerConfig,
|
|
) -> Result<Option<Value>> {
|
|
let mut p = Pager::new(config.clone());
|
|
let commands = create_command_registry();
|
|
|
|
let is_record = matches!(input, PipelineData::Value(Value::Record { .. }, ..));
|
|
let is_binary = matches!(
|
|
input,
|
|
PipelineData::Value(Value::Binary { .. }, ..) | PipelineData::ByteStream(..)
|
|
);
|
|
|
|
if is_binary {
|
|
p.show_message("For help type :help");
|
|
|
|
let view = binary_view(input)?;
|
|
return p.run(engine_state, stack, ctrlc, Some(view), commands);
|
|
}
|
|
|
|
let (columns, data) = collect_pipeline(input)?;
|
|
|
|
let has_no_input = columns.is_empty() && data.is_empty();
|
|
if has_no_input {
|
|
return p.run(engine_state, stack, ctrlc, help_view(), commands);
|
|
}
|
|
|
|
p.show_message("For help type :help");
|
|
|
|
if let Some(value) = has_simple_value(&data) {
|
|
let text = value.to_abbreviated_string(config.nu_config);
|
|
let view = Some(Page::new(Preview::new(&text), false));
|
|
return p.run(engine_state, stack, ctrlc, view, commands);
|
|
}
|
|
|
|
let view = create_record_view(columns, data, is_record, config);
|
|
p.run(engine_state, stack, ctrlc, view, commands)
|
|
}
|
|
|
|
fn create_record_view(
|
|
columns: Vec<String>,
|
|
data: Vec<Vec<Value>>,
|
|
// wait, why would we use RecordView for something that isn't a record?
|
|
is_record: bool,
|
|
config: PagerConfig,
|
|
) -> Option<Page> {
|
|
let mut view = RecordView::new(columns, data);
|
|
if is_record {
|
|
view.set_orientation_current(Orientation::Left);
|
|
}
|
|
|
|
if config.tail {
|
|
if let Some((Width(w), Height(h))) = terminal_size::terminal_size() {
|
|
view.tail(w, h);
|
|
}
|
|
}
|
|
|
|
Some(Page::new(view, true))
|
|
}
|
|
|
|
fn help_view() -> Option<Page> {
|
|
Some(Page::new(HelpCmd::view(), false))
|
|
}
|
|
|
|
fn binary_view(input: PipelineData) -> Result<Page> {
|
|
let data = match input {
|
|
PipelineData::Value(Value::Binary { val, .. }, _) => val,
|
|
PipelineData::ByteStream(bs, _) => bs.into_bytes()?,
|
|
_ => unreachable!("checked beforehand"),
|
|
};
|
|
|
|
let view = BinaryView::new(data);
|
|
|
|
Ok(Page::new(view, true))
|
|
}
|
|
|
|
fn create_command_registry() -> CommandRegistry {
|
|
let mut registry = CommandRegistry::new();
|
|
create_commands(&mut registry);
|
|
create_aliases(&mut registry);
|
|
|
|
registry
|
|
}
|
|
|
|
fn create_commands(registry: &mut CommandRegistry) {
|
|
registry.register_command_view(NuCmd::new(), true);
|
|
registry.register_command_view(TableCmd::new(), true);
|
|
|
|
registry.register_command_view(ExpandCmd::new(), false);
|
|
registry.register_command_view(TryCmd::new(), false);
|
|
registry.register_command_view(HelpCmd::default(), false);
|
|
|
|
registry.register_command_reactive(QuitCmd);
|
|
}
|
|
|
|
fn create_aliases(registry: &mut CommandRegistry) {
|
|
registry.create_aliases("h", HelpCmd::NAME);
|
|
registry.create_aliases("e", ExpandCmd::NAME);
|
|
registry.create_aliases("q", QuitCmd::NAME);
|
|
registry.create_aliases("q!", QuitCmd::NAME);
|
|
}
|