feat(display): finalize display module with edge case handling
Swadhin Biswas committed 7 months ago
24a4879
1 parent 5b0417d
commit 24a4879
1 file changed
+49 -0
Showing 1 changed file+49-0
▼src/display/mod.rs
+49-0
| @@ -548,3 +548,52 @@ impl Display { | ||
548 | 548 | "\u{f6ff}", |
549 | 549 | &sys.local_ip |
550 | 550 | ); // |
551 | + add_field!( | |
552 | + self.config.show_public_ip, | |
553 | + "Public IP", | |
554 | + "\u{f0ac}", | |
555 | + &sys.public_ip | |
556 | + ); // | |
557 | + add_field!(self.config.show_locale, "Locale", "\u{f0ac}", &sys.locale); // | |
558 | + add_field!(self.config.show_song, "Song", "\u{f001}", &sys.song); // | |
559 | + add_field!(self.config.show_users, "Users", "\u{f007}", &sys.users); // | |
560 | + | |
561 | + // Blank line before color blocks | |
562 | + if self.config.show_colors && !self.config.no_color { | |
563 | + lines.push(String::new()); | |
564 | + let blocks = colors::color_blocks( | |
565 | + self.config.block_range.0, | |
566 | + self.config.block_range.1, | |
567 | + self.config.block_width, | |
568 | + ); | |
569 | + for block_line in blocks { | |
570 | + lines.push(block_line); | |
571 | + } | |
572 | + } | |
573 | + | |
574 | + lines | |
575 | + } | |
576 | +} | |
577 | + | |
578 | +/// Get terminal width in columns. | |
579 | +fn terminal_width() -> usize { | |
580 | + // Try COLUMNS env first | |
581 | + if let Ok(cols) = std::env::var("COLUMNS") { | |
582 | + if let Ok(n) = cols.parse::<usize>() { | |
583 | + return n; | |
584 | + } | |
585 | + } | |
586 | + | |
587 | + // Try `tput cols` | |
588 | + if let Ok(output) = std::process::Command::new("tput").arg("cols").output() { | |
589 | + if output.status.success() { | |
590 | + if let Ok(s) = String::from_utf8(output.stdout) { | |
591 | + if let Ok(n) = s.trim().parse::<usize>() { | |
592 | + return n; | |
593 | + } | |
594 | + } | |
595 | + } | |
596 | + } | |
597 | + | |
598 | + 80 | |
599 | +} | |
551 | 600 | |