From 4e6fc1a8c302b794734a2906d2410a935a292783 Mon Sep 17 00:00:00 2001 From: zhang_ji Date: Mon, 27 Jun 2016 09:54:04 +0800 Subject: [PATCH] [color] color for print item done. --- src/curses.rs | 7 +++++++ src/main.rs | 4 ++-- src/matcher.rs | 2 +- src/model.rs | 20 +++++++++++--------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/curses.rs b/src/curses.rs index 6d11df23..d77a75d4 100644 --- a/src/curses.rs +++ b/src/curses.rs @@ -99,6 +99,13 @@ impl Curses { addstr(text); attroff(attr); } + + pub fn caddch(&self, pair: i16, is_bold: bool, ch: char) { + let attr = self.get_color(pair, is_bold); + attron(attr); + addch(ch as u64); + attroff(attr); + } } // use default if x is COLOR_UNDEFINED, else use x diff --git a/src/main.rs b/src/main.rs index 101379af..b13f9e8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,8 +94,8 @@ fn main() { } Event::EvMatcherUpdateProcess => { - let (matched, total) : (u64, u64) = *val.downcast().unwrap(); - model.update_process_info(matched, total); + let (matched, total, processed) : (u64, u64, u64) = *val.downcast().unwrap(); + model.update_process_info(matched, total, processed); while let Ok(matched_item) = rx_matched.try_recv() { model.push_item(matched_item); diff --git a/src/matcher.rs b/src/matcher.rs index dd8635eb..c07b012d 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -64,7 +64,7 @@ impl Matcher { break; } } - (*self.eb_notify).set(Event::EvMatcherUpdateProcess, Box::new((self.num_matched, items.len() as u64))); + (*self.eb_notify).set(Event::EvMatcherUpdateProcess, Box::new((self.num_matched, items.len() as u64, self.item_pos as u64))); } fn reset_query(&mut self, query: &str) { diff --git a/src/model.rs b/src/model.rs index 7c888a93..785226cd 100644 --- a/src/model.rs +++ b/src/model.rs @@ -19,6 +19,7 @@ pub struct Model { pub items: Arc>>, // all items selected_indics: HashSet, pub matched_items: RefCell>, + processed_percentage: u64, item_cursor: usize, // the index of matched item currently highlighted. line_cursor: usize, // line No. item_start_pos: usize, // for screen scroll. @@ -39,6 +40,7 @@ impl Model { items: Arc::new(RwLock::new(Vec::new())), selected_indics: HashSet::new(), matched_items: RefCell::new(OrderedVec::new()), + processed_percentage: 100, item_cursor: 0, line_cursor: (max_y - 3) as usize, item_start_pos: 0, @@ -82,9 +84,10 @@ impl Model { self.query_cursor = cursor; } - pub fn update_process_info(&mut self, matched: u64, total: u64) { + pub fn update_process_info(&mut self, matched: u64, total: u64, processed: u64) { self.num_matched = matched; self.num_total = total; + self.processed_percentage = (processed+1)*100/(total+1); } pub fn push_item(&mut self, item: MatchedItem) { @@ -131,12 +134,14 @@ impl Model { pub fn print_info(&self) { mv(self.max_y-2, 0); - addstr(format!(" {}/{}", self.num_matched, self.num_total).as_str()); + addstr(format!(" {}/{}{}", self.num_matched, self.num_total, + if self.processed_percentage == 100 {"".to_string()} else {format!("({}%)", self.processed_percentage)} + ).as_str()); } fn print_item(&self, matched: &MatchedItem, is_current: bool) { let items = self.items.read().unwrap(); - let ref item = items[matched.index]; + let ref item = items[matched.index]; let is_selected = self.selected_indics.contains(&matched.index); @@ -152,16 +157,13 @@ impl Model { for (idx, ch) in item.text.chars().enumerate() { if let Some(&&index) = matched_indics_iter.peek() { if idx == index { - let attr = self.curses.get_color(COLOR_MATCHED, false); - attron(attr); - addch(ch as u64); - attroff(attr); + self.curses.caddch(COLOR_MATCHED, is_current, ch); let _ = matched_indics_iter.next(); } else { - addch(ch as u64); + self.curses.caddch(if is_current {COLOR_CURRENT} else {COLOR_NORMAL}, is_current, ch) } } else { - addch(ch as u64); + self.curses.caddch(if is_current {COLOR_CURRENT} else {COLOR_NORMAL}, is_current, ch) } if idx >= (self.max_x - 3) as usize { break;