Author Topic: Menu me this....  (Read 16392 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Menu me this....
« Reply #30 on: October 24, 2018, 11:53:37 AM »
HotBasic Version  8) 8) 8)

Code: Text
  1. $apptype console: $typecheck on
  2.  
  3. Declare function SelectFolder(Title$ as string, Path$ as string) as string
  4.  
  5. function SelectFolder(Title$ as string, Path$ as string) as string
  6.     defstr r$,border$
  7.     dim menu(15) as string*64
  8.     deflng count,selection,theLoop
  9.     dim exclude as LIST
  10.  
  11.     theLoop = 1
  12.     border$ = string$(Title$.length,"-")
  13.     exclude.AddItems("All Users","Default","Default User","Public")
  14.  
  15.     while theLoop
  16.         cls
  17.         count = 0
  18.  
  19.         print Title$
  20.         print border$
  21.  
  22.         r$ = dir$(Path$ + "/*",&H10)
  23.  
  24.         do
  25.             if r$[1] <> "." AND exclude.indexof(r$) = -1 then
  26.                 menu(count) = r$
  27.                 inc count
  28.                 print str$(count) + ") " + r$
  29.             end if
  30.             r$ = dir$
  31.         loop until r$.length = 0
  32.  
  33.         print border$
  34.         input "Select User: "; selection
  35.  
  36.         if selection < 1 or selection > count then
  37.             print ">> Invalid Selection <<"
  38.             sleep .8
  39.         else
  40.             theLoop = 0
  41.         end if
  42.     wend
  43.     result =  menu(selection-1)
  44. end function
  45.  
  46. ' ** PROGRAM START **
  47. defstr SelectedUser = SelectFolder("Armando's Test Menu","/Users")
  48. print CRLF + "You Selected: " + SelectedUser
  49.  


Man this takes me back.....

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Menu me this....
« Reply #31 on: October 24, 2018, 02:53:00 PM »
Wow!

It has been a long time since I seen Hot Basic code. Is Jim still in the game?

Curious, what are your thoughts about the unicode solution I proposed?
« Last Edit: October 24, 2018, 04:12:25 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Menu me this....
« Reply #32 on: October 24, 2018, 10:07:28 PM »
C VERSION

Not 100% happy with this, but it seems to work okay...

Code: C
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. #define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))
  8.  
  9. const char* exclude[] = {"Guest","Shared","Public"};
  10.  
  11. char * SelectFolder(const char* Title, const char *Path){
  12.     char *line = NULL, *end, *menu[14];
  13.     char **ptr = menu;
  14.     int selected, count, i=0, loop = 1;
  15.     size_t len = 1, read = 0;
  16.  
  17.  
  18.     while (loop) {
  19.         printf("\033[2J\033[H");
  20.         printf("%s\n",Title);
  21.         printf("%.*s\n", (int)strlen(Title), "------------------------------------------");
  22.         i = count = 0;
  23.  
  24.         DIR *dir = opendir(Path);
  25.  
  26.         struct dirent *entry = readdir(dir);
  27.  
  28.         while (i< NELEMS(exclude)) {
  29.             while (entry != NULL) {
  30.                 if ( (entry->d_type == DT_DIR) && (strncmp(entry->d_name,".",1 ) != 0) && (strcmp(entry->d_name,exclude[i]) !=0)) {
  31.                     printf("%i) %s\n", count+1,entry->d_name);
  32.                     menu[count] = entry->d_name;
  33.                     count++;
  34.                 }
  35.                 entry = readdir(dir);
  36.                
  37.             }
  38.             i++;
  39.         }
  40.        
  41.         closedir(dir);
  42.  
  43.         printf("%.*s\n", (int)strlen(Title), "------------------------------------------");
  44.         printf("%s","Select User: ");
  45.         read = getline(&line, &len, stdin);
  46.         selected = strtol(line,&end,10);
  47.         printf("\n");
  48.        
  49.  
  50.         if ( (selected <1) || (selected > count) ) {
  51.             printf("%s\n",">> Invalid Selection <<");
  52.             sleep(1);
  53.         }else{
  54.             loop = 0;
  55.             free(line);
  56.         }
  57.            
  58.     }
  59.     return strdup(menu[selected-1]);
  60. }
  61.  
  62. int main() {
  63.     char* selected = SelectFolder("Armando's Test Menu","/Users");
  64.     printf("You Selected: %s\n\n",selected);
  65.     free(selected);
  66.     return 0;
  67. }
  68.  
  69.  

I think this is the last language I'll post...waiting on others to jump in....

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Menu me this....
« Reply #33 on: October 24, 2018, 10:14:49 PM »
Maybe Charles can find time to represent Oxygen Basic in this challenge?

Hopefully he will have better luck under Windows then I did with SB.
« Last Edit: October 24, 2018, 10:27:10 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: Menu me this....
« Reply #34 on: October 25, 2018, 08:00:46 PM »
Hopefully he will have better luck under Windows then I did with SB.

I don't see what the issue is with SB itself other than the forced lowercasing of the returned folder list.

IF you make the change in filesys.c that I suggested previously (just comment that line), and edit the script as I have below, it works fine:

Code: ScriptBasic
  1. ' AIR - Select user for backup
  2.    
  3. FUNCTION SelectFolder(Title, Folder)
  4.     LOCAL fn, Exclude, Border, Search_Options, file_list, fName, users, idx, uid, ret
  5.     fn = FREEFILE()
  6.  
  7.     Exclude = ".|All Users|Default|Default User|Public|desktop.ini"
  8.  
  9.     Border = STRING(LEN(Title), "-")
  10.  
  11.     OPEN DIRECTORY Folder PATTERN "" OPTION SbCollectDirectories AS #fn
  12.  
  13.     RESET DIRECTORY #fn
  14.  
  15.     fName = NEXTFILE(#fn)
  16.  
  17.     WHILE fName <> undef
  18.         IF NOT INSTR(Exclude, fName) = undef THEN file_list &= fName & "\n"
  19.         fName = NEXTFILE(#fn)
  20.     WEND
  21.  
  22.     CLOSE DIRECTORY #fn
  23.  
  24. Retry:
  25.     users = undef
  26.  
  27.     ret = EXECUTE("cmd /c cls",-1,PID)
  28.  
  29.     SPLITA file_list BY "\n" TO users
  30.    
  31.     PRINT Title,"\n",Border,"\n"
  32.  
  33.     FOR idx = 0 TO UBOUND(users)
  34.         PRINT idx + 1,") ",users[idx],"\n"
  35.     NEXT
  36.  
  37.     PRINT Border,"\n","Select User: "
  38.  
  39.     LINE INPUT uid
  40.  
  41.     uid = CHOMP(uid)
  42.  
  43.     IF uid - 1 > UBOUND(users) OR users[uid - 1] = undef THEN
  44.         PRINT "\n>> Invalid Selection <<"
  45.         SLEEP 1
  46.         GOTO Retry
  47.     END IF
  48.    
  49.     SelectFolder = users[uid -1]
  50.  
  51. END FUNCTION
  52.  
  53. selectedUser = SelectFolder("Armando's Test Menu", "/Users")
  54.  
  55. PRINT "\nYou Selected: ", selectedUser, "\n"
  56.    
  57.  

AIR.

P.S.  When posting code, please format it so that it's easier to read/follow (as I've done above).  It's easy to miss something, and also hard to follow the flow of the code, when it's densely packed.  Thanks!!


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Menu me this....
« Reply #35 on: October 25, 2018, 11:55:27 PM »
Thanks for the confirmation your suggested change fixes the Windows issue. Why do you think Peter forced lower case for Windows only?

I will try to make the SB code I submit more readable. I may have caught Tomaaz's one line wonder disease.

« Last Edit: October 25, 2018, 11:58:12 PM by John »

Mike Lobanovsky

  • Guest
Re: Menu me this....
« Reply #36 on: October 26, 2018, 03:32:34 AM »
... hard to follow the flow of the code, when it's densely packed.

Not so for me actually. I prefer camel case, 8-px monospace fonts, 2-char tabs, indentation guides and syntax highlighting, and I code on every line leaving one blank line between functions. :)

Mike Lobanovsky

  • Guest
Re: Menu me this....
« Reply #37 on: October 26, 2018, 03:47:48 AM »
Why do you think Peter forced lower case for Windows only?

Obviously because Windows pathnames and filenames are case insensitive. Windows also supports forward and back slashes indiscriminately. It even allows them to be intermixed at will. :)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Menu me this....
« Reply #38 on: October 26, 2018, 07:23:57 AM »
Does that also hold true for unicode filenames?

I'm still going ahead with AIR's recommended change as it would be confusing to users seeing their filenames changed.
« Last Edit: October 26, 2018, 10:11:18 AM by John »

Mike Lobanovsky

  • Guest
Re: Menu me this....
« Reply #39 on: October 26, 2018, 12:21:31 PM »
No, Unicode pathnames are a horror and you're done if you don't have an appropriate language support installed on your box. You won't be able to either access, or rename, or delete the offending file. But then, upper-/lower-casing isn't going to work for you either.

The same goes about Linux chars not allowed in the Windows pathnames created or copied to the Windows hosts from under their Linux VM's.

Avoid localized path/file names under Windows at all times and use ANSI whenever possible because there may be potential users whose environments are not equipped with the corresponding locale.

Not that it matters much to Anglo-Saxon users though whose locale support is built into the MS Windows installations natively. ;)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Menu me this....
« Reply #40 on: October 26, 2018, 01:21:55 PM »
Thanks Mike for your thoughts on unicode. I'm going take your advice as well.