From 2db294c10c9cd1ad5cd8fb975eb39ec46e21b7d8 Mon Sep 17 00:00:00 2001
From: polikuo <michael5329208@gmail.com>
Date: Sun, 24 Nov 2024 01:22:13 +0800
Subject: [PATCH] mnttool: rename variable rewrite resizing logic for device
 names such as mmcblk0p1

---
 mnttool/mnttool.cxx |  78 ++++++++++++++++++----------------
 mnttool/mnttool.fl  | 100 ++++++++++++++++++++++++--------------------
 mnttool/mnttool.h   |   2 +-
 3 files changed, 98 insertions(+), 82 deletions(-)

diff --git a/mnttool/mnttool.cxx b/mnttool/mnttool.cxx
index 7fa8169..cb9dfd3 100644
--- a/mnttool/mnttool.cxx
+++ b/mnttool/mnttool.cxx
@@ -1,7 +1,10 @@
-// generated by Fast Light User Interface Designer (fluid) version 1.0304
+// generated by Fast Light User Interface Designer (fluid) version 1.0400
 
-#include <libintl.h>
 #include "mnttool.h"
+#include <libintl.h>
+#ifndef gettext_noop
+#  define gettext_noop(text) text
+#endif
 // (c) Robert Shingledecker 2008-2011
 #include <cstdlib>
 #include <iostream>
@@ -14,21 +17,21 @@
 #include <sys/inotify.h>
 using namespace std;
 /**
-   for watching /etc/fstab
+ for watching /etc/fstab
 */
 static int infd = -1; // inotify fd
-static void infd_read(void *); 
-static void reset_watch(); 
+static void infd_read(void *);
+static void reset_watch();
 /**
-   other globals
+ other globals
 */
-static int size, selected, xPos, yPos; 
-static string filemgr, winPos; 
-static istringstream ins; 
-vector<string> mountList; 
-static vector<int> mountState; 
-static vector<Fl_Button*> btn; 
-vector<string> mountLabels; 
+static int mount_list_size, selected, xPos, yPos;
+static string filemgr, winPos;
+static istringstream ins;
+vector<string> mountList;
+static vector<int> mountState;
+static vector<Fl_Button*> btn;
+vector<string> mountLabels;
 
 void getPos() {
   xPos = w->x();
@@ -37,12 +40,15 @@ void getPos() {
 
 void refresh() {
   getMountables();
-  if ( size == 0 ) 
+  if ( mount_list_size == 0 )
     exit(1);
-  
+
   pack->clear();
-  
-  for (int i=0; i < size; i++)
+
+  // width of the button pack
+  size_t wotbp = 1;
+
+  for (int i=0; i < mount_list_size; i++)
   {  
      Fl_Button* btn[i];
      
@@ -50,25 +56,27 @@ void refresh() {
      btn[i]->label(mountList[i].c_str());
      btn[i]->tooltip(mountLabels[i].c_str());
      btn[i]->callback((Fl_Callback*)btnCallback,(void*)(uintptr_t)i);
-  
+
      if ( mountState[i] == 0)
         btn[i]->color((Fl_Color)2);
      else
         btn[i]->color((Fl_Color)1);
-        
+
      pack->add(btn[i]);
-        
+     if ( strlen(mountList[i].c_str()) > wotbp )
+        wotbp = strlen(mountList[i].c_str());
   }
-  
+
   Fl_Button* btnRefresh;
   btnRefresh = new Fl_Button(0,0,80,25);
   btnRefresh->label("Refresh");
   btnRefresh->callback((Fl_Callback*)btnRefreshCallback);
   pack->add(btnRefresh);
-  
+
   selected = 0;
+  pack->resize(0,0,12*wotbp>80 ? 12*wotbp : 80,(25*(mount_list_size)));
   pack->redraw();
-  w->resize(0,0,80,(25*(size+1)));
+  w->resize(0,0,12*wotbp>80 ? 12*wotbp : 80,(25*(mount_list_size+1)));
   w->position(xPos,yPos);
   w->redraw();
 }
@@ -76,14 +84,14 @@ void refresh() {
 void getMountables() {
   mountList.clear();
   mountState.clear();
-  
+
    system("mountables.sh");
-  
+
   ifstream fin("/tmp/mountables");
   string line;
   string commandHead = "grep '/mnt/";
   string commandTail = " ' /etc/mtab >/dev/null";
-  
+
   while ( getline(fin,line) )
   {
      int sepator = line.find("~",0);
@@ -95,8 +103,8 @@ void getMountables() {
   }   
   fin.close();
   unlink("/tmp/mountables");
-  
-  size = mountList.size();
+
+  mount_list_size = mountList.size();
 }
 
 void btnCallback(Fl_Widget*, void* userdata) {
@@ -135,27 +143,27 @@ void btnRefreshCallback(Fl_Widget*, void* userdata) {
 }
 
 /**
-   called by FLTK when infd has data; schedules infd_read()
+ called by FLTK when infd has data; schedules infd_read()
 */
 static void infd_ready_cb(int, void *) {
   Fl::add_timeout(0, infd_read); // schedule infd_read
 }
 
 /**
-   clear inotify queue, refresh, reset_watch
+ clear inotify queue, refresh, reset_watch
 */
 static void infd_read(void *) {
   // read/flush the inotify buf - we're only watching one event. 
   const unsigned iebuflen = sizeof(inotify_event)*32;
   char iebuf[iebuflen];
   read(infd, &iebuf, iebuflen); 
-  
+
   refresh();
   reset_watch(); // set new watch
 }
 
 /**
-   add inotify watch on /etc/fstab; ask FLTK to call infd_ready_cb() on changes
+ add inotify watch on /etc/fstab; ask FLTK to call infd_ready_cb() on changes
 */
 static void reset_watch() {
   if(infd < 0) infd = inotify_init(); 
@@ -183,7 +191,7 @@ int main(int argc, char **argv) {
   setlocale(LC_ALL, "");
   bindtextdomain("tinycore","/usr/local/share/locale");
   textdomain("tinycore");
-  { w = new Fl_Double_Window(85, 745, gettext("mnttool"));
+  { w = new Fl_Double_Window(80, 500, gettext("mnttool"));
     { pack = new Fl_Pack(0, 0, 80, 500);
       pack->end();
     } // Fl_Pack* pack
@@ -191,10 +199,10 @@ int main(int argc, char **argv) {
   } // Fl_Double_Window* w
   if (getenv("FILEMGR"))
      filemgr = getenv("FILEMGR");
-  
+
   xPos=80;
   yPos=60;   
-  
+
   if (getenv("MNTTOOL")) { 
      winPos = getenv("MNTTOOL");
      ins.str(winPos);
diff --git a/mnttool/mnttool.fl b/mnttool/mnttool.fl
index f6b586a..ef6db15 100644
--- a/mnttool/mnttool.fl
+++ b/mnttool/mnttool.fl
@@ -1,93 +1,98 @@
 # data file for the Fltk User Interface Designer (fluid)
-version 1.0303 
-i18n_type 1 
-i18n_include <libintl.h> 
-i18n_function gettext 
-header_name {.h} 
+version 1.0400
+i18n_type 1
+i18n_include {<libintl.h>}
+i18n_conditional {}
+i18n_gnu_function gettext
+i18n_gnu_static_function gettext_noop
+header_name {.h}
 code_name {.cxx}
 decl {// (c) Robert Shingledecker 2008-2011} {private local
-} 
+}
 
 decl {\#include <unistd.h>} {public global
-} 
+}
 
 decl {\#include <cstdlib>} {private local
-} 
+}
 
 decl {\#include <iostream>} {private local
-} 
+}
 
 decl {\#include <sstream>} {private local
-} 
+}
 
 decl {\#include <fstream>} {private local
-} 
+}
 
 decl {\#include <vector>} {private local
-} 
+}
 
 decl {\#include <locale.h>} {private local
-} 
+}
 
 decl {\#include <stdint.h>} {private global
-} 
+}
 
 decl {\#include <FL/Fl_Button.H>} {private local
-} 
+}
 
 decl {\#include <sys/inotify.h>} {private local
-} 
+}
 
 decl {using namespace std;} {private local
-} 
+}
 
 decl {int infd = -1; // inotify fd} {
   comment {for watching /etc/fstab} private local
-} 
+}
 
 decl {void infd_read(void *);} {private local
-} 
+}
 
 decl {void reset_watch();} {private local
-} 
+}
 
-decl {int size, selected, xPos, yPos;} {
+decl {int mount_list_size, selected, xPos, yPos;} {
   comment {other globals} private local
-} 
+}
 
 decl {string filemgr, winPos;} {private local
-} 
+}
 
 decl {istringstream ins;} {private local
-} 
+}
 
 decl {vector<string> mountList;} {private global
-} 
+}
 
 decl {vector<int> mountState;} {private local
-} 
+}
 
 decl {vector<Fl_Button*> btn;} {private local
-} 
+}
 
 decl {vector<string> mountLabels;} {private global
-} 
+}
 
 Function {getPos()} {open return_type {static void}
 } {
   code {xPos = w->x();
 yPos = w->y();} {}
-} 
+}
 
 Function {refresh()} {open
 } {
   code {getMountables();
-if ( size == 0 ) 
+if ( mount_list_size == 0 )
   exit(1);
 
 pack->clear();
 
-for (int i=0; i < size; i++)
+// width of the button pack
+size_t wotbp = 1;
+
+for (int i=0; i < mount_list_size; i++)
 {  
    Fl_Button* btn[i];
    
@@ -100,9 +105,10 @@ for (int i=0; i < size; i++)
       btn[i]->color((Fl_Color)2);
    else
       btn[i]->color((Fl_Color)1);
-      
+
    pack->add(btn[i]);
-      
+   if ( strlen(mountList[i].c_str()) > wotbp )
+      wotbp = strlen(mountList[i].c_str());
 }
 
 Fl_Button* btnRefresh;
@@ -112,11 +118,13 @@ btnRefresh->callback((Fl_Callback*)btnRefreshCallback);
 pack->add(btnRefresh);
 
 selected = 0;
+pack->resize(0,0,12*wotbp>80 ? 12*wotbp : 80,(25*(mount_list_size)));
 pack->redraw();
-w->resize(0,0,80,(25*(size+1)));
+w->resize(0,0,12*wotbp>80 ? 12*wotbp : 80,(25*(mount_list_size+1)));
 w->position(xPos,yPos);
-w->redraw();} {}
-} 
+w->redraw();} {selected
+  }
+}
 
 Function {getMountables()} {open return_type void
 } {
@@ -142,8 +150,8 @@ while ( getline(fin,line) )
 fin.close();
 unlink("/tmp/mountables");
 
-size = mountList.size();} {}
-} 
+mount_list_size = mountList.size();} {}
+}
 
 Function {btnCallback(Fl_Widget*, void* userdata)} {open return_type {static void}
 } {
@@ -173,21 +181,21 @@ else
       }
    }
 }} {}
-} 
+}
 
 Function {btnRefreshCallback(Fl_Widget*, void* userdata)} {open return_type {static void}
 } {
   code {getPos();
 system("sudo rebuildfstab");
 refresh();} {}
-} 
+}
 
 Function {infd_ready_cb(int, void *)} {
   comment {called by FLTK when infd has data; schedules infd_read()
 } open private return_type void
 } {
   code {Fl::add_timeout(0, infd_read); // schedule infd_read} {}
-} 
+}
 
 Function {infd_read(void *)} {
   comment {clear inotify queue, refresh, reset_watch} open private return_type void
@@ -199,7 +207,7 @@ read(infd, &iebuf, iebuflen);
 
 refresh();
 reset_watch(); // set new watch} {}
-} 
+}
 
 Function {reset_watch()} {
   comment {add inotify watch on /etc/fstab; ask FLTK to call infd_ready_cb() on changes} open private return_type void
@@ -218,7 +226,7 @@ Function {reset_watch()} {
     }
   
     Fl::add_fd(infd, FL_READ, infd_ready_cb);} {}
-} 
+}
 
 Function {} {open
 } {
@@ -227,8 +235,8 @@ setlocale(LC_ALL, "");
 bindtextdomain("tinycore","/usr/local/share/locale");
 textdomain("tinycore");} {}
   Fl_Window w {
-    label mnttool open selected
-    xywh {267 20 85 745} type Double hide
+    label mnttool open
+    xywh {0 0 80 500} type Double visible
   } {
     Fl_Pack pack {open
       xywh {0 0 80 500}
@@ -248,4 +256,4 @@ if (getenv("MNTTOOL")) {
    
 refresh();
 reset_watch();} {}
-} 
+}
diff --git a/mnttool/mnttool.h b/mnttool/mnttool.h
index c9f4b6a..5c155ed 100644
--- a/mnttool/mnttool.h
+++ b/mnttool/mnttool.h
@@ -1,4 +1,4 @@
-// generated by Fast Light User Interface Designer (fluid) version 1.0304
+// generated by Fast Light User Interface Designer (fluid) version 1.0400
 
 #ifndef mnttool_h
 #define mnttool_h
