Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.

Commit d158b0c

Browse files
committed
terminal-tui: Support context component
1 parent 5f8836e commit d158b0c

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

terminal-tui/src/components.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,153 @@ where
468468
)
469469
}
470470
}
471+
472+
/// A single-line component, that displays multiple spans containing
473+
/// context information.
474+
pub struct Context {
475+
props: Props,
476+
}
477+
478+
impl Default for Context {
479+
fn default() -> Self {
480+
Self {
481+
props: Props::default(),
482+
}
483+
.height(1)
484+
}
485+
}
486+
487+
impl Context {
488+
pub fn context(mut self, context: String) -> Self {
489+
self.attr(Attribute::Custom("context"), AttrValue::String(context));
490+
self
491+
}
492+
493+
pub fn id(mut self, id: String) -> Self {
494+
self.attr(Attribute::Custom("id"), AttrValue::String(id));
495+
self
496+
}
497+
498+
pub fn title(mut self, title: String) -> Self {
499+
self.attr(Attribute::Custom("title"), AttrValue::String(title));
500+
self
501+
}
502+
503+
pub fn author(mut self, author: String) -> Self {
504+
self.attr(Attribute::Custom("author"), AttrValue::String(author));
505+
self
506+
}
507+
508+
pub fn count(mut self, count: String) -> Self {
509+
self.attr(Attribute::Custom("count"), AttrValue::String(count));
510+
self
511+
}
512+
513+
pub fn height(mut self, h: u16) -> Self {
514+
self.attr(Attribute::Height, AttrValue::Size(h));
515+
self
516+
}
517+
}
518+
519+
impl MockComponent for Context {
520+
fn view(&mut self, render: &mut Frame, area: Rect) {
521+
if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) {
522+
let context = self
523+
.props
524+
.get_or(
525+
Attribute::Custom("context"),
526+
AttrValue::String(String::new()),
527+
)
528+
.unwrap_string();
529+
let id = self
530+
.props
531+
.get_or(Attribute::Custom("id"), AttrValue::String(String::new()))
532+
.unwrap_string();
533+
let title = self
534+
.props
535+
.get_or(Attribute::Custom("title"), AttrValue::String(String::new()))
536+
.unwrap_string();
537+
let author = self
538+
.props
539+
.get_or(
540+
Attribute::Custom("author"),
541+
AttrValue::String(String::new()),
542+
)
543+
.unwrap_string();
544+
let count = self
545+
.props
546+
.get_or(Attribute::Custom("count"), AttrValue::String(String::new()))
547+
.unwrap_string();
548+
549+
let context_w = context.len() as u16;
550+
let id_w = id.len() as u16;
551+
let author_w = author.len() as u16;
552+
let count_w = count.len() as u16;
553+
let title_w = area
554+
.width
555+
.saturating_sub(context_w + id_w + count_w + author_w);
556+
557+
let area = TuiLayout::default()
558+
.direction(LayoutDirection::Horizontal)
559+
.constraints(
560+
[
561+
Constraint::Length(context_w),
562+
Constraint::Length(id_w),
563+
Constraint::Length(title_w),
564+
Constraint::Length(author_w),
565+
Constraint::Length(count_w),
566+
]
567+
.as_ref(),
568+
)
569+
.split(area);
570+
571+
let context =
572+
Paragraph::new(context).style(Style::default().bg(Color::Rgb(238, 111, 248)));
573+
render.render_widget(context, area[0]);
574+
575+
let id = Paragraph::new(id).style(
576+
Style::default()
577+
.fg(Color::Rgb(117, 113, 249))
578+
.bg(Color::Rgb(40, 40, 40)),
579+
);
580+
render.render_widget(id, area[1]);
581+
582+
let title = Paragraph::new(title).style(
583+
Style::default()
584+
.fg(Color::Rgb(70, 70, 70))
585+
.bg(Color::Rgb(40, 40, 40)),
586+
);
587+
render.render_widget(title, area[2]);
588+
589+
let author = Paragraph::new(author).style(
590+
Style::default()
591+
.fg(Color::Rgb(117, 113, 249))
592+
.bg(Color::Rgb(40, 40, 40)),
593+
);
594+
render.render_widget(author, area[3]);
595+
596+
let count = Paragraph::new(count).style(
597+
Style::default()
598+
.fg(Color::Rgb(70, 70, 70))
599+
.bg(Color::Rgb(50, 50, 50)),
600+
);
601+
render.render_widget(count, area[4]);
602+
}
603+
}
604+
605+
fn query(&self, attr: Attribute) -> Option<AttrValue> {
606+
self.props.get(attr)
607+
}
608+
609+
fn attr(&mut self, attr: Attribute, value: AttrValue) {
610+
self.props.set(attr, value)
611+
}
612+
613+
fn state(&self) -> State {
614+
State::None
615+
}
616+
617+
fn perform(&mut self, _cmd: Cmd) -> CmdResult {
618+
CmdResult::None
619+
}
620+
}

0 commit comments

Comments
 (0)