@ -2,13 +2,16 @@ package main
import "core:fmt"
import "core:log"
import "core:math"
import "core:os"
import "core:slice"
import "core:strings"
import "core:thread"
import "core:time"
import "core:math"
import rl "vendor:raylib"
import "file_dialog"
main :: proc() {
context.logger = log.create_console_logger(.Debug)
@ -16,11 +19,6 @@ main :: proc() {
args := os.args[1:]
if len(args) == 0 {
fmt.println("Usage: gui_cat <file1> [file2] [file3] ...")
return
}
config, ok := parse_config("config.ini")
if !ok {
log.fatal("failed to parse config file")
@ -29,7 +27,7 @@ main :: proc() {
// Initialize application
app := App{
files = make([dynamic]FileBuffer),
files = make([dynamic]File_ Buffer),
fonts = make([dynamic]FontConfig),
active_tab = 0,
current_font_index = 0,
@ -55,12 +53,16 @@ main :: proc() {
load_fonts(&app)
// Load files from command line arguments
if len(args) > 1 {
for filename in args {
load_file(&app, filename)
create_file_buffer(&app, filename)
}
} else {
create_file_buffer(&app)
}
if len(app.files) == 0 {
fmt.println("N o files could be loaded")
log.error("n o files could be loaded")
rl.CloseWindow()
return
}
@ -73,141 +75,15 @@ main :: proc() {
// Main loop
for !rl.WindowShouldClose() {
update(&app)
draw(&app)
}
}
load_fonts :: proc(app: ^App) {
// TODO: Support loading multiple fonts
// TODO: Allow searching for a font based on it's name and search fc-list database
font := FontConfig{
font = rl.LoadFont(strings.clone_to_cstring(app.config.font)),
size = f32(app.config.font_size),
name = "Default",
}
append(&app.fonts, font)
app.line_height = app.fonts[app.current_font_index].size + 4
}
load_file :: proc(app: ^App, filename: string) {
data, ok := os.read_entire_file(filename)
if !ok {
fmt.printf("Failed to read file: %s\n", filename)
return
}
buffer := FileBuffer{
filename = strings.clone(filename),
content = string(data),
modified = false,
cursor_pos = 0,
scroll_y = 0,
selection = Selection{start = 0, end = 0, active = false},
}
append(&app.files, buffer)
}
get_wrapped_lines :: proc(content: string, font: ^FontConfig, max_width: f32) -> []WrappedLine {
lines := strings.split(content, "\n")
defer delete(lines)
wrapped_lines := make([dynamic]WrappedLine)
character_offset := 0
for line, line_index in lines {
if len(line) == 0 {
// Empty line
append(&wrapped_lines, WrappedLine{
text = "",
original_line = line_index,
character_offset = character_offset,
})
} else {
// Wrap long lines
current_pos := 0
for current_pos < len(line) {
// Find how many characters fit in the width
fit_characters := 0
current_width: f32 = 0
for i in current_pos..<len(line) {
character_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(fmt.tprint(rune(line[i]))), font.size, 1).x
if current_width + character_width > max_width && fit_characters > 0 {
break
}
current_width += character_width
fit_characters += 1
}
// If we couldn't fit even one character, force at least one
if fit_characters == 0 {
fit_characters = 1
}
// Extract the text that fits
end_pos := min(current_pos + fit_characters, len(line))
wrapped_text := line[current_pos:end_pos]
append(&wrapped_lines, WrappedLine{
text = wrapped_text,
original_line = line_index,
character_offset = character_offset + current_pos,
})
current_pos = end_pos
}
}
character_offset += len(line) + 1 // +1 for newline
}
return wrapped_lines[:]
}
get_character_at_position :: proc(app: ^App, mouse_pos: rl.Vector2) -> int {
if len(app.files) == 0 || app.active_tab >= len(app.files) do return 0
file := &app.files[app.active_tab]
font := &app.fonts[app.current_font_index]
content_area_x := 10 + app.line_number_width
content_area_y := app.text_area_y
content_width := f32(app.window_width - 20) - app.line_number_width - 10
if mouse_pos.x < content_area_x || mouse_pos.y < content_area_y do return 0
wrapped_lines := get_wrapped_lines(file.content, font, content_width)
defer delete(wrapped_lines)
y_pos := content_area_y - file.scroll_y + 5
for wrapped_line, i in wrapped_lines {
if mouse_pos.y >= y_pos && mouse_pos.y < y_pos + app.line_height {
// Found the line, now find the character
x_offset := mouse_pos.x - content_area_x - 5
rl.BeginDrawing()
rl.ClearBackground(app.config.colours.background)
if x_offset <= 0 {
return wrapped_line.character_offset
for file in app.files {
if app.thread_finished && thread.is_done(app.thread) {
thread.destroy(app.thread)
}
for j in 0..=len(wrapped_line.text) {
text_slice := wrapped_line.text[:j]
text_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(text_slice), font.size, 1).x
if x_offset <= text_width {
return wrapped_line.character_offset + j
}
}
return wrapped_line.character_offset + len(wrapped_line.text)
}
y_pos += app.line_height
}
return len(file.content)
}
update :: proc(app: ^App) {
// Handle window resize
if rl.IsWindowResized() {
app.window_width = rl.GetScreenWidth()
@ -226,6 +102,7 @@ update :: proc(app: ^App) {
}
}
if !app.file_dialog_open {
// Handle text selection with mouse
if len(app.files) > 0 && app.active_tab < len(app.files) {
current_file := &app.files[app.active_tab]
@ -233,7 +110,7 @@ update :: proc(app: ^App) {
if rl.IsMouseButtonPressed(.LEFT) {
if mouse_pos.y > app.text_area_y {
character_pos := get_character_at_position(app, mouse_pos)
character_pos := get_character_at_position(& app, mouse_pos)
current_file.cursor_pos = character_pos
current_file.selection.start = character_pos
current_file.selection.end = character_pos
@ -245,7 +122,7 @@ update :: proc(app: ^App) {
if app.is_dragging && rl.IsMouseButtonDown(.LEFT) {
if mouse_pos.y > app.text_area_y {
character_pos := get_character_at_position(app, mouse_pos)
character_pos := get_character_at_position(& app, mouse_pos)
current_file.cursor_pos = character_pos
if character_pos != app.mouse_drag_start {
current_file.selection.start = min(app.mouse_drag_start, character_pos)
@ -386,13 +263,16 @@ update :: proc(app: ^App) {
// Handle keyboard shortcuts
if rl.IsKeyDown(.LEFT_CONTROL) || rl.IsKeyDown(.RIGHT_CONTROL) {
if rl.IsKeyPressed(.S) {
save_file(current_file)
app.thread = thread.create_and_start_with_poly_data2(&app, current_file, save_file)
}
if rl.IsKeyPressed(.O) {
app.thread = thread.create_and_start_with_poly_data(&app, open_file)
}
if rl.IsKeyPressed(.A) {
select_all(current_file)
}
if rl.IsKeyPressed(.C) {
copy_selection(app, current_file)
copy_selection(& app, current_file)
}
if rl.IsKeyPressed(.V) {
paste_text(current_file, app.copied_text)
@ -414,24 +294,193 @@ update :: proc(app: ^App) {
}
}
}
}
draw :: proc(app: ^App) {
rl.BeginDrawing()
rl.ClearBackground(app.config.colours.background)
}
// Draw tabs
draw_tabs(app)
draw_tabs(& app)
// Draw active file content
if len(app.files) > 0 && app.active_tab < len(app.files) {
draw_file_content(app, &app.files[app.active_tab])
draw_file_content(& app, &app.files[app.active_tab])
}
// Draw status bar
draw_status_bar(app)
draw_status_bar(& app)
rl.EndDrawing()
}
}
load_fonts :: proc(app: ^App) {
// TODO: Support loading multiple fonts
// TODO: Allow searching for a font based on it's name and search fc-list database
font := FontConfig{
font = rl.LoadFont(strings.clone_to_cstring(app.config.font)),
size = f32(app.config.font_size),
name = "Default",
}
append(&app.fonts, font)
app.line_height = app.fonts[app.current_font_index].size + 4
}
create_file_buffer :: proc(app: ^App, filename: string = "") -> (ok: bool) {
ok = false
buffer: File_Buffer
if len(filename) == 0 {
buffer = File_Buffer{
filename = "",
content = "",
modified = false,
cursor_pos = 0,
scroll_y = 0,
selection = Selection{start = 0, end = 0, active = false},
}
} else {
data, ok := os.read_entire_file(filename)
if !ok {
fmt.printf("Failed to read file: %s\n", filename)
return
}
buffer = File_Buffer{
filename = strings.clone(filename),
content = string(data),
modified = false,
cursor_pos = 0,
scroll_y = 0,
selection = Selection{start = 0, end = 0, active = false},
}
}
append(&app.files, buffer)
ok = true
return
}
open_file :: proc(app: ^App) {
app.file_dialog_open = true
result: file_dialog.Result
result = file_dialog.show_open_file_dialog()
app.file_dialog_open = false
if !result.success {
log.error(result.error_message)
return
}
defer file_dialog.destroy_result(&result)
ok := create_file_buffer(app, result.file_path)
if !ok {
log.error("failed ot create file buffer")
return
}
app.active_tab+=1
return
}
get_wrapped_lines :: proc(content: string, font: ^FontConfig, max_width: f32) -> []WrappedLine {
lines := strings.split(content, "\n")
defer delete(lines)
wrapped_lines := make([dynamic]WrappedLine)
character_offset := 0
for line, line_index in lines {
if len(line) == 0 {
// Empty line
append(&wrapped_lines, WrappedLine{
text = "",
original_line = line_index,
character_offset = character_offset,
})
} else {
// Wrap long lines
current_pos := 0
for current_pos < len(line) {
// Find how many characters fit in the width
fit_characters := 0
current_width: f32 = 0
for i in current_pos..<len(line) {
character_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(fmt.tprint(rune(line[i]))), font.size, 1).x
if current_width + character_width > max_width && fit_characters > 0 {
break
}
current_width += character_width
fit_characters += 1
}
// If we couldn't fit even one character, force at least one
if fit_characters == 0 {
fit_characters = 1
}
// Extract the text that fits
end_pos := min(current_pos + fit_characters, len(line))
wrapped_text := line[current_pos:end_pos]
append(&wrapped_lines, WrappedLine{
text = wrapped_text,
original_line = line_index,
character_offset = character_offset + current_pos,
})
current_pos = end_pos
}
}
character_offset += len(line) + 1 // +1 for newline
}
return wrapped_lines[:]
}
get_character_at_position :: proc(app: ^App, mouse_pos: rl.Vector2) -> int {
if len(app.files) == 0 || app.active_tab >= len(app.files) do return 0
file := &app.files[app.active_tab]
font := &app.fonts[app.current_font_index]
content_area_x := 10 + app.line_number_width
content_area_y := app.text_area_y
content_width := f32(app.window_width - 20) - app.line_number_width - 10
if mouse_pos.x < content_area_x || mouse_pos.y < content_area_y do return 0
wrapped_lines := get_wrapped_lines(file.content, font, content_width)
defer delete(wrapped_lines)
y_pos := content_area_y - file.scroll_y + 5
for wrapped_line, i in wrapped_lines {
if mouse_pos.y >= y_pos && mouse_pos.y < y_pos + app.line_height {
// Found the line, now find the character
x_offset := mouse_pos.x - content_area_x - 5
if x_offset <= 0 {
return wrapped_line.character_offset
}
for j in 0..=len(wrapped_line.text) {
text_slice := wrapped_line.text[:j]
text_width := rl.MeasureTextEx(font.font, strings.clone_to_cstring(text_slice), font.size, 1).x
if x_offset <= text_width {
return wrapped_line.character_offset + j
}
}
return wrapped_line.character_offset + len(wrapped_line.text)
}
y_pos += app.line_height
}
return len(file.content)
}
draw :: proc(app: ^App) {
}
draw_tabs :: proc(app: ^App) {
@ -479,7 +528,7 @@ draw_tabs :: proc(app: ^App) {
}
}
draw_file_content :: proc(app: ^App, file: ^FileBuffer) {
draw_file_content :: proc(app: ^App, file: ^File_ Buffer) {
font := &app.fonts[app.current_font_index]
// Content area (excluding line numbers)
@ -657,7 +706,7 @@ draw_status_bar :: proc(app: ^App) {
}
}
insert_character :: proc(file: ^FileBuffer, character: rune) {
insert_character :: proc(file: ^File_ Buffer, character: rune) {
if file.cursor_pos <= len(file.content) {
before := file.content[:file.cursor_pos]
after := file.content[file.cursor_pos:]
@ -668,7 +717,7 @@ insert_character :: proc(file: ^FileBuffer, character: rune) {
}
}
delete_character :: proc(file: ^FileBuffer, delete_backwards: bool = false) {
delete_character :: proc(file: ^File_ Buffer, delete_backwards: bool = false) {
if file.cursor_pos > 0 && !delete_backwards {
before := file.content[:file.cursor_pos-1]
after := file.content[file.cursor_pos:]
@ -685,7 +734,7 @@ delete_character :: proc(file: ^FileBuffer, delete_backwards: bool = false) {
}
}
delete_selection :: proc(file: ^FileBuffer) {
delete_selection :: proc(file: ^File_ Buffer) {
if !file.selection.active do return
start := min(file.selection.start, file.selection.end)
@ -699,20 +748,20 @@ delete_selection :: proc(file: ^FileBuffer) {
clear_selection(file)
}
clear_selection :: proc(file: ^FileBuffer) {
clear_selection :: proc(file: ^File_ Buffer) {
file.selection.active = false
file.selection.start = 0
file.selection.end = 0
}
select_all :: proc(file: ^FileBuffer) {
select_all :: proc(file: ^File_ Buffer) {
file.selection.start = 0
file.selection.end = len(file.content)
file.selection.active = true
file.cursor_pos = len(file.content)
}
copy_selection :: proc(app: ^App, file: ^FileBuffer) {
copy_selection :: proc(app: ^App, file: ^File_ Buffer) {
if !file.selection.active do return
start := min(file.selection.start, file.selection.end)
@ -723,7 +772,7 @@ copy_selection :: proc(app: ^App, file: ^FileBuffer) {
rl.SetClipboardText(strings.clone_to_cstring(app.copied_text))
}
paste_text :: proc(file: ^FileBuffer, text: string) {
paste_text :: proc(file: ^File_ Buffer, text: string) {
if file.selection.active {
delete_selection(file)
}
@ -740,7 +789,7 @@ paste_text :: proc(file: ^FileBuffer, text: string) {
}
}
move_cursor_up :: proc(file: ^FileBuffer) {
move_cursor_up :: proc(file: ^File_ Buffer) {
// For wrapped text, we need to handle cursor movement differently
if file.cursor_pos > 0 {
// Find the previous newline
@ -767,7 +816,7 @@ move_cursor_up :: proc(file: ^FileBuffer) {
}
}
move_cursor_down :: proc(file: ^FileBuffer) {
move_cursor_down :: proc(file: ^File_ Buffer) {
// For wrapped text, we need to handle cursor movement differently
if file.cursor_pos < len(file.content) {
// Find the current line start
@ -803,7 +852,7 @@ move_cursor_down :: proc(file: ^FileBuffer) {
}
}
move_cursor_to_start_of_line :: proc(file: ^FileBuffer) {
move_cursor_to_start_of_line :: proc(file: ^File_ Buffer) {
// For wrapped text, we need to handle cursor movement differently
if file.cursor_pos < len(file.content) {
// Find the current line start
@ -819,7 +868,7 @@ move_cursor_to_start_of_line :: proc(file: ^FileBuffer) {
}
}
move_cursor_to_end_of_line :: proc(file: ^FileBuffer) {
move_cursor_to_end_of_line :: proc(file: ^File_ Buffer) {
// For wrapped text, we need to handle cursor movement differently
if file.cursor_pos < len(file.content) {
// Find the next newline
@ -835,13 +884,26 @@ move_cursor_to_end_of_line :: proc(file: ^FileBuffer) {
}
}
save_file :: proc(file: ^FileBuffer) {
success := os.write_entire_file(file.filename, transmute([]u8)file.content)
if success {
file.modified = false
fmt.printf("Saved: %s\n", file.filename)
} else {
fmt.printf("Failed to save: %s\n", file.filename)
save_file :: proc(app: ^App, file: ^File_Buffer) {
app.thread_finished = false
if len(file.filename) == 0 {
app.file_dialog_open = true
result := file_dialog.show_save_file_dialog()
app.file_dialog_open = false
if !result.success {
log.error(result.error_message)
}
defer file_dialog.destroy_result(&result)
file.filename = strings.clone(result.file_path)
}
ok := os.write_entire_file(file.filename, transmute([]u8)file.content)
file.modified = !ok
if !ok {
log.error("failed to save file, \"", file.filename, "\"", sep="") // TODO: Show popup window that displays this to the user
return
}
app.thread_finished = true
}