From fe37b30e4a7321776621c91cacd3b58fb4247044 Mon Sep 17 00:00:00 2001 From: cflip Date: Thu, 19 Jan 2023 10:10:23 -0700 Subject: Show filename and current working directory in window title --- editor.c | 1 + file.c | 2 ++ window.c | 15 +++++++++++++++ window.h | 1 + 4 files changed, 19 insertions(+) diff --git a/editor.c b/editor.c index f7fc684..b83ad26 100644 --- a/editor.c +++ b/editor.c @@ -28,6 +28,7 @@ void init_editor(struct editor_state* editor) editor->mode = EDITOR_MODE_NORMAL; editor_update_screen_size(editor); + window_set_filename("[New]"); } void editor_set_status_message(struct editor_state* editor, const char* format, ...) diff --git a/file.c b/file.c index 606fcc3..88f789e 100644 --- a/file.c +++ b/file.c @@ -42,6 +42,7 @@ void editor_open(struct editor_state* editor, char* filename) memcpy(editor->filename, filename, filename_len); editor_select_syntax_highlight(editor); + window_set_filename(filename); /* If there is no file with this name, the editor will create it on save. */ if (access(filename, F_OK) != 0) @@ -78,6 +79,7 @@ void editor_save(struct editor_state* editor) return; editor_select_syntax_highlight(editor); + window_set_filename(editor->filename); } int length; diff --git a/window.c b/window.c index 031caef..bbd7d2f 100644 --- a/window.c +++ b/window.c @@ -1,5 +1,6 @@ #include "window.h" +#include #include #include "buffer.h" @@ -177,6 +178,20 @@ void window_redraw(struct editor_state *editor) SDL_UpdateWindowSurface(window); } +void window_set_filename(const char *filename) +{ +#define TITLE_BUFSIZE 128 +#define WORKDIR_BUFSIZE 128 + + char titlebuf[TITLE_BUFSIZE]; + char cwdbuf[WORKDIR_BUFSIZE]; + char *workdir; + workdir = getcwd(cwdbuf, WORKDIR_BUFSIZE); + + snprintf(titlebuf, TITLE_BUFSIZE, "%s - (%s)", filename, workdir); + SDL_SetWindowTitle(window, titlebuf); +} + void window_get_size(int *rows, int *cols) { *cols = window_width / font.width; diff --git a/window.h b/window.h index a414f6f..a5fa195 100644 --- a/window.h +++ b/window.h @@ -6,6 +6,7 @@ struct editor_state; void window_init(const char *title, int rows, int cols); int window_handle_event(struct editor_state *editor); void window_redraw(struct editor_state *editor); +void window_set_filename(const char *filename); void window_get_size(int *rows, int *cols); void window_destroy(); -- cgit v1.2.3