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:]
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")
@ -55,8 +50,12 @@ main :: proc() {
load_fonts(&app)
// Load files from command line arguments
for filename in args {
load_file(&app, filename)
if len(args) > 1 {
for filename in args {
load_file(&app, filename)
}
} else {
load_file(&app)
}
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
}
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
}
load_file :: proc(app: ^App, filename: string = "") {
buffer: FileBuffer
if len(filename) == 0 {
buffer = FileBuffer{
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{
filename = strings.clone(filename),
content = string(data),
modified = false,
cursor_pos = 0,
scroll_y = 0,
selection = Selection{start = 0, end = 0, active = false},
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)
@ -836,9 +847,13 @@ move_cursor_to_end_of_line :: 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)
if success {
file.modified = false
// file.modified = false
fmt.printf("Saved: %s\n", file.filename)
} else {
fmt.printf("Failed to save: %s\n", file.filename)

Loading…
Cancel
Save