How to get percentage from npm install in Pure?

Just starting out? Need help? Post your questions and find answers here.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

How to get percentage from npm install in Pure?

Post by Mythros »

Hi all! How can I add a progress bar that shows the percent of a command prompt npm install? IE : You know how when installing with npm, it shows a text loading bar?

Code: Select all

[###########.......]
How do I grab that current value & turn it into a percentage & plug it into a percentage bar?

ANY & ALL help is GREATLY appreciated! <3

Thank You! <3
infratec
Always Here
Always Here
Posts: 7657
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to get percentage from npm install in Pure?

Post by infratec »

viewtopic.php?p=544671#p544671

You have to search the line which contains '[' and count the carrets.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: How to get percentage from npm install in Pure?

Post by Mythros »

@infratec : Thank you! Although I do not understand this. Is there easier way?

Thank You! <3
infratec
Always Here
Always Here
Posts: 7657
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to get percentage from npm install in Pure?

Post by infratec »

No, it's not enough to show the way...
I have to code it all :wink:

Code: Select all

EnableExplicit


Import "kernel32.lib"
  AttachConsole(processID.l)
EndImport


Procedure.f GetHashPercentage()
  
  Protected hConsole.i, processID.i, hStdOut.i, NumberOfChars.l, Y.i, Pos1.i, Pos2.i, Result.f
  Protected MaxCarrets.i, CurrentCarrets.i
  Protected ConsoleScreenBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
  Protected *Buffer
  Protected ReadCoord.COORD
  Protected Line$
  
  
  Result = -1
  
  hConsole = FindWindow_("ConsoleWindowClass", #Null)
  If hConsole
    GetWindowThreadProcessId_(hConsole, @processID)
    If AttachConsole(processID)
      hStdOut = GetStdHandle_(#STD_OUTPUT_HANDLE)
      If hStdOut
        If GetConsoleScreenBufferInfo_(hStdOut, @ConsoleScreenBufferInfo)
          
          *Buffer = AllocateMemory((ConsoleScreenBufferInfo\srWindow\right + 1) * (ConsoleScreenBufferInfo\srWindow\bottom + 1) * SizeOf(Character), #PB_Memory_NoClear)
          If *Buffer
            ReadCoord\x = 0
            ReadCoord\y = ConsoleScreenBufferInfo\srWindow\top
            
            If ReadConsoleOutputCharacter_(hStdOut, *Buffer, (ConsoleScreenBufferInfo\srWindow\right + 1) * (ConsoleScreenBufferInfo\srWindow\bottom + 1), PeekL(@ReadCoord), @NumberOfChars)
              
              For Y = 0 To ConsoleScreenBufferInfo\srWindow\bottom - ConsoleScreenBufferInfo\srWindow\top
                Line$ = PeekS(*Buffer + (Y * (ConsoleScreenBufferInfo\srWindow\right + 1) * SizeOf(Character)), ConsoleScreenBufferInfo\srWindow\right + 1)
                Pos1 = FindString(Line$, "[")
                If Pos1
                  Debug Line$
                  If Pos1 < 16 ; I don't know where it starts, but needed because DOS returns the Version in []
                    Pos1 + 1
                    Pos2 = FindString(Line$, "]", Pos1)
                    If Pos2
                      MaxCarrets = Pos2 - Pos1
                      Line$ = Mid(Line$, Pos1, MaxCarrets)
                      
                      CurrentCarrets = CountString(Line$, "#")
                      
                      Debug Str(CurrentCarrets) + " of " + Str(MaxCarrets)
                      Result = CurrentCarrets * 100 / MaxCarrets
                      
                      Break
                    EndIf
                  EndIf
                EndIf
              Next Y
            EndIf
            FreeMemory(*Buffer)
          EndIf
          
        EndIf
      EndIf
      FreeConsole_()
    EndIf
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Define Prog.i

Prog = RunProgram("cmd", "/K @echo [#####               ]", "", #PB_Program_Open)
If Prog
  
  Delay(1000)
  
  Debug StrF(GetHashPercentage(), 2) + "%"
  
  Delay(3000)
  
  KillProgram(Prog)
  CloseProgram(Prog)
EndIf
infratec
Always Here
Always Here
Posts: 7657
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to get percentage from npm install in Pure?

Post by infratec »

I finished.
Now I go for a walk with my dogs.
Post Reply