Skip to content

Commit

Permalink
feat(tui): add last icmp packet code column (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Apr 20, 2024
1 parent 2f0ecbe commit d6d2b0e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/config/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub enum TuiColumn {
LastSeq,
/// The icmp packet type for the last probe for this hop.
LastIcmpPacketType,
/// The icmp packet code for the last probe for this hop.
LastIcmpPacketCode,
}

impl TryFrom<char> for TuiColumn {
Expand All @@ -113,6 +115,7 @@ impl TryFrom<char> for TuiColumn {
'P' => Ok(Self::LastDestPort),
'Q' => Ok(Self::LastSeq),
'T' => Ok(Self::LastIcmpPacketType),
'C' => Ok(Self::LastIcmpPacketCode),
c => Err(anyhow!(format!("unknown column code: {c}"))),
}
}
Expand Down Expand Up @@ -140,6 +143,7 @@ impl Display for TuiColumn {
Self::LastDestPort => write!(f, "P"),
Self::LastSeq => write!(f, "Q"),
Self::LastIcmpPacketType => write!(f, "T"),
Self::LastIcmpPacketCode => write!(f, "C"),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ pub enum ColumnType {
LastSeq,
/// The icmp packet type for the last probe for this hop.
LastIcmpPacketType,
/// The icmp packet code for the last probe for this hop.
LastIcmpPacketCode,
}

impl From<ColumnType> for char {
Expand All @@ -204,6 +206,7 @@ impl From<ColumnType> for char {
ColumnType::LastDestPort => 'P',
ColumnType::LastSeq => 'Q',
ColumnType::LastIcmpPacketType => 'T',
ColumnType::LastIcmpPacketCode => 'C',
}
}
}
Expand All @@ -230,6 +233,7 @@ impl From<TuiColumn> for Column {
TuiColumn::LastDestPort => Self::new_shown(ColumnType::LastDestPort),
TuiColumn::LastSeq => Self::new_shown(ColumnType::LastSeq),
TuiColumn::LastIcmpPacketType => Self::new_shown(ColumnType::LastIcmpPacketType),
TuiColumn::LastIcmpPacketCode => Self::new_shown(ColumnType::LastIcmpPacketCode),
}
}
}
Expand All @@ -256,6 +260,7 @@ impl Display for ColumnType {
Self::LastDestPort => write!(f, "Dprt"),
Self::LastSeq => write!(f, "Seq"),
Self::LastIcmpPacketType => write!(f, "Type"),
Self::LastIcmpPacketCode => write!(f, "Code"),
}
}
}
Expand Down Expand Up @@ -284,6 +289,7 @@ impl ColumnType {
Self::LastDestPort => ColumnWidth::Fixed(7),
Self::LastSeq => ColumnWidth::Fixed(7),
Self::LastIcmpPacketType => ColumnWidth::Fixed(7),
Self::LastIcmpPacketCode => ColumnWidth::Fixed(7),
}
}
}
Expand Down Expand Up @@ -341,6 +347,7 @@ mod tests {
Column::new_hidden(ColumnType::LastDestPort),
Column::new_hidden(ColumnType::LastSeq),
Column::new_hidden(ColumnType::LastIcmpPacketType),
Column::new_hidden(ColumnType::LastIcmpPacketCode),
])
);
}
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn new_cell(
ColumnType::LastDestPort => render_port_cell(hop.last_dest_port()),
ColumnType::LastSeq => render_usize_cell(usize::from(hop.last_sequence())),
ColumnType::LastIcmpPacketType => render_icmp_packet_type_cell(hop.last_icmp_packet_type()),
ColumnType::LastIcmpPacketCode => render_icmp_packet_code_cell(hop.last_icmp_packet_type()),
}
}

Expand Down Expand Up @@ -220,6 +221,13 @@ fn render_icmp_packet_type_cell(icmp_packet_type: Option<IcmpPacketType>) -> Cel
}
}

fn render_icmp_packet_code_cell(icmp_packet_type: Option<IcmpPacketType>) -> Cell<'static> {
match icmp_packet_type {
Some(IcmpPacketType::Unreachable(code)) => Cell::from(format!("{code}")),
_ => Cell::from("n/a"),
}
}

fn render_port_cell(port: u16) -> Cell<'static> {
if port > 0 {
Cell::from(format!("{port}"))
Expand Down

0 comments on commit d6d2b0e

Please sign in to comment.