]> git.lizzy.rs Git - rust.git/blob - src/lists.rs
terminating newline bug
[rust.git] / src / lists.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use utils::make_indent;
12
13 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
14 pub enum ListTactic {
15     // One item per row.
16     Vertical,
17     // All items on one row.
18     Horizontal,
19     // Try Horizontal layout, if that fails then vertical
20     HorizontalVertical,
21     // Pack as many items as possible per row over (possibly) many rows.
22     Mixed,
23 }
24
25 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
26 pub enum SeparatorTactic {
27     Always,
28     Never,
29     Vertical,
30 }
31
32 // TODO having some helpful ctors for ListFormatting would be nice.
33 pub struct ListFormatting<'a> {
34     pub tactic: ListTactic,
35     pub separator: &'a str,
36     pub trailing_separator: SeparatorTactic,
37     pub indent: usize,
38     // Available width if we layout horizontally.
39     pub h_width: usize,
40     // Available width if we layout vertically
41     pub v_width: usize,
42 }
43
44 // Format a list of strings into a string.
45 // Precondition: all strings in items are trimmed.
46 pub fn write_list<'b>(items: &[(String, String)], formatting: &ListFormatting<'b>) -> String {
47     if items.len() == 0 {
48         return String::new();
49     }
50
51     let mut tactic = formatting.tactic;
52
53     // Conservatively overestimates because of the changing separator tactic.
54     let sep_count = if formatting.trailing_separator != SeparatorTactic::Never {
55         items.len()
56     } else {
57         items.len() - 1
58     };
59     let sep_len = formatting.separator.len();
60     let total_sep_len = (sep_len + 1) * sep_count;
61
62     let total_width = calculate_width(items);
63
64     // Check if we need to fallback from horizontal listing, if possible.
65     if tactic == ListTactic::HorizontalVertical { 
66         if total_width + total_sep_len > formatting.h_width {
67             tactic = ListTactic::Vertical;
68         } else {
69             tactic = ListTactic::Horizontal;
70         }
71     }
72
73     // Now that we know how we will layout, we can decide for sure if there
74     // will be a trailing separator.
75     let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
76
77     // Create a buffer for the result.
78     // TODO could use a StringBuffer or rope for this
79     let alloc_width = if tactic == ListTactic::Horizontal {
80         total_width + total_sep_len
81     } else {
82         total_width + items.len() * (formatting.indent + 1)
83     };
84     let mut result = String::with_capacity(alloc_width);
85
86     let mut line_len = 0;
87     let indent_str = &make_indent(formatting.indent);
88     for (i, &(ref item, ref comment)) in items.iter().enumerate() {
89         let first = i == 0;
90         let separate = i != items.len() - 1 || trailing_separator;
91
92         match tactic {
93             ListTactic::Horizontal if !first => {
94                 result.push(' ');
95             }
96             ListTactic::Vertical if !first => {
97                 result.push('\n');
98                 result.push_str(indent_str);
99             }
100             ListTactic::Mixed => {
101                 let mut item_width = item.len();
102                 if separate {
103                     item_width += sep_len;
104                 }
105
106                 if line_len > 0 && line_len + item_width > formatting.v_width {
107                     result.push('\n');
108                     result.push_str(indent_str);
109                     line_len = 0;
110                 }
111
112                 if line_len > 0 {
113                     result.push(' ');
114                     line_len += 1;
115                 }
116
117                 line_len += item_width;
118             }
119             _ => {}
120         }
121
122         result.push_str(item);
123         
124         if tactic != ListTactic::Vertical && comment.len() > 0 {
125             result.push(' ');
126             result.push_str(comment);
127         }
128
129         if separate {
130             result.push_str(formatting.separator);
131         }
132
133         if tactic == ListTactic::Vertical && comment.len() > 0 {
134             result.push(' ');
135             result.push_str(comment);
136         }
137     }
138
139     result
140 }
141
142 fn needs_trailing_separator(separator_tactic: SeparatorTactic, list_tactic: ListTactic) -> bool {
143     match separator_tactic {
144         SeparatorTactic::Always => true,
145         SeparatorTactic::Vertical => list_tactic == ListTactic::Vertical,
146         SeparatorTactic::Never => false,
147     }
148 }
149
150 fn calculate_width(items:&[(String, String)]) -> usize {
151     let missed_width = items.iter().map(|&(_, ref s)| {
152         let text_len = s.trim().len();
153         if text_len > 0 {
154             // We'll put a space before any comment.
155             text_len + 1
156         } else {
157             text_len
158         }
159     }).fold(0, |a, l| a + l);
160     let item_width = items.iter().map(|&(ref s, _)| s.len()).fold(0, |a, l| a + l);
161     missed_width + item_width
162 }