1use crate::color;
2use crate::utils;
3use colored::*;
4
5const DEFAULT_LINE_ABOVE: i64 = 2;
7const DEFAULT_START_LINE: i64 = 1;
8const MAX_CODEFRAME_LENGTH: usize = 100;
9const MIN_CODEFRAME_LENGTH: usize = 5;
10
11pub fn capture_codeframe(lines: Vec<String>, line: i64, color: crate::Color) -> Option<String> {
12 let mut result = String::new();
13 let call_line = line;
14 let mut start_loc: i64 = call_line - DEFAULT_LINE_ABOVE;
15
16 if start_loc <= 0 {
17 start_loc = DEFAULT_START_LINE
18 }
19
20 let file_length = lines.len() as i64;
21
22 let mut position = start_loc;
23 let mut start_coloring = false;
24 let mut paranthesis_count = 0;
25 let code_frame = &lines[(start_loc - 1) as usize..];
26 for line in code_frame {
27 let mut formatted_line = format!("{} | {}", position, line);
28 if position == call_line {
29 paranthesis_count += utils::count_paranthesis(line);
30 if paranthesis_count != 0 {
31 start_coloring = true;
32 }
33 formatted_line = color::color_line(&formatted_line, color);
34 } else if start_coloring {
35 paranthesis_count += utils::count_paranthesis(line);
36 if paranthesis_count == 0 {
37 start_coloring = false;
38 }
39 formatted_line = color::color_line(&formatted_line, color).to_string();
40 } else {
41 if utils::count_newlines(&result) >= MIN_CODEFRAME_LENGTH {
42 break;
43 }
44 formatted_line = formatted_line.dimmed().to_string();
45 if position > file_length {
46 break;
47 }
48 }
49 result.push_str(formatted_line.as_str());
50 result.push_str("\n");
51 position += 1;
52
53 if utils::count_newlines(&result) >= MAX_CODEFRAME_LENGTH {
54 break;
55 }
56 }
57
58 Some(result)
59}