Skip to content

Commit 8656d9f

Browse files
authored
fix: tail_file read from the ending not beginning (#70)
* Update tail_file.rs: read from the ending not beginning * fix: `tail_file` return the first n lines, not last n lines `newline_positions` is already in reverse order * Update read.rs * Update test_fs_service.rs 5 is a special number, so it can pass the `test_tail_file_normal` test, I add a new line
1 parent 6378464 commit 8656d9f

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/fs_service/io/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl FileSystemService {
116116
let start_pos = if line_count <= n {
117117
0 // Read from start if fewer than n lines
118118
} else {
119-
*newline_positions.get(line_count - n).unwrap_or(&0) + 1
119+
*newline_positions.get(n-1).unwrap_or(&0) + 1
120120
};
121121

122122
// Read forward from start_pos

src/tools/tail_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::fs_service::FileSystemService;
2424
pub struct TailFile {
2525
/// The path of the file to get information for.
2626
pub path: String,
27-
/// The number of lines to read from the beginning of the file.
27+
/// The number of lines to read from the ending of the file.
2828
pub lines: u64,
2929
}
3030

tests/test_fs_service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,13 +1213,13 @@ async fn test_tail_file_normal() {
12131213
let file_path = create_test_file_with_line_ending(
12141214
&temp_dir.to_path_buf(),
12151215
"dir1/test.txt",
1216-
vec!["line1", "line2", "line3", "line4", "line5"],
1216+
vec!["line1", "line2", "line3", "line4", "line5", "line6"],
12171217
"\n",
12181218
)
12191219
.await;
12201220

12211221
let result = service.tail_file(&file_path, 3).await.unwrap();
1222-
assert_eq!(result, "line3\nline4\nline5"); // No trailing newline
1222+
assert_eq!(result, "line4\nline5\nline6"); // No trailing newline
12231223
}
12241224

12251225
#[tokio::test]

0 commit comments

Comments
 (0)