mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
add src/util_msg.rs
This commit is contained in:
78
src/lib.rs
78
src/lib.rs
@@ -4,7 +4,7 @@ use std::{
|
||||
//cell::RefCell,
|
||||
env,
|
||||
fs,
|
||||
io::{self, Write, Error, ErrorKind},
|
||||
io::{self, Error, ErrorKind},
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
@@ -18,6 +18,7 @@ pub const SIZE_TB: i64 = SIZE_PB * SIZE_KB;
|
||||
|
||||
pub mod util_io;
|
||||
pub mod util_os;
|
||||
pub mod util_msg;
|
||||
|
||||
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
@@ -113,81 +114,6 @@ pub fn new_box_ioerror(m: &str) -> Box<dyn std::error::Error> {
|
||||
Box::new(Error::new(ErrorKind::Other, m))
|
||||
}
|
||||
|
||||
pub enum MessageType { INFO, OK, WARN, ERROR, }
|
||||
|
||||
pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
let mut t = term::stdout().unwrap();
|
||||
match color {
|
||||
Some(c) => t.fg(c).unwrap(),
|
||||
None => (),
|
||||
}
|
||||
if is_bold {
|
||||
t.attr(term::Attr::Bold).unwrap();
|
||||
}
|
||||
write!(t, "{}", m).unwrap();
|
||||
t.reset().unwrap();
|
||||
}
|
||||
|
||||
pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
print_color(color, is_bold, m);
|
||||
flush_stdout();
|
||||
}
|
||||
|
||||
pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) {
|
||||
print_color(color, true, h);
|
||||
println!(" {}", message);
|
||||
}
|
||||
|
||||
pub fn print_message(mt: MessageType, message: &str) {
|
||||
match mt {
|
||||
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
||||
MessageType::WARN => print_message_ex(Some(term::color::YELLOW), "[WARN ]", message),
|
||||
MessageType::ERROR => print_message_ex(Some(term::color::RED), "[ERROR]", message),
|
||||
MessageType::INFO => print_message_ex(None, "[INFO]", message),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flush_stdout() {
|
||||
io::stdout().flush().ok();
|
||||
}
|
||||
|
||||
pub fn clear_lastline() {
|
||||
print_lastline("");
|
||||
}
|
||||
|
||||
pub fn print_lastline(line: &str) {
|
||||
print!("\x1b[1000D{}\x1b[K", line);
|
||||
flush_stdout();
|
||||
}
|
||||
|
||||
// thanks https://blog.csdn.net/star_xiong/article/details/89401149
|
||||
pub fn find_char_boundary(s: &str, index: usize) -> usize {
|
||||
if s.len() <= index {
|
||||
return index;
|
||||
}
|
||||
let mut new_index = index;
|
||||
while !s.is_char_boundary(new_index) {
|
||||
new_index += 1;
|
||||
}
|
||||
new_index
|
||||
}
|
||||
|
||||
pub fn get_term_width_message(message: &str, left: usize) -> String {
|
||||
match term_size::dimensions() {
|
||||
None => message.to_string(),
|
||||
Some((w, _h)) => {
|
||||
let len = message.len();
|
||||
if w > len {
|
||||
return message.to_string();
|
||||
}
|
||||
let mut s = String::new();
|
||||
s.push_str(&message[0..find_char_boundary(&message, w-10-5-left)]);
|
||||
s.push_str("[...]");
|
||||
s.push_str(&message[find_char_boundary(&message, len-10)..]);
|
||||
s
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_size(size: &str) -> XResult<i64> {
|
||||
let lower_size = size.to_lowercase();
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
};
|
||||
|
||||
use super::get_display_size;
|
||||
use super::print_lastline;
|
||||
use super::util_msg::print_lastline;
|
||||
|
||||
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
|
||||
pub const SIZE_KB: i64 = 1024;
|
||||
|
||||
79
src/util_msg.rs
Normal file
79
src/util_msg.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
};
|
||||
|
||||
pub enum MessageType { INFO, OK, WARN, ERROR, }
|
||||
|
||||
pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
let mut t = term::stdout().unwrap();
|
||||
match color {
|
||||
Some(c) => t.fg(c).unwrap(),
|
||||
None => (),
|
||||
}
|
||||
if is_bold {
|
||||
t.attr(term::Attr::Bold).unwrap();
|
||||
}
|
||||
write!(t, "{}", m).unwrap();
|
||||
t.reset().unwrap();
|
||||
}
|
||||
|
||||
pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
print_color(color, is_bold, m);
|
||||
flush_stdout();
|
||||
}
|
||||
|
||||
pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) {
|
||||
print_color(color, true, h);
|
||||
println!(" {}", message);
|
||||
}
|
||||
|
||||
pub fn print_message(mt: MessageType, message: &str) {
|
||||
match mt {
|
||||
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
||||
MessageType::WARN => print_message_ex(Some(term::color::YELLOW), "[WARN ]", message),
|
||||
MessageType::ERROR => print_message_ex(Some(term::color::RED), "[ERROR]", message),
|
||||
MessageType::INFO => print_message_ex(None, "[INFO]", message),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flush_stdout() {
|
||||
io::stdout().flush().ok();
|
||||
}
|
||||
|
||||
pub fn clear_lastline() {
|
||||
print_lastline("");
|
||||
}
|
||||
|
||||
pub fn print_lastline(line: &str) {
|
||||
print!("\x1b[1000D{}\x1b[K", line);
|
||||
flush_stdout();
|
||||
}
|
||||
|
||||
// thanks https://blog.csdn.net/star_xiong/article/details/89401149
|
||||
pub fn find_char_boundary(s: &str, index: usize) -> usize {
|
||||
if s.len() <= index {
|
||||
return index;
|
||||
}
|
||||
let mut new_index = index;
|
||||
while !s.is_char_boundary(new_index) {
|
||||
new_index += 1;
|
||||
}
|
||||
new_index
|
||||
}
|
||||
|
||||
pub fn get_term_width_message(message: &str, left: usize) -> String {
|
||||
match term_size::dimensions() {
|
||||
None => message.to_string(),
|
||||
Some((w, _h)) => {
|
||||
let len = message.len();
|
||||
if w > len {
|
||||
return message.to_string();
|
||||
}
|
||||
let mut s = String::new();
|
||||
s.push_str(&message[0..find_char_boundary(&message, w-10-5-left)]);
|
||||
s.push_str("[...]");
|
||||
s.push_str(&message[find_char_boundary(&message, len-10)..]);
|
||||
s
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user