-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathlib.rs
More file actions
155 lines (135 loc) · 5.55 KB
/
lib.rs
File metadata and controls
155 lines (135 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#![allow(clippy::arc_with_non_send_sync)]
pub use common::CliResult;
use inquire::ui::{Color, RenderConfig, StyleSheet, Styled};
pub mod commands;
pub mod common;
pub mod config;
pub mod js_command_match;
pub mod network;
pub mod network_for_transaction;
pub mod network_view_at_block;
pub mod transaction_signature_options;
pub mod types;
pub mod utils_command;
#[derive(Debug, Clone)]
pub struct GlobalContext {
pub config: crate::config::Config,
pub offline: bool,
pub verbosity: Verbosity,
}
#[derive(Debug, Copy, Clone, Default)]
pub enum Verbosity {
#[default]
Interactive,
TeachMe,
Quiet,
}
pub fn setup_tracing(verbosity: Verbosity) -> CliResult {
setup_tracing_with_extra_directives(verbosity, &[])
}
pub fn setup_tracing_with_extra_directives(
verbosity: Verbosity,
extra_directives: &[&str],
) -> CliResult {
use tracing::{Event, Level, Subscriber};
use tracing_indicatif::IndicatifLayer;
use tracing_indicatif::style::ProgressStyle;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{
fmt::{FmtContext, FormatEvent, FormatFields, format::Writer},
registry::LookupSpan,
};
struct SimpleFormatter;
impl<S, N> FormatEvent<S, N> for SimpleFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &Event<'_>,
) -> std::fmt::Result {
let level = *event.metadata().level();
let (icon, color_code) = match level {
Level::TRACE => ("TRACE ", "\x1b[35m"), // Magenta
Level::DEBUG => ("DEBUG ", "\x1b[34m"), // Blue
Level::INFO => ("", ""), // Default
Level::WARN => ("Warning: ", "\x1b[33m"), // Yellow
Level::ERROR => ("ERROR ", "\x1b[31m"), // Red
};
write!(writer, "{color_code}├ {icon}")?;
write!(writer, "\x1b[0m")?;
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}
match verbosity {
Verbosity::TeachMe => {
let mut env_filter = EnvFilter::from_default_env()
.add_directive(tracing::Level::WARN.into())
.add_directive("near_teach_me=info".parse()?)
.add_directive("near_cli_rs=info".parse()?);
for directive in extra_directives {
env_filter = env_filter.add_directive(directive.parse()?);
}
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().event_format(SimpleFormatter))
.with(env_filter)
.init();
}
Verbosity::Interactive => {
let indicatif_layer = IndicatifLayer::new()
.with_progress_style(
ProgressStyle::with_template(
"{spinner:.blue}{span_child_prefix} {span_name} {msg} {span_fields}",
)
.unwrap()
.tick_strings(&["◐", "◓", "◑", "◒"]),
)
.with_span_child_prefix_symbol("↳ ");
let mut env_filter = EnvFilter::from_default_env()
.add_directive(tracing::Level::WARN.into())
.add_directive("near_cli_rs=info".parse()?);
for directive in extra_directives {
env_filter = env_filter.add_directive(directive.parse()?);
}
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.event_format(SimpleFormatter)
.with_writer(indicatif_layer.get_stderr_writer()),
)
.with(indicatif_layer)
.with(env_filter)
.init();
}
Verbosity::Quiet => {}
};
Ok(())
}
pub fn get_global_render_config() -> RenderConfig<'static> {
let mut render_config = RenderConfig::default_colored();
render_config.prompt_prefix = Styled::new("◆ ").with_fg(Color::DarkGreen);
render_config.answered_prompt_prefix = Styled::new("◇ ").with_fg(Color::DarkGreen);
render_config.highlighted_option_prefix = Styled::new(" ●").with_fg(Color::DarkGreen);
render_config.unhighlighted_option_prefix = Styled::new(" ○").with_fg(Color::DarkGrey);
render_config.selected_checkbox = Styled::new("◼").with_fg(Color::LightGreen);
render_config.scroll_up_prefix = Styled::new("↑○").with_fg(Color::DarkGrey);
render_config.scroll_down_prefix = Styled::new("↓○").with_fg(Color::DarkGrey);
render_config.unselected_checkbox = Styled::new("◻").with_fg(Color::DarkGrey);
render_config.option = StyleSheet::new().with_fg(Color::DarkGrey);
render_config.selected_option = Some(StyleSheet::new().with_fg(Color::Grey));
render_config.new_line_prefix = Some(Styled::new("│ ").with_fg(Color::LightBlue));
render_config.answer_from_new_line = true;
render_config.error_message = render_config
.error_message
.with_prefix(Styled::new("❌").with_fg(Color::LightRed));
render_config.text_input = StyleSheet::new().with_fg(Color::LightYellow);
render_config.answer = StyleSheet::new().with_fg(Color::DarkGrey);
render_config.help_message = StyleSheet::new().with_fg(Color::DarkYellow);
render_config
}