Skip to content

Commit 1bb5ecb

Browse files
authored
feat: Allow showing wide field even without wide mode (#1077)
Current logic outputs requested wide field only in the wide mode. This hurts in UX and views configuration requiring wide mode by default. Change the logic to return the wide field in wide more or when it is explicitly requested.
1 parent 5cdc07b commit 1bb5ecb

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

structable_derive/src/structable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl ToTokens for TableStructInputReceiver {
151151
},
152152

153153
true => quote!(
154-
if (options.fields.is_empty() || options.fields.contains(#field_title)) && options.wide {
154+
if options.fields.contains(#field_title) || (options.fields.is_empty() && options.wide) {
155155
res.push(Vec::from([
156156
#field_title.to_string(),
157157
#field_value
@@ -167,7 +167,7 @@ impl ToTokens for TableStructInputReceiver {
167167
}
168168
),
169169
true => quote!(
170-
if (options.fields.is_empty() || options.fields.contains(#field_title)) && options.wide {
170+
if options.fields.contains(#field_title) || (options.fields.is_empty() && options.wide) {
171171
row.push(#field_vec_value);
172172
}
173173
),
@@ -180,7 +180,7 @@ impl ToTokens for TableStructInputReceiver {
180180
}
181181
),
182182
true => quote!(
183-
if (options.fields.is_empty() || options.fields.contains(#field_title)) && options.wide {
183+
if options.fields.contains(#field_title) || (options.fields.is_empty() && options.wide) {
184184
headers.push(#field_title .to_string());
185185
}
186186
),

structable_derive/tests/main.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,56 @@ fn test_single_wide() {
109109
);
110110
}
111111

112+
#[test]
113+
fn test_single_wide_column() {
114+
let config = OutputConfig {
115+
fields: BTreeSet::from(["Long".into()]),
116+
wide: false,
117+
pretty: false,
118+
};
119+
let user = User {
120+
id: 1,
121+
first_name: "Scooby".into(),
122+
last_name: "Doo".into(),
123+
extra: "XYZ".into(),
124+
complex_data: Some(json!({"a": "b", "c": "d"})),
125+
dummy: None,
126+
};
127+
let data = user.build(&config);
128+
assert_eq!(
129+
data,
130+
(
131+
vec!["Attribute".into(), "Value".into()],
132+
vec![vec!["Long".into(), "XYZ".into()],]
133+
)
134+
);
135+
}
136+
137+
#[test]
138+
fn test_single_wide_column_wide_mode() {
139+
let config = OutputConfig {
140+
fields: BTreeSet::from(["Long".into()]),
141+
wide: true,
142+
pretty: false,
143+
};
144+
let user = User {
145+
id: 1,
146+
first_name: "Scooby".into(),
147+
last_name: "Doo".into(),
148+
extra: "XYZ".into(),
149+
complex_data: Some(json!({"a": "b", "c": "d"})),
150+
dummy: None,
151+
};
152+
let data = user.build(&config);
153+
assert_eq!(
154+
data,
155+
(
156+
vec!["Attribute".into(), "Value".into()],
157+
vec![vec!["Long".into(), "XYZ".into()],]
158+
)
159+
);
160+
}
161+
112162
#[test]
113163
fn test_single_wide_pretty() {
114164
let config = OutputConfig {
@@ -187,6 +237,78 @@ fn test_list() {
187237
);
188238
}
189239

240+
#[test]
241+
fn test_list_wide_column() {
242+
let config = OutputConfig {
243+
fields: BTreeSet::from(["Long".into()]),
244+
wide: false,
245+
pretty: false,
246+
};
247+
let users = vec![
248+
User {
249+
id: 1,
250+
first_name: "Scooby".into(),
251+
last_name: "Doo".into(),
252+
extra: "Foo".into(),
253+
complex_data: Some(json!({"a": "b", "c": "d"})),
254+
dummy: None,
255+
},
256+
User {
257+
id: 2,
258+
first_name: "John".into(),
259+
last_name: "Cena".into(),
260+
extra: "Bar".into(),
261+
complex_data: None,
262+
dummy: Some("foo".into()),
263+
},
264+
];
265+
266+
let data = users.build(&config);
267+
assert_eq!(
268+
data,
269+
(
270+
vec!["Long".into(),],
271+
vec![vec!["Foo".into(),], vec!["Bar".into(),],]
272+
)
273+
);
274+
}
275+
276+
#[test]
277+
fn test_list_wide_column_wide_mode() {
278+
let config = OutputConfig {
279+
fields: BTreeSet::from(["Long".into()]),
280+
wide: true,
281+
pretty: false,
282+
};
283+
let users = vec![
284+
User {
285+
id: 1,
286+
first_name: "Scooby".into(),
287+
last_name: "Doo".into(),
288+
extra: "Foo".into(),
289+
complex_data: Some(json!({"a": "b", "c": "d"})),
290+
dummy: None,
291+
},
292+
User {
293+
id: 2,
294+
first_name: "John".into(),
295+
last_name: "Cena".into(),
296+
extra: "Bar".into(),
297+
complex_data: None,
298+
dummy: Some("foo".into()),
299+
},
300+
];
301+
302+
let data = users.build(&config);
303+
assert_eq!(
304+
data,
305+
(
306+
vec!["Long".into(),],
307+
vec![vec!["Foo".into(),], vec!["Bar".into(),],]
308+
)
309+
);
310+
}
311+
190312
#[test]
191313
fn test_list_wide() {
192314
let config = OutputConfig {

0 commit comments

Comments
 (0)