-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
406 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod pod; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod kube; | ||
pub mod view; | ||
pub mod message; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod filter; | ||
mod log; | ||
mod pod; | ||
|
||
pub use log::*; | ||
pub use pod::*; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use anyhow::Result; | ||
|
||
use crate::{message::Message, workers::Kube}; | ||
|
||
use super::kube::LogConfig; | ||
|
||
#[derive(Debug)] | ||
pub enum LogMessage { | ||
Request(LogConfig), | ||
Response(Result<Vec<String>>), | ||
} | ||
|
||
impl From<LogMessage> for Message { | ||
fn from(m: LogMessage) -> Message { | ||
Message::Kube(Kube::Log(m)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod tab; | ||
mod widgets; | ||
|
||
pub use tab::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::{cell::RefCell, rc::Rc}; | ||
|
||
use crossbeam::channel::Sender; | ||
use ratatui::layout::{Constraint, Direction}; | ||
|
||
use crate::{ | ||
action::view_id, | ||
clipboard::Clipboard, | ||
context::Namespace, | ||
message::Message, | ||
ui::{ | ||
tab::{LayoutElement, NestedLayoutElement, NestedWidgetLayout}, | ||
widget::Widget, | ||
Tab, | ||
}, | ||
}; | ||
|
||
use super::widgets::{log_query_help_widget, log_query_widget, log_widget, pod_widget}; | ||
|
||
pub struct PodTab { | ||
pub tab: Tab<'static>, | ||
pub log_query_help_popup: Widget<'static>, | ||
} | ||
|
||
impl PodTab { | ||
pub fn new( | ||
title: &'static str, | ||
tx: &Sender<Message>, | ||
clipboard: &Option<Rc<RefCell<Clipboard>>>, | ||
split_direction: Direction, | ||
namespaces: Rc<RefCell<Namespace>>, | ||
) -> Self { | ||
let pod_widget = pod_widget(tx); | ||
let log_query_widget = log_query_widget(tx, namespaces); | ||
let log_widget = log_widget(clipboard); | ||
let log_query_help_widget = log_query_help_widget(); | ||
|
||
let layout = layout(split_direction); | ||
|
||
let mut tab = Tab::new( | ||
view_id::tab_pod, | ||
title, | ||
[pod_widget, log_query_widget, log_widget], | ||
layout, | ||
); | ||
|
||
tab.activate_widget_by_id(view_id::tab_pod_widget_pod); | ||
|
||
Self { | ||
tab, | ||
log_query_help_popup: log_query_help_widget, | ||
} | ||
} | ||
} | ||
|
||
fn layout(split_direction: Direction) -> NestedWidgetLayout { | ||
let pod_layout = { | ||
let constraint = match split_direction { | ||
Direction::Horizontal => Constraint::Percentage(50), | ||
Direction::Vertical => Constraint::Percentage(45), // log_query領域分小さくする | ||
}; | ||
|
||
NestedLayoutElement(constraint, LayoutElement::WidgetIndex(0)) | ||
}; | ||
|
||
let log_query_layout = | ||
NestedLayoutElement(Constraint::Length(3), LayoutElement::WidgetIndex(1)); | ||
|
||
let log_layout = NestedLayoutElement( | ||
Constraint::Percentage(50), | ||
LayoutElement::NestedElement( | ||
NestedWidgetLayout::default() | ||
.direction(Direction::Vertical) | ||
.nested_widget_layout([ | ||
log_query_layout, | ||
NestedLayoutElement(Constraint::Min(3), LayoutElement::WidgetIndex(2)), | ||
]), | ||
), | ||
); | ||
|
||
NestedWidgetLayout::default() | ||
.direction(split_direction) | ||
.nested_widget_layout([pod_layout, log_layout]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
mod log; | ||
mod log_query; | ||
mod log_query_help; | ||
mod pod; | ||
|
||
pub(super) use log::*; | ||
pub(super) use log_query::*; | ||
pub(super) use log_query_help::*; | ||
pub(super) use pod::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{cell::RefCell, rc::Rc}; | ||
|
||
use crossterm::event::KeyCode; | ||
use ratatui::widgets::Block; | ||
|
||
use crate::{ | ||
action::view_id, | ||
clipboard::Clipboard, | ||
message::UserEvent, | ||
ui::{ | ||
event::EventResult, | ||
widget::{config::WidgetConfig, Item, Text, Widget, WidgetTrait as _}, | ||
Window, | ||
}, | ||
}; | ||
|
||
pub fn log_widget(clipboard: &Option<Rc<RefCell<Clipboard>>>) -> Widget<'static> { | ||
let builder = Text::builder() | ||
.id(view_id::tab_pod_widget_log) | ||
.widget_config(&WidgetConfig::builder().title("Log").build()) | ||
.wrap() | ||
.follow() | ||
.block_injection(block_injection()) | ||
.action(UserEvent::from(KeyCode::Enter), add_blankline()); | ||
|
||
if let Some(cb) = clipboard { | ||
builder.clipboard(cb.clone()) | ||
} else { | ||
builder | ||
} | ||
.build() | ||
.into() | ||
} | ||
|
||
fn block_injection() -> impl Fn(&Text, bool, bool) -> Block<'static> { | ||
|text: &Text, is_active: bool, is_mouse_over: bool| { | ||
let (index, size) = text.state(); | ||
|
||
let mut config = text.widget_config().clone(); | ||
|
||
*config.title_mut() = format!("Log [{}/{}]", index, size).into(); | ||
|
||
config.render_block(text.can_activate() && is_active, is_mouse_over) | ||
} | ||
} | ||
|
||
fn add_blankline() -> impl Fn(&mut Window) -> EventResult { | ||
move |w: &mut Window| { | ||
let w = w.find_widget_mut(view_id::tab_pod_widget_log); | ||
|
||
w.select_last(); | ||
w.append_widget_item(Item::Single(Default::default())); | ||
|
||
EventResult::Nop | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::{cell::RefCell, rc::Rc}; | ||
|
||
use crossbeam::channel::Sender; | ||
use crossterm::event::KeyCode; | ||
|
||
use crate::{ | ||
action::view_id, | ||
context::Namespace, | ||
features::pod::{ | ||
kube::{LogConfig, LogPrefixType}, | ||
message::LogMessage, | ||
}, | ||
message::{Message, UserEvent}, | ||
ui::{ | ||
event::EventResult, | ||
widget::{ | ||
config::WidgetConfig, input::InputFormBuilder, SelectedItem, Widget, WidgetTrait as _, | ||
}, | ||
Window, | ||
}, | ||
}; | ||
|
||
pub fn log_query_widget(tx: &Sender<Message>, namespaces: Rc<RefCell<Namespace>>) -> Widget<'static> { | ||
let tx = tx.clone(); | ||
|
||
InputFormBuilder::default() | ||
.id(view_id::tab_pod_widget_log_query) | ||
.widget_config(WidgetConfig::builder().title("Log Query").build()) | ||
.actions(UserEvent::from(KeyCode::Enter), exec_query(tx, namespaces)) | ||
.build() | ||
.into() | ||
} | ||
|
||
fn exec_query( | ||
tx: Sender<Message>, | ||
namespaces: Rc<RefCell<Namespace>>, | ||
) -> impl Fn(&mut Window) -> EventResult { | ||
move |w: &mut Window| { | ||
let widget = w.find_widget_mut(view_id::tab_pod_widget_log_query); | ||
|
||
let Some(SelectedItem::Literal { metadata: _, item }) = widget.widget_item() else { | ||
return EventResult::Ignore; | ||
}; | ||
|
||
if item == "?" || item == "help" { | ||
widget.clear(); | ||
w.open_popup(view_id::tab_pod_widget_log_query_help); | ||
return EventResult::Nop; | ||
} | ||
|
||
w.widget_clear(view_id::tab_pod_widget_log); | ||
|
||
let namespaces = namespaces.borrow(); | ||
|
||
let prefix_type = if 1 < namespaces.len() { | ||
LogPrefixType::All | ||
} else { | ||
LogPrefixType::PodAndContainer | ||
}; | ||
|
||
let config = LogConfig::new(item, namespaces.to_owned(), prefix_type); | ||
|
||
tx.send(LogMessage::Request(config).into()) | ||
.expect("Failed to send LogMessage::Request"); | ||
|
||
EventResult::Ignore | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crossterm::event::KeyCode; | ||
use indoc::indoc; | ||
|
||
use crate::{ | ||
action::view_id, | ||
message::UserEvent, | ||
ui::{ | ||
event::EventResult, | ||
widget::{config::WidgetConfig, Text, Widget}, | ||
Window, | ||
}, | ||
}; | ||
|
||
pub fn log_query_help_widget() -> Widget<'static> { | ||
Text::builder() | ||
.id(view_id::tab_pod_widget_log_query_help) | ||
.widget_config(&WidgetConfig::builder().title("Log Query Help").build()) | ||
.items(content()) | ||
.action(UserEvent::from(KeyCode::Enter), close_popup()) | ||
.build() | ||
.into() | ||
} | ||
|
||
fn content() -> Vec<String> { | ||
indoc! {r#" | ||
Usage: QUERY [ QUERY ]... | ||
Queries: | ||
pod:<regex> (alias: pods, po, p) | ||
!pod:<regex> (alias: !pods, !po, p) | ||
container:<regex> (alias: containers, co, c) | ||
!container:<regex> (alias: !containers, !co, !c) | ||
log:<regex> (alias: logs, lo, l) | ||
!log:<regex> (alias: !logs, !lo, !l) | ||
label:<selector> (alias: labels) | ||
field:<selector> (alias: fields) | ||
<resource>/<name> | ||
Resources: | ||
pod (alias: pods, po) | ||
replicaset (alias: replicasets, rs) | ||
deployment (alias: deployments, deploy) | ||
statefulset (alias: statefulsets, sts) | ||
daemonset (alias: daemonsets, ds) | ||
service (alias: services, svc) | ||
job (alias: jobs) | ||
"# } | ||
.lines() | ||
.map(ToString::to_string) | ||
.collect() | ||
} | ||
|
||
fn close_popup() -> impl Fn(&mut Window) -> EventResult { | ||
move |w: &mut Window| { | ||
w.close_popup(); | ||
EventResult::Nop | ||
} | ||
} |
Oops, something went wrong.