Add support for opening without filename arg

This allows the user to open notepad_squared without needing to pass a
filename as an argument, however, we currently don't support saving
files that have been opened in this manor
open-files
Ronald 7 months ago
parent 97d8f60810
commit 69169f7ca2

@ -16,11 +16,6 @@ main :: proc() {
args := os.args[1:] args := os.args[1:]
if len(args) == 0 {
fmt.println("Usage: gui_cat <file1> [file2] [file3] ...")
return
}
config, ok := parse_config("config.ini") config, ok := parse_config("config.ini")
if !ok { if !ok {
log.fatal("failed to parse config file") log.fatal("failed to parse config file")
@ -55,8 +50,12 @@ main :: proc() {
load_fonts(&app) load_fonts(&app)
// Load files from command line arguments // Load files from command line arguments
for filename in args { if len(args) > 1 {
load_file(&app, filename) for filename in args {
load_file(&app, filename)
}
} else {
load_file(&app)
} }
if len(app.files) == 0 { if len(app.files) == 0 {
@ -91,20 +90,32 @@ load_fonts :: proc(app: ^App) {
app.line_height = app.fonts[app.current_font_index].size + 4 app.line_height = app.fonts[app.current_font_index].size + 4
} }
load_file :: proc(app: ^App, filename: string) { load_file :: proc(app: ^App, filename: string = "") {
data, ok := os.read_entire_file(filename) buffer: FileBuffer
if !ok { if len(filename) == 0 {
fmt.printf("Failed to read file: %s\n", filename) buffer = FileBuffer{
return 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 := FileBuffer{ buffer = FileBuffer{
filename = strings.clone(filename), filename = strings.clone(filename),
content = string(data), content = string(data),
modified = false, modified = false,
cursor_pos = 0, cursor_pos = 0,
scroll_y = 0, scroll_y = 0,
selection = Selection{start = 0, end = 0, active = false}, selection = Selection{start = 0, end = 0, active = false},
}
} }
append(&app.files, buffer) append(&app.files, buffer)
@ -836,9 +847,13 @@ move_cursor_to_end_of_line :: proc(file: ^FileBuffer) {
} }
save_file :: proc(file: ^FileBuffer) { save_file :: proc(file: ^FileBuffer) {
if file.filename == "" {
fmt.println("No filename, currently this feature is not supported, sorry :-(")
return
}
success := os.write_entire_file(file.filename, transmute([]u8)file.content) success := os.write_entire_file(file.filename, transmute([]u8)file.content)
if success { if success {
file.modified = false // file.modified = false
fmt.printf("Saved: %s\n", file.filename) fmt.printf("Saved: %s\n", file.filename)
} else { } else {
fmt.printf("Failed to save: %s\n", file.filename) fmt.printf("Failed to save: %s\n", file.filename)

Loading…
Cancel
Save