on 37b3b87 help sign in
bat/src/less.rs
62 lines 1.9 kB history raw
1use std::process::Command;
3pub fn retrieve_less_version() -> Option<usize> {
4 let cmd = Command::new("less").arg("--version").output().ok()?;
5 parse_less_version(&cmd.stdout)
6}
8fn parse_less_version(output: &[u8]) -> Option<usize> {
9 if output.starts_with(b"less ") {
10 let version = std::str::from_utf8(&output[5..]).ok()?;
11 let end = version.find(' ')?;
12 version[..end].parse::<usize>().ok()
13 } else {
14 None
15 }
18#[test]
19fn test_parse_less_version_487() {
20 let output = b"less 487 (GNU regular expressions)
21Copyright (C) 1984-2016 Mark Nudelman
23less comes with NO WARRANTY, to the extent permitted by law.
24For information about the terms of redistribution,
25see the file named README in the less distribution.
26Homepage: http://www.greenwoodsoftware.com/less";
28 assert_eq!(Some(487), parse_less_version(output));
31#[test]
32fn test_parse_less_version_529() {
33 let output = b"less 529 (Spencer V8 regular expressions)
34Copyright (C) 1984-2017 Mark Nudelman
36less comes with NO WARRANTY, to the extent permitted by law.
37For information about the terms of redistribution,
38see the file named README in the less distribution.
39Homepage: http://www.greenwoodsoftware.com/less";
41 assert_eq!(Some(529), parse_less_version(output));
44#[test]
45fn test_parse_less_version_551() {
46 let output = b"less 551 (PCRE regular expressions)
47Copyright (C) 1984-2019 Mark Nudelman
49less comes with NO WARRANTY, to the extent permitted by law.
50For information about the terms of redistribution,
51see the file named README in the less distribution.
52Home page: http://www.greenwoodsoftware.com/less";
54 assert_eq!(Some(551), parse_less_version(output));
57#[test]
58fn test_parse_less_version_wrong_program() {
59 let output = b"more from util-linux 2.34";
61 assert_eq!(None, parse_less_version(output));