Skip to content
Merged
Changes from 13 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
696a2fd
impl: parse_and_output_manpages
charliethomson Jul 13, 2020
9617c78
oops, fix my bad code lol
charliethomson Jul 13, 2020
eca4937
fixup! 9617c78a296389d6555983cd2b2bb00d8a0fa6eb
scooter-dangle Jul 13, 2020
f678924
add_diagnostic comment to function
charliethomson Jul 14, 2020
1f0458c
P -> PathBuf, diag for IOError
charliethomson Jul 14, 2020
855c774
Merge branch 'parse_and_output_manpages_fixed' of https://www.github.…
charliethomson Jul 14, 2020
da160a2
and_then for cmd_name
charliethomson Jul 14, 2020
1f7b988
rust fmt string padding
charliethomson Jul 14, 2020
a87d53b
fix the format string
charliethomson Jul 14, 2020
f67aa76
fix the format string again
charliethomson Jul 14, 2020
57c0602
Minor reformat, add num_digits, use fmt padding again
charliethomson Jul 14, 2020
00daf5e
Remove return statements
charliethomson Jul 14, 2020
711fc9e
Remove python
charliethomson Jul 14, 2020
51b744e
Vec -> Slice
charliethomson Jul 14, 2020
1a8590b
println! with no args
charliethomson Jul 14, 2020
d5fbeb8
log(10.) -> log10()
charliethomson Jul 14, 2020
33259a2
Merge branch 'parse_and_output_manpages_fixed' of https://www.github.…
charliethomson Jul 14, 2020
8cfe648
fixup
charliethomson Jul 14, 2020
ceffcce
fixup
charliethomson Jul 14, 2020
00611ab
fixup
charliethomson Jul 14, 2020
a08c571
clippy
charliethomson Jul 14, 2020
c69fa96
Promote num_digits, add tests
charliethomson Jul 15, 2020
ad18824
Remove tuple constructor, add enumerate
charliethomson Jul 15, 2020
9f34fd4
Make index start from 1
charliethomson Jul 15, 2020
5596c5f
Fix num_digits, add another test case
charliethomson Jul 15, 2020
0b80ab2
remove .to_mut().to_owned()
charliethomson Jul 15, 2020
c825f8d
remove Progress
charliethomson Jul 15, 2020
7aa3ab0
cmd_name String -> str
charliethomson Jul 16, 2020
634209a
move cmd_name
charliethomson Jul 16, 2020
30a5e69
.unwrap() on .split()
charliethomson Jul 17, 2020
66a918f
already mutable
charliethomson Jul 17, 2020
19e39c7
write to stdout check
charliethomson Jul 17, 2020
abc2bf6
fmt
charliethomson Jul 17, 2020
ccb6f1d
fmt
charliethomson Jul 17, 2020
34857c9
fixup!
scooter-dangle Jul 21, 2020
ca4569f
remove resolved todos
charliethomson Jul 21, 2020
beeda3a
Merge branch 'parse_and_output_manpages_fixed' of https://www.github.…
charliethomson Jul 21, 2020
d8e1d8e
Remove comment about xz/lzma, not relevant to this port, we support them
charliethomson Jul 21, 2020
10a0120
pickfire, fancy \r
charliethomson Jul 22, 2020
4f22c2c
Remove one line of whitespace
charliethomson Jul 24, 2020
8ae866d
as_ref()
charliethomson Jul 24, 2020
c72ad4d
as_ref() part 2
charliethomson Jul 24, 2020
59d309a
deas_ref, max_digits, remove initial spaces from
charliethomson Jul 24, 2020
69bca77
remove last_len, padding; use progress instead
charliethomson Jul 26, 2020
5cae975
Use display
charliethomson Jul 26, 2020
40df153
panic! one line
charliethomson Aug 1, 2020
96db160
revert
charliethomson Aug 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 109 additions & 67 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Read, Write};
use std::ops::Deref;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -1311,78 +1312,119 @@ fn parse_manpage_at_path(
}
}

// def parse_and_output_man_pages(paths, output_directory, show_progress):
// global diagnostic_indent, CMDNAME
// paths.sort()
// total_count = len(paths)
// successful_count, index = 0, 0
// padding_len = len(str(total_count))
// last_progress_string_length = 0
// if show_progress and not WRITE_TO_STDOUT:
// print("Parsing man pages and writing completions to {0}".format(output_directory))
//
// man_page_suffixes = set([os.path.splitext(m)[1][1:] for m in paths])
// lzma_xz_occurs = "xz" in man_page_suffixes or "lzma" in man_page_suffixes
// if lzma_xz_occurs and not lzma_available:
// add_diagnostic('At least one man page is compressed with lzma or xz, but the "lzma" module is not available.'
// ' Any man page compressed with either will be skipped.',
// NOT_VERBOSE)
// flush_diagnostics(sys.stderr)
//
// for manpage_path in paths:
// index += 1
//
// # Get the "base" command, e.g. gcc.1.gz -> gcc
// man_file_name = os.path.basename(manpage_path)
// CMDNAME = man_file_name.split('.', 1)[0]
// output_file_name = CMDNAME + '.fish'
//
// # Show progress if we're doing that
// if show_progress:
// progress_str = ' {0} / {1} : {2}'.format((str(index).rjust(padding_len)), total_count, man_file_name)
// # Pad on the right with spaces so we overwrite whatever we wrote last time
// padded_progress_str = progress_str.ljust(last_progress_string_length)
// last_progress_string_length = len(progress_str)
// sys.stdout.write("\r{0}\r".format(padded_progress_str))
// sys.stdout.flush()
//
// # Maybe we want to skip this item
// skip = False
// if not WRITE_TO_STDOUT:
// # Compute the path that we would write to
// output_path = os.path.join(output_directory, output_file_name)
//
// # Now skip if requested
// if skip:
// continue
//
// try:
// if parse_manpage_at_path(manpage_path, output_directory):
// successful_count += 1
// except IOError:
// diagnostic_indent = 0
// add_diagnostic('Cannot open ' + manpage_path)
// except (KeyboardInterrupt, SystemExit):
// raise
// except:
// add_diagnostic('Error parsing %s: %s' % (manpage_path, sys.exc_info()[0]), BRIEF_VERBOSE)
// flush_diagnostics(sys.stderr)
// traceback.print_exc(file=sys.stderr)
// flush_diagnostics(sys.stderr)
// print("") #Newline after loop
// add_diagnostic("Successfully parsed %d / %d pages" % (successful_count, total_count), BRIEF_VERBOSE)
// flush_diagnostics(sys.stderr)

#[derive(Copy, Clone, Debug)]
struct Progress(pub bool);

// TODO Arg/output types?
// Output: Result<cmd_name, error>
// TODO: Result<cmd_name, CompletionsError>
fn parse_and_output_man_pages(
_paths: impl Iterator<Item = PathBuf>,
_output_directory: PathBuf,
Progress(_show_progress): Progress,
paths: Vec<PathBuf>,
output_directory: PathBuf,
Progress(show_progress): Progress,
deroff_only: bool,
write_to_stdout: bool,
) {
unimplemented!();
// Get the number of digits in num
#[inline(always)]
fn num_digits(num: usize) -> usize {
(num as f32).log(10.) as usize + 1
}

let mut paths = paths;
paths.sort();
paths.dedup();

let total = paths.len();

let (mut successful_count, mut index) = (0, 0);
// TODO;
let mut cmd_name = "".to_owned();

let padding = num_digits(total);

let mut last_len: usize = 0;

if show_progress && !write_to_stdout {
println!(
"Parsing man pages and writing completions to {:?}",
output_directory
);
}

// As far as I can tell, we're fully supporting xz / lzma, so the checks here aren't needed

for manpage_path in paths {
index += 1;
// foo/bar/gcc.1.gz -> gcc.1.gz
// TODO: Expect
let man_file_name = manpage_path
.file_name()
.and_then(|fname| Some(fname.to_string_lossy().to_mut().to_owned()))
.expect(&format!(
"Failed to get manfile name from {:?}",
manpage_path
));

// gcc.1.gz -> gcc
cmd_name = man_file_name
.split('.')
.next()
.and_then(|cmd_name| Some(cmd_name.to_owned()))
.expect(&format!(
"Failed to get command name from {}",
man_file_name
));

if show_progress {
let num_spaces = padding - num_digits(index);

let progress = format!(
" {0:>1$} / {2} : {3}",
index, num_spaces, total, man_file_name,
);

// padded_progress_str = progress_str.ljust(last_progress_string_length)
let padded = format!(
"{0:<1$}",
progress,
last_len.checked_sub(progress.len()).unwrap_or(0)
);

last_len = progress.len();
// TODO: Expects
let stdout = std::io::stdout();
let mut lock = stdout.lock();
lock.write(format!("\r{}\r", padded).as_bytes())
.expect("Failed to write to stdout");

lock.flush().expect("Failed to flush stdout");
}

match parse_manpage_at_path(
manpage_path.deref(),
output_directory.as_ref(),
deroff_only,
write_to_stdout,
) {
Ok(successful) => {
if successful {
successful_count += 1
}
// Theres no else here, i assume they just pass over it lol
}
Err(e) => {
// add_diagnostic(format!("Cannot open {}", manpage_path), VERY_VERBOSE)
}
};

// flush diagnostics
}

// "Newline after loop"
println!("");

// add_diagnostic(format!("successfully parsed {} / {} pages", successful_count, total_count), BRIEF_VERBOSE);
// flush diagnostics
}

macro_rules! mantypes {
Expand Down