1 /**
2  * Copyright (c) 2007-2012, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file statusview_curses.cc
30  */
31 
32 #include "config.h"
33 
34 #include <vector>
35 #include <algorithm>
36 
37 #include "statusview_curses.hh"
38 
39 using namespace std;
40 
set_value(std::string value)41 void status_field::set_value(std::string value)
42 {
43     string_attrs_t &sa = this->sf_value.get_attrs();
44 
45     sa.clear();
46 
47     scrub_ansi_string(value, sa);
48     this->sf_value.with_string(value);
49 
50     if (this->sf_cylon) {
51         this->do_cylon();
52     }
53 }
54 
do_cylon()55 void status_field::do_cylon()
56 {
57     string_attrs_t &sa = this->sf_value.get_attrs();
58 
59     remove_string_attr(sa, &view_curses::VC_STYLE);
60 
61     struct line_range lr(this->sf_cylon_pos, this->sf_width);
62     view_colors &vc = view_colors::singleton();
63 
64     sa.emplace_back(lr, &view_curses::VC_STYLE,
65                     vc.attrs_for_role(view_colors::VCR_ACTIVE_STATUS) | A_REVERSE);
66 
67     this->sf_cylon_pos += 1;
68     if (this->sf_cylon_pos > this->sf_width) {
69         this->sf_cylon_pos = 0;
70     }
71 }
72 
set_stitch_value(view_colors::role_t left,view_colors::role_t right)73 void status_field::set_stitch_value(view_colors::role_t left,
74                                     view_colors::role_t right)
75 {
76     string_attrs_t &sa = this->sf_value.get_attrs();
77     struct line_range lr(0, 1);
78 
79     this->sf_value.get_string() = "::";
80     sa.clear();
81     sa.emplace_back(lr, &view_curses::VC_ROLE, left);
82     lr.lr_start = 1;
83     lr.lr_end   = 2;
84     sa.emplace_back(lr, &view_curses::VC_ROLE, right);
85 }
86 
do_update()87 void statusview_curses::do_update()
88 {
89     int           top, attrs, field, field_count, left = 0, right;
90     view_colors & vc = view_colors::singleton();
91     unsigned long width, height;
92 
93     if (!this->vc_visible) {
94         return;
95     }
96 
97     getmaxyx(this->sc_window, height, width);
98     this->window_change();
99 
100     top   = this->sc_top < 0 ? height + this->sc_top : this->sc_top;
101     right = width;
102     attrs = vc.attrs_for_role(this->sc_enabled ? view_colors::VCR_STATUS
103         : view_colors::VCR_INACTIVE_STATUS);
104 
105     wattron(this->sc_window, attrs);
106     wmove(this->sc_window, top, 0);
107     wclrtoeol(this->sc_window);
108     whline(this->sc_window, ' ', width);
109     wattroff(this->sc_window, attrs);
110 
111     if (this->sc_source != nullptr) {
112         field_count = this->sc_source->statusview_fields();
113         for (field = 0; field < field_count; field++) {
114             status_field &sf = this->sc_source->statusview_value_for_field(
115                 field);
116             struct line_range lr(0, sf.get_width());
117             attr_line_t val;
118             int x;
119 
120             val = sf.get_value();
121             if (!this->sc_enabled) {
122                 for (auto &sa : val.get_attrs()) {
123                     if (sa.sa_type == &view_curses::VC_STYLE) {
124                         sa.sa_value.sav_int &= ~(A_REVERSE | A_COLOR);
125                     } else if (sa.sa_type == &view_curses::VC_ROLE) {
126                         if (sa.sa_value.sav_int == view_colors::VCR_ALERT_STATUS) {
127                             sa.sa_value.sav_int = view_colors::VCR_INACTIVE_ALERT_STATUS;
128                         } else {
129                             sa.sa_value.sav_int = view_colors::VCR_NONE;
130                         }
131                     }
132                 }
133             } else if (sf.is_cylon()) {
134                 sf.do_cylon();
135             }
136             if (sf.get_left_pad() > 0) {
137                 val.insert(0, sf.get_left_pad(), ' ');
138             }
139 
140             if (sf.is_right_justified()) {
141                 val.right_justify(sf.get_width());
142 
143                 right -= sf.get_width();
144                 x = right;
145             }
146             else {
147                 x = left;
148                 left += sf.get_width();
149             }
150 
151             if (val.length() > sf.get_width()) {
152                 static const string ELLIPSIS = "\xE2\x8B\xAF";
153 
154                 if (sf.get_width() > 11) {
155                     size_t half_width = sf.get_width() / 2 - 1;
156 
157                     val.erase(half_width, val.length() - (half_width * 2));
158                     val.insert(half_width, ELLIPSIS);
159                 } else {
160                     val = val.subline(0, sf.get_width() - 1);
161                     val.append(ELLIPSIS);
162                 }
163             }
164 
165             auto default_role = sf.get_role();
166             if (!this->sc_enabled) {
167                 if (default_role == view_colors::VCR_ALERT_STATUS) {
168                     default_role = view_colors::VCR_INACTIVE_ALERT_STATUS;
169                 } else {
170                     default_role = view_colors::VCR_INACTIVE_STATUS;
171                 }
172             }
173 
174             mvwattrline(this->sc_window,
175                         top, x,
176                         val,
177                         lr,
178                         default_role);
179         }
180     }
181     wmove(this->sc_window, top + 1, 0);
182 }
183 
window_change()184 void statusview_curses::window_change()
185 {
186     if (this->sc_source == nullptr) {
187         return;
188     }
189 
190     int           field_count = this->sc_source->statusview_fields();
191     int           total_shares = 0;
192     unsigned long width, height;
193     double remaining = 0;
194     vector<status_field *> resizable;
195 
196     getmaxyx(this->sc_window, height, width);
197     // Silence the compiler. Remove this if height is used at a later stage.
198     (void)height;
199     remaining = width - 2;
200 
201     for (int field = 0; field < field_count; field++) {
202         auto &sf = this->sc_source->statusview_value_for_field(field);
203 
204         remaining -= sf.get_share() ? sf.get_min_width() : sf.get_width();
205         total_shares += sf.get_share();
206         if (sf.get_share()) {
207             resizable.emplace_back(&sf);
208         }
209     }
210 
211     if (remaining < 2) {
212         remaining = 0;
213     }
214 
215     std::stable_sort(begin(resizable), end(resizable), [](auto l, auto r) {
216         return r->get_share() < l->get_share();
217     });
218     for (auto sf : resizable) {
219         double divisor = total_shares / sf->get_share();
220         int available = remaining / divisor;
221         int actual_width;
222 
223         if ((sf->get_left_pad() + sf->get_value().length()) <
224             (sf->get_min_width() + available)) {
225             actual_width = std::max((int) sf->get_min_width(),
226                                     (int) (sf->get_left_pad() +
227                                            sf->get_value().length()));
228         } else {
229             actual_width = sf->get_min_width() + available;
230         }
231         remaining -= (actual_width - sf->get_min_width());
232         total_shares -= sf->get_share();
233 
234         sf->set_width(actual_width);
235     }
236 
237     this->sc_last_width = width;
238 }
239