Last active
December 29, 2023 15:26
-
-
Save RaphGL/0e335c8718ea79c0097023d495622d2f to your computer and use it in GitHub Desktop.
Bug printing textbuffer window with ncurses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "core:c" | |
import "core:fmt" | |
import "core:os" | |
import "core:slice" | |
import "core:strings" | |
import nc "ncurses/src" | |
TextBuffer :: struct { | |
win: ^nc.Window, | |
rows: [dynamic]string, | |
col, row: c.int, | |
} | |
textbuffer_new :: proc(filepath: string) -> (tb: TextBuffer, success: bool) { | |
file_contents: []string | |
if filepath != "" { | |
data := os.read_entire_file(filepath) or_return | |
file_contents = strings.split(string(data), "\n") | |
} | |
h, w := nc.getmaxyx(nc.stdscr) | |
bufwin := nc.newwin(h - 10, w, 0, 0) | |
return TextBuffer{rows = slice.clone_to_dynamic(file_contents), win = bufwin}, true | |
} | |
textbuffer_free :: proc(tb: TextBuffer) -> bool { | |
nc.werase(tb.win) | |
nc.delwin(tb.win) | |
return delete(tb.rows) == .None | |
} | |
main :: proc() { | |
tb, success := textbuffer_new(os.args[1] if len(os.args) > 1 else "") | |
if !success { | |
fmt.eprintln("Failed to load buffer from %s.", os.args[1]) | |
return | |
} | |
defer textbuffer_free(tb) | |
nc.initscr() | |
nc.noecho() | |
nc.curs_set(0) | |
nc.cbreak() | |
nc.refresh() | |
defer nc.endwin() | |
for { | |
nc.erase() | |
nc.werase(tb.win) | |
for row in tb.rows { | |
nc.wprintw(tb.win, "%s\n", row) | |
} | |
nc.wrefresh(tb.win) | |
nc.refresh() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment