Listing hash tables and their contents

From Scriptwiki
Revision as of 21:34, 10 July 2013 by Jay2k1 (talk | contribs) (Created page with "When you are working on a script that uses hash tables, sometimes you want to see their contents. In contrast to text or ini files, you can't just view them in an easy way. Th...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

When you are working on a script that uses hash tables, sometimes you want to see their contents. In contrast to text or ini files, you can't just view them in an easy way. That's where this script comes into play.

You can install it by opening the script editor (Alt+R), creating a new script file (File -> New) and then copy paste the script into it.

/*
***************************************************************************************
*
*  hlist.mrc by Jay2k1 @ QuakeNet, 2013
*
*  shows the contents of hash tables in alphabetically sorted custom windows
*
***************************************************************************************
*
*  USAGE
*
*  /hlist [-c|--close] [(partial) table name]
*
*  When used without options, /hlist will open a sorted window for every hash table and display their contents.
*  When used with a hash table name, it will only display that hash table's contents.
*  It can be used with partial names, e.g. you have the tables "joins_#help" and "joins_#help.script", the
*  command '/hlist joins' would display both of these.
*  The -c switch closes the windows. Again, you can specify a full or partial hash table name.
*
***************************************************************************************
*
*  SETTINGS
*/
alias -l hlist.pad {
  ; set this to $true if you want the hash table data column to be padded (only useful with monospaced font,
  ; e.g. Fixedsys) or to $false if not.
  return $true
}
alias -l hlist.prefix {
  ; this is the prefix for the custom window's names. Default is HT_
  return HT_
}
/*
***************************************************************************************
*/

alias hlist {
  ; without any arguments, list all hash tables
  if !$1 { var %a = $hget(0) | while %a { hlist.show $hget(%a) | dec %a } }

  ; closing windows
  elseif ($1 == -c) || ($1 == --close) {

    ; without further arguments, close all windows
    if !$2 {
      var %a = $window($+(@,$hlist.prefix,*),0)

      while %a {
        window -c $window($+(@,$hlist.prefix,*),%a)
        dec %a
      }
    }

    ; otherwise, close requested windows
    else {
      if ($window($+(@,$hlist.prefix,$2))) window -c $v1
      else {
        var %a = $window($+(@,$hlist.prefix,*,$2,*),0)
        while %a {
          window -c $window($+(@,$hlist.prefix,*,$2,*),%a)
          dec %a
        }
      }
    }
  }

  ; list hash tables by wildcard match if no matching table exists
  elseif !$hget($1) {
    var %a = $hget(0)
    if !%a { echo -ag hlist: there are no hash tables | return }
    while %a {
      if ($1 isin $hget(%a)) hlist.show $hget(%a)
      dec %a
    }
  }

  ; list specified hash table
  else hlist.show $1
}

; alias hlist.show: opens a custom window and populates it with contents of a given hash table
; expects: hash table name as $1
; returns: -
alias -l hlist.show {
  var %a = $hget($1,0).item, %w = $+(@,$hlist.prefix,$1,$chr(160),[,%a,]), %len = 0
  if $window(%w) { clear %w }
  else { window -ls %w }

  if $hlist.pad {
    ; get length of the longest item name
    while %a {
      if ($len($hget($1,%a).item) > %len) var %len = $v1
      dec %a
    }
    var %a = $hget($1,0).item
  }

  ; loop through hash table items and add them into the window
  while %a {
    var %i = $hget($1,%a).item
    aline %w $+(%i,$str($chr(160),$calc(%len - $len(%i)))) => $hget($1,%a).data
    dec %a
  }
}
Contributed by Jay2k1