/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Get driveletters for All drives (Providers) in MSH... "," Math "," Exploring .NET types, classes and Enums from MSH "," Wipe the screen in MSH "," Blog series about Errorhandling in MSH "," This Month's line-break and Pipelines are Back "," [msh]cd.. "," MSH start. Function "," Argggg "," Where are my pipelines "," ")

Sunday, November 13, 2005

 


Enhanced More Function for MSH



As you may have noticed the more command support in MSH is a bit limited.

But most annoying :
if you use it the StatusMessageline will stay in your output. (Bahh)

<SPACE> next page; <CR> next line; Q quit
test5
<SPACE> next page; <CR> next line; Q quit
test6
<SPACE> next page; <CR> next line; Q quit
test7
<SPACE> next page; <CR> next line; Q quit


if you look at the definition of the More function :

Function More {

if($args.length -ne 0)    {
       foreach ($local:file in $args)       {
          get-content $local:file | out-host -p       }    }
else { $input | out-host -p }
}


You can see it just uses Out-host to page the Content.

As I did realy not like the way the More command worked, I wrote a new one that behaves a bit more like the CMD more Command.

SO :

1) It will NOT leave his StatusMessages around messing up the output

2) It Will show the filePosition as Percentage

3) It supports the P Command that let's YOU give a number of lines

4) It Supports the = Command that gives you the current Linenumber

5) It Supports the F Command that skips to the next file.

6) It Supports the -C argument to clear the Screen First.

At the Moment it will only work for files that are given as an argument, if you use a pipeline to feed it, it will fail back to the "Old"-way (to let you see what you miss there ;-))

also the -E switch is Checked but not Implemented (I like it always Extended ;-))
I added it already because the script does check if there are "Non-Switch" parameters available.

gr /\/\o\/\/

# More.msh
# a MSH more function replacement.
# That works more like the CMD More Command.
# And does not leave his StatusMessages Around
#/\/\o\/\/ 2005

Function More {

  # Get Window Size

  $Lines = $host.ui.rawui.WindowSize.Height
  $width =  $host.ui.rawui.WindowSize.width

  if($args.length -ne 0) {

    # Switch Not used yet, Always in Extended Mode

    if ($args -eq '-e') {write-host "E param given"}
    if ($args -eq '-c') {cls}

    # check if InputFiles are given as Arguments

    $InParamsGiven = $False
    $Inparams = new-object system.collections.arraylist
    $args | foreach {if (([string]$_).substring(0,1) -ne '-') {$InParamGiven = $true;[void]$Inparams.add($_)}}

    # Inparms Given Process Them

    if ($InParamGiven = $true) {

     $Quit = $False 
     foreach ($file in $Inparams) {
        If ($Quit -eq $True){Break

        # Open File (Unbuffered to keep Position)
         
        $fs = (gi $file).OpenRead()
        $LineNumber = 0
        $eof = $False

        while ($eof -eq $False) {

          # use Stringbuilder to Store Page text

          $sb = new-object text.stringbuilder
          
          # Get needed Lines from Stream

          for ($i = 0 ; $i -lt $lines ; $i++) {
            $byte = $Null
            while ($byte -ne 13) {
              $byte = $fs.readbyte()
              if ($byte -ne -1){[void]$sb.Append([char]$byte)}
              Else {$eof = $True;break}
            }
            $LineNumber += 1
          }
          write-host -no $sb.tostring()

          # If we are not ready Yet, Show status and wait for Orders

          if ($eof -eq $false) {

            # Calcultate Percentage and Make StatusString

            $Msg = "-- More ($([int]($fs.position / ($fs.length / 100)))%) --"
            $row = $host.ui.rawui.NewBufferCellArray($msg,'gray','black')
            $pos = $host.ui.rawui.CursorPosition
            $rect = "system.management.automation.host.rectangle"
            $re = new-object $rect $pos.x,$pos.y,$width,$pos.y  

            # Get Old Data (only if reading new lines, so we do not copy status)
 
            if ($lines -ne 0) {$buffer = $host.ui.rawui.getbuffercontents($re)}

            #show Status, and wait for a Key

            $host.ui.rawui.SetBufferContents($Pos,$row)
            Switch ([Console]::ReadKey($True).key) {

              "Enter" {$lines = 1

              "Spacebar" {$lines = $host.ui.rawui.WindowSize.Height - 1

              "P" {$pos.x = 20
                $host.ui.rawui.CursorPosition = $pos
                $lines = read-host "Lines"
                $pos.x = 0 ; $pos.y = $pos.y - 1
                $host.ui.rawui.CursorPosition = $pos
                $pos.y = $pos.y + 1
              }

              "F" {$eof = $true;}

              "Q" {$eof = $true;$Quit = $true}

              "OemPlus" {$pos.x = 20
                $host.ui.rawui.CursorPosition = $pos
                Write-host -no "[Line : $LineNumber]"
                $lines = 0
                $pos.x = 0
                $host.ui.rawui.CursorPosition = $pos
                $pos.y = $pos.y + 1 
              }
              Default {$lines = 0}
            }

            #write back Old Data

            $host.ui.rawui.SetBufferContents($pos,$buffer)
          }
        }
        if ($eof -eq $true) {""}
      }
    }
    Else { #No Imparms 

      # still the Old way 
      $input | out-host -p
    }
  }Else { # No Params

    # Still the old way
    $input | out-host -p

  }
}



Comments: Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?