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