-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Refactor codex card layout #4069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
codex-rs/tui/src/history_cell.rs
Outdated
struct CodexCardLayout { | ||
inner_width: usize, | ||
} | ||
|
||
impl CodexCardLayout { | ||
fn new(width: u16, max_inner_width: usize) -> Option<Self> { | ||
if width < 4 { | ||
return None; | ||
} | ||
let inner_width = std::cmp::min(width.saturating_sub(2) as usize, max_inner_width); | ||
Some(Self { inner_width }) | ||
} | ||
|
||
fn inner_width(&self) -> usize { | ||
self.inner_width | ||
} | ||
|
||
fn top_border(&self) -> Line<'static> { | ||
vec![Span::from(format!("╭{}╮", "─".repeat(self.inner_width))).dim()].into() | ||
} | ||
|
||
fn bottom_border(&self) -> Line<'static> { | ||
vec![Span::from(format!("╰{}╯", "─".repeat(self.inner_width))).dim()].into() | ||
} | ||
|
||
fn blank_row(&self) -> Line<'static> { | ||
self.row(Vec::new()) | ||
} | ||
|
||
fn row(&self, mut content: Vec<Span<'static>>) -> Line<'static> { | ||
let used_width: usize = content | ||
.iter() | ||
.map(|span| UnicodeWidthStr::width(span.content.as_ref())) | ||
.sum(); | ||
|
||
if used_width < self.inner_width { | ||
let padding = self.inner_width - used_width; | ||
content.push(Span::from(" ".repeat(padding)).dim()); | ||
} | ||
|
||
let mut spans: Vec<Span<'static>> = Vec::with_capacity(content.len() + 2); | ||
spans.push(Span::from("│").dim()); | ||
spans.extend(content); | ||
spans.push(Span::from("│").dim()); | ||
|
||
Line::from(spans) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be a single function, fn with_border(lines: Vec<Line>) -> Vec<Line>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this gives us the ability to wrap long lines and handle them differently
codex-rs/tui/src/history_cell.rs
Outdated
.unwrap_or(0); | ||
|
||
let mut out = Vec::with_capacity(lines.len() + 2); | ||
out.push(vec![Span::from(format!("╭{}╮", "─".repeat(inner_width))).dim()].into()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need Span::from() here, as .dim()
will turn a String into a span.
codex-rs/tui/src/history_cell.rs
Outdated
if used_width < inner_width { | ||
spans.push(Span::from(" ".repeat(inner_width - used_width)).dim()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is duplicative with with_border
codex-rs/tui/src/history_cell.rs
Outdated
model_spans.push(Span::from(" ").dim()); | ||
model_spans.push(Span::from(CHANGE_MODEL_HINT_COMMAND).cyan()); | ||
model_spans.push(Span::from(CHANGE_MODEL_HINT_EXPLANATION).dim()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
model_spans.push(Span::from(" ").dim()); | |
model_spans.push(Span::from(CHANGE_MODEL_HINT_COMMAND).cyan()); | |
model_spans.push(Span::from(CHANGE_MODEL_HINT_EXPLANATION).dim()); | |
model_spans.push(" ".dim()); | |
model_spans.push(CHANGE_MODEL_HINT_COMMAND.cyan()); | |
model_spans.push(CHANGE_MODEL_HINT_EXPLANATION.dim()); |
codex-rs/tui/src/history_cell.rs
Outdated
.sum(); | ||
let span_count = line.spans.len(); | ||
let mut spans: Vec<Span<'static>> = Vec::with_capacity(span_count + 4); | ||
spans.push(Span::from("│").dim()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
”| “.dim()
let mut title_spans: Vec<Span<'static>> = vec![ | ||
Span::from("│").dim(), | ||
Span::from(" ").dim(), | ||
let make_row = |spans: Vec<Span<'static>>| Line::from(spans); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make_row is redundant now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why? we convert a span to line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just call Line::from directly
Refactor it to be used in status