]> granicus.if.org Git - neomutt/commitdiff
Refactor resize.c into separate functions
authorIvan J <parazyd@users.noreply.github.com>
Tue, 19 Jun 2018 14:15:13 +0000 (16:15 +0200)
committerRichard Russon <rich@flatcap.org>
Tue, 19 Jun 2018 14:15:13 +0000 (15:15 +0100)
Splits up mutt_resize_screen() into its separate function to improve readability.

Closes #1149

resize.c

index 51a2bc0f8e9e87130ad25e40286181665c8c2296..48b782834e23f528842d43a4d0bdd9eceab0e9d5 100644 (file)
--- a/resize.c
+++ b/resize.c
@@ -4,6 +4,7 @@
  *
  * @authors
  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
+ * Copyright (C) 2018      Ivan J. <parazyd@dyne.org>
  *
  * @copyright
  * This program is free software: you can redistribute it and/or modify it under
 #endif
 #endif
 
-/**
- * mutt_resize_screen - Called after receiving SIGWINCH
- */
-void mutt_resize_screen(void)
+struct winsize mutt_get_winsize(void)
 {
-  const char *cp = NULL;
-  int fd;
-  struct winsize w;
-#if !defined(USE_SLANG_CURSES)
-  int SLtt_Screen_Rows, SLtt_Screen_Cols;
-#endif
-
-  SLtt_Screen_Rows = -1;
-  SLtt_Screen_Cols = -1;
-  fd = open("/dev/tty", O_RDONLY);
+  struct winsize w = { 0 };
+  int fd = open("/dev/tty", O_RDONLY);
   if (fd != -1)
   {
-    if (ioctl(fd, TIOCGWINSZ, &w) != -1)
-    {
-      SLtt_Screen_Rows = w.ws_row;
-      SLtt_Screen_Cols = w.ws_col;
-    }
+    ioctl(fd, TIOCGWINSZ, &w);
     close(fd);
   }
+  return w;
+}
+
+#ifdef USE_SLANG_CURSES
+void mutt_resize_screen(void)
+{
+  struct winsize w = mutt_get_winsize();
+
+  /* The following two variables are global to slang */
+  SLtt_Screen_Rows = w.ws_row;
+  SLtt_Screen_Cols = w.ws_col;
+
   if (SLtt_Screen_Rows <= 0)
   {
-    cp = mutt_str_getenv("LINES");
+    const char *cp = mutt_str_getenv("LINES");
     if (cp && (mutt_str_atoi(cp, &SLtt_Screen_Rows) < 0))
       SLtt_Screen_Rows = 24;
   }
+
   if (SLtt_Screen_Cols <= 0)
   {
-    cp = mutt_str_getenv("COLUMNS");
+    const char *cp = mutt_str_getenv("COLUMNS");
     if (cp && (mutt_str_atoi(cp, &SLtt_Screen_Cols) < 0))
       SLtt_Screen_Cols = 80;
   }
-#ifdef USE_SLANG_CURSES
+
   delwin(stdscr);
   SLsmg_reset_smg();
   SLsmg_init_smg();
   stdscr = newwin(0, 0, 0, 0);
   keypad(stdscr, true);
+  mutt_window_reflow();
+}
 #else
-  resizeterm(SLtt_Screen_Rows, SLtt_Screen_Cols);
-#endif
+void mutt_resize_screen(void)
+{
+  struct winsize w = mutt_get_winsize();
+
+  int screenrows = w.ws_row;
+  int screencols = w.ws_col;
+
+  if (screenrows <= 0)
+  {
+    const char *cp = mutt_str_getenv("LINES");
+    if (cp && (mutt_str_atoi(cp, &screenrows) < 0))
+      screenrows = 24;
+  }
+
+  if (screencols <= 0)
+  {
+    const char *cp = mutt_str_getenv("LINES");
+    if (cp && (mutt_str_atoi(cp, &screencols) < 0))
+      screencols = 80;
+  }
+
+  resizeterm(screenrows, screencols);
   mutt_window_reflow();
 }
+#endif