Skip to content

Commit

Permalink
Writing new colors source file
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 12, 2024
1 parent ecbb3f1 commit 32a1d97
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 45 deletions.
7 changes: 1 addition & 6 deletions notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
* Features
** Implement new flags
** Ignore source lines
** Log file
** Documentation
** Tidy up code
** Find todos
** Better file backup
* Bugs
** Better errors including line numbers in source file
** Handle list items and comments properly
** Handle errors due to unwrappable lines
** Better color printing
** Colors in logging errors, etc
** Don't repeat long line warning
** Only print errors once, handle better
** Try to allocate less memory
6 changes: 6 additions & 0 deletions src/colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const CYAN: &str = "\x1b[36m\x1b[1m";
pub const PINK: &str = "\x1b[35m\x1b[1m";
pub const RED: &str = "\x1b[31m\x1b[1m";
pub const RESET: &str = "\x1b[00m\x1b[0m";
pub const WHITE: &str = "\x1b[37m\x1b[1m";
pub const YELLOW: &str = "\x1b[33m\x1b[1m";
11 changes: 8 additions & 3 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ use crate::subs::*;
use crate::wrap::*;
use crate::Cli;

const MAX_WRAP_TRY: u8 = 5;
const MAX_WRAP_TRY: u8 = 10;

fn apply_wraps(file: &str, args: &Cli) -> String {
let mut wrap_tries = 0;
let mut new_file = file.to_string();
let mut old_file: String = "".to_string();
let mut warn_string: Option<String> = None;
while needs_wrap(&new_file) && wrap_tries < MAX_WRAP_TRY {
log::info!("Pass number {}", wrap_tries);
while needs_wrap(&new_file)
&& wrap_tries < MAX_WRAP_TRY
&& new_file != old_file
{
log::info!("Wrapping pass number {}", wrap_tries + 1);
old_file = new_file.clone();
wrap_tries += 1;
(new_file, warn_string) = wrap(&new_file);
new_file = remove_trailing_spaces(&new_file);
Expand Down
28 changes: 16 additions & 12 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::colors::*;
use crate::comments::*;
use crate::regexes::*;
use crate::Cli;
Expand Down Expand Up @@ -98,23 +99,16 @@ fn get_back(line: &str) -> i8 {
back
}

fn get_indent(line: &str, linum: usize, prev_indent: Indent) -> Indent {
fn get_indent(line: &str, prev_indent: Indent) -> Indent {
let diff = get_diff(line);
let back = get_back(line);
let actual = prev_indent.actual + diff;
let visual = prev_indent.actual - back;
log::info!(
"Indent line {}: diff = {}, actual = {}, visual = {}: {:.20}",
linum,
diff,
actual,
visual,
line
);
Indent { actual, visual }
}

pub fn apply_indent(file: &str, args: &Cli) -> String {
log::info!("Indenting file");
let mut indent = Indent::new();
let mut new_file = "".to_owned();
let mut verbatim_count = 0;
Expand All @@ -127,20 +121,30 @@ pub fn apply_indent(file: &str, args: &Cli) -> String {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, i, indent);
indent = get_indent(line_strip, indent);
log::info!(
"Indent line {}: actual = {}, visual = {}:{} {}",
i,
indent.actual,
indent.visual,
WHITE,
line,
);
if !args.debug {
if indent.actual < 0 {
log::error!(
"Actual indent is negative: line {}: {:.20}",
"Actual indent negative on line {}: {}{}",
i,
WHITE,
line
);
indent.actual = 0;
}
if indent.visual < 0 {
log::error!(
"Visual indent is negative: line {}: {:.20}",
"Visual indent negative on line {}: {}{}",
i,
WHITE,
line
);
indent.visual = 0;
Expand Down
6 changes: 1 addition & 5 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::colors::*;
use crate::Builder;
use crate::Cli;
use log::Level;
use log::LevelFilter;
use std::io::Write;

const RED: &str = "\x1b[31m\x1b[1m";
const CYAN: &str = "\x1b[36m\x1b[1m";
const YELLOW: &str = "\x1b[33m\x1b[1m";
const RESET: &str = "\x1b[00m\x1b[0m";

fn get_log_style(log_level: Level) -> String {
match log_level {
Level::Info => CYAN.to_string(),
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::fs;

const TAB: i8 = 2;

mod colors;
mod comments;
mod format;
mod indent;
Expand Down Expand Up @@ -56,7 +57,7 @@ fn main() {
print_script_name();

for filename in &args.filenames {
if args.debug {
if args.verbose {
print_file_name(filename);
}

Expand Down
4 changes: 1 addition & 3 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const PINK: &str = "\x1b[35m\x1b[1m";
const RESET: &str = "\x1b[00m\x1b[0m";
const YELLOW: &str = "\x1b[33m\x1b[1m";
use crate::colors::*;

pub fn print_script_name() {
println!("{}", String::new() + PINK + "tex-fmt" + RESET);
Expand Down
6 changes: 1 addition & 5 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
mod tests {

use crate::apply;
use crate::colors::*;
use crate::format_file;
use crate::rstest;
use crate::template;
use crate::Cli;
use std::fs;

const YELLOW: &str = "\x1b[33m\x1b[1m";
const RED: &str = "\x1b[31m\x1b[1m";
const WHITE: &str = "\x1b[37m\x1b[1m";
const RESET: &str = "\x1b[00m\x1b[0m";

#[template]
#[rstest]
#[case::brackets("brackets", "tex")]
Expand Down
21 changes: 11 additions & 10 deletions src/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::colors::*;
use crate::comments::*;
use crate::regexes::*;

Expand Down Expand Up @@ -28,8 +29,8 @@ fn find_wrap_point(line: &str) -> Option<usize> {
wrap_point
}

fn wrap_line(line: &str) -> (String, Option<String>) {
log::info!("Wrap long line: {:.50}...", line);
fn wrap_line(line: &str, linum: usize) -> (String, Option<String>) {
log::info!("Wrap long line: {}{}", WHITE, line);
let mut remaining_line = line.to_string();
let mut new_line = "".to_string();
let mut can_wrap = true;
Expand Down Expand Up @@ -58,8 +59,8 @@ fn wrap_line(line: &str) -> (String, Option<String>) {
None => {
can_wrap = false;
warn_string = Some(format!(
"Long line cannot be wrapped: {:.50}...",
remaining_line
"Line {} cannot be wrapped: {}{}",
linum, WHITE, remaining_line,
));
}
}
Expand All @@ -73,12 +74,12 @@ pub fn wrap(file: &str) -> (String, Option<String>) {
let mut new_line: String;
let mut verbatim_count = 0;
let mut warn_string: Option<String> = None;
for line in file.lines() {
for (i, line) in file.lines().enumerate() {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
if line_needs_wrap(line) && verbatim_count == 0 {
(new_line, warn_string) = wrap_line(line);
(new_line, warn_string) = wrap_line(line, i);
new_file.push_str(&new_line);
} else {
new_file.push_str(line);
Expand All @@ -99,22 +100,22 @@ fn test_wrap_line() {
Therefore it should be split.";
let s_out = "This line is too long because it has more than eighty characters inside it.\n \
Therefore it should be split.";
assert_eq!(wrap_line(s_in).0, s_out);
assert_eq!(wrap_line(s_in, 0).0, s_out);
// break before comment
let s_in = "This line is too long because it has more than eighty characters inside it. \
Therefore it % should be split.";
let s_out = "This line is too long because it has more than eighty characters inside it.\n \
Therefore it % should be split.";
assert_eq!(wrap_line(s_in).0, s_out);
assert_eq!(wrap_line(s_in, 0).0, s_out);
// break after comment
let s_in = "This line is too long because % it has more than eighty characters inside it. \
Therefore it should be split.";
let s_out = "This line is too long because % it has more than eighty characters inside it.\n\
% Therefore it should be split.";
assert_eq!(wrap_line(s_in).0, s_out);
assert_eq!(wrap_line(s_in, 0).0, s_out);
// leading spaces
let s_in = " Thislineistoolongbecauseithasmorethaneightycharactersinsideiteventhoughitstartswithspaces. \
Thereforeitshouldbesplit.";
let s_out = s_in;
assert_eq!(wrap_line(s_in).0, s_out);
assert_eq!(wrap_line(s_in, 0).0, s_out);
}

0 comments on commit 32a1d97

Please sign in to comment.