mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
printf: remove cli module
Remove the cli module from the printf crate and move its functions into the module tokenize::unescaped_text module, the only place they are used.
This commit is contained in:
parent
ec386fa460
commit
2d66c84413
4 changed files with 33 additions and 37 deletions
|
@ -1,23 +0,0 @@
|
||||||
//! stdio convenience fns
|
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) bslice
|
|
||||||
|
|
||||||
use std::io::{stdout, Write};
|
|
||||||
|
|
||||||
pub const EXIT_OK: i32 = 0;
|
|
||||||
pub const EXIT_ERR: i32 = 1;
|
|
||||||
|
|
||||||
// by default stdout only flushes
|
|
||||||
// to console when a newline is passed.
|
|
||||||
pub fn flush_char(c: char) {
|
|
||||||
print!("{}", c);
|
|
||||||
let _ = stdout().flush();
|
|
||||||
}
|
|
||||||
pub fn flush_str(s: &str) {
|
|
||||||
print!("{}", s);
|
|
||||||
let _ = stdout().flush();
|
|
||||||
}
|
|
||||||
pub fn flush_bytes(bslice: &[u8]) {
|
|
||||||
let _ = stdout().write(bslice);
|
|
||||||
let _ = stdout().flush();
|
|
||||||
}
|
|
|
@ -6,7 +6,6 @@ use clap::{crate_version, App, Arg};
|
||||||
use uucore::error::{UResult, UUsageError};
|
use uucore::error::{UResult, UUsageError};
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
mod cli;
|
|
||||||
mod memo;
|
mod memo;
|
||||||
mod tokenize;
|
mod tokenize;
|
||||||
|
|
||||||
|
|
|
@ -17,11 +17,12 @@ use super::num_format::format_field::{FieldType, FormatField};
|
||||||
use super::num_format::num_format;
|
use super::num_format::num_format;
|
||||||
use super::token;
|
use super::token;
|
||||||
use super::unescaped_text::UnescapedText;
|
use super::unescaped_text::UnescapedText;
|
||||||
use crate::cli;
|
|
||||||
|
const EXIT_ERR: i32 = 1;
|
||||||
|
|
||||||
fn err_conv(sofar: &str) {
|
fn err_conv(sofar: &str) {
|
||||||
show_error!("%{}: invalid conversion specification", sofar);
|
show_error!("%{}: invalid conversion specification", sofar);
|
||||||
exit(cli::EXIT_ERR);
|
exit(EXIT_ERR);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_asterisk_arg_int(asterisk_arg: &str) -> isize {
|
fn convert_asterisk_arg_int(asterisk_arg: &str) -> isize {
|
||||||
|
@ -80,7 +81,7 @@ impl Sub {
|
||||||
_ => {
|
_ => {
|
||||||
// should be unreachable.
|
// should be unreachable.
|
||||||
println!("Invalid field type");
|
println!("Invalid field type");
|
||||||
exit(cli::EXIT_ERR);
|
exit(EXIT_ERR);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Sub {
|
Sub {
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
use itertools::PutBackN;
|
use itertools::PutBackN;
|
||||||
use std::char::from_u32;
|
use std::char::from_u32;
|
||||||
|
use std::io::{stdout, Write};
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
|
@ -14,7 +15,25 @@ use std::str::Chars;
|
||||||
|
|
||||||
use super::token;
|
use super::token;
|
||||||
|
|
||||||
use crate::cli;
|
const EXIT_OK: i32 = 0;
|
||||||
|
const EXIT_ERR: i32 = 1;
|
||||||
|
|
||||||
|
// by default stdout only flushes
|
||||||
|
// to console when a newline is passed.
|
||||||
|
fn flush_char(c: char) {
|
||||||
|
print!("{}", c);
|
||||||
|
let _ = stdout().flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush_str(s: &str) {
|
||||||
|
print!("{}", s);
|
||||||
|
let _ = stdout().flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush_bytes(bslice: &[u8]) {
|
||||||
|
let _ = stdout().write(bslice);
|
||||||
|
let _ = stdout().flush();
|
||||||
|
}
|
||||||
|
|
||||||
pub struct UnescapedText(Vec<u8>);
|
pub struct UnescapedText(Vec<u8>);
|
||||||
impl UnescapedText {
|
impl UnescapedText {
|
||||||
|
@ -53,7 +72,7 @@ impl UnescapedText {
|
||||||
if found < min_chars {
|
if found < min_chars {
|
||||||
// only ever expected for hex
|
// only ever expected for hex
|
||||||
println!("missing hexadecimal number in escape"); //todo stderr
|
println!("missing hexadecimal number in escape"); //todo stderr
|
||||||
exit(cli::EXIT_ERR);
|
exit(EXIT_ERR);
|
||||||
}
|
}
|
||||||
retval
|
retval
|
||||||
}
|
}
|
||||||
|
@ -76,7 +95,7 @@ impl UnescapedText {
|
||||||
);
|
);
|
||||||
if (val < 159 && (val != 36 && val != 64 && val != 96)) || (val > 55296 && val < 57343) {
|
if (val < 159 && (val != 36 && val != 64 && val != 96)) || (val > 55296 && val < 57343) {
|
||||||
println!("{}", err_msg); //todo stderr
|
println!("{}", err_msg); //todo stderr
|
||||||
exit(cli::EXIT_ERR);
|
exit(EXIT_ERR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// pass an iterator that succeeds an '/',
|
// pass an iterator that succeeds an '/',
|
||||||
|
@ -117,7 +136,7 @@ impl UnescapedText {
|
||||||
let val = (UnescapedText::base_to_u32(min_len, max_len, base, it) % 256) as u8;
|
let val = (UnescapedText::base_to_u32(min_len, max_len, base, it) % 256) as u8;
|
||||||
byte_vec.push(val);
|
byte_vec.push(val);
|
||||||
let bvec = [val];
|
let bvec = [val];
|
||||||
cli::flush_bytes(&bvec);
|
flush_bytes(&bvec);
|
||||||
} else {
|
} else {
|
||||||
byte_vec.push(ch as u8);
|
byte_vec.push(ch as u8);
|
||||||
}
|
}
|
||||||
|
@ -145,7 +164,7 @@ impl UnescapedText {
|
||||||
'f' => '\x0C',
|
'f' => '\x0C',
|
||||||
// escape character
|
// escape character
|
||||||
'e' => '\x1B',
|
'e' => '\x1B',
|
||||||
'c' => exit(cli::EXIT_OK),
|
'c' => exit(EXIT_OK),
|
||||||
'u' | 'U' => {
|
'u' | 'U' => {
|
||||||
let len = match e {
|
let len = match e {
|
||||||
'u' => 4,
|
'u' => 4,
|
||||||
|
@ -165,7 +184,7 @@ impl UnescapedText {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
s.push(ch);
|
s.push(ch);
|
||||||
cli::flush_str(&s);
|
flush_str(&s);
|
||||||
byte_vec.extend(s.bytes());
|
byte_vec.extend(s.bytes());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -193,7 +212,7 @@ impl UnescapedText {
|
||||||
// lazy branch eval
|
// lazy branch eval
|
||||||
// remember this fn could be called
|
// remember this fn could be called
|
||||||
// many times in a single exec through %b
|
// many times in a single exec through %b
|
||||||
cli::flush_char(ch);
|
flush_char(ch);
|
||||||
tmp_str.push(ch);
|
tmp_str.push(ch);
|
||||||
}
|
}
|
||||||
'\\' => {
|
'\\' => {
|
||||||
|
@ -213,7 +232,7 @@ impl UnescapedText {
|
||||||
x if x == '%' && !subs_mode => {
|
x if x == '%' && !subs_mode => {
|
||||||
if let Some(follow) = it.next() {
|
if let Some(follow) = it.next() {
|
||||||
if follow == '%' {
|
if follow == '%' {
|
||||||
cli::flush_char(ch);
|
flush_char(ch);
|
||||||
tmp_str.push(ch);
|
tmp_str.push(ch);
|
||||||
} else {
|
} else {
|
||||||
it.put_back(follow);
|
it.put_back(follow);
|
||||||
|
@ -226,7 +245,7 @@ impl UnescapedText {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
cli::flush_char(ch);
|
flush_char(ch);
|
||||||
tmp_str.push(ch);
|
tmp_str.push(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -252,6 +271,6 @@ impl token::Tokenizer for UnescapedText {
|
||||||
}
|
}
|
||||||
impl token::Token for UnescapedText {
|
impl token::Token for UnescapedText {
|
||||||
fn print(&self, _: &mut Peekable<Iter<String>>) {
|
fn print(&self, _: &mut Peekable<Iter<String>>) {
|
||||||
cli::flush_bytes(&self.0[..]);
|
flush_bytes(&self.0[..]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue