SHARE
TWEET

Untitled

a guest Nov 19th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import terminal, sequtils, tables, strutils
  2. from os import sleep
  3. import redis, dashing, asyncdispatch
  4.  
  5. var
  6.   keys: RedisList
  7.   diff: Table[RedisString, RedisString]
  8.  
  9. proc renderRedis() {.async.} =
  10.   ## Poll Redis for new keys and add the keys to our `diff`
  11.  
  12.   let redisClient = await openAsync()
  13.   # Clear our diff table
  14.   clear diff
  15.   while true:
  16.     var oldKeys: RedisList
  17.     shallowCopy(oldKeys, keys)
  18.    
  19.     # Get all keys
  20.     keys = await redisClient.keys("*")
  21.     for key in keys:
  22.       if not oldKeys.contains(key):
  23.         #Add the new key into the diff
  24.         let val = await redisClient.get(key)
  25.         discard diff.hasKeyOrPut(key, val)
  26.  
  27.     await sleepAsync(2000)
  28.  
  29. proc render() {.async.} =
  30.   ## Build our Tiles and display them
  31.  
  32.   var
  33.     latestKeys, curKeys: Table[RedisString, RedisString]
  34.     allKeys = Tile(kind: Log, title: "Current Keys", border_color:"f00", color:"0ff")
  35.     latestDiff = Tile(kind: Text, title: "Latest Changes", border_color:"00f", color:"fff")
  36.     cleared: bool
  37.  
  38.   while true:
  39.     erase_screen()
  40.     cleared = false
  41.     latestDiff.text = ""
  42.  
  43.     # If we have new keys in the diff, then clear our list of latest keys
  44.     if diff.len != latestKeys.len:
  45.       curKeys.clear()
  46.       cleared = true
  47.  
  48.     # For each new key, add it to our total list and log it
  49.     for key in diff.keys:
  50.       if not latestKeys.contains(key):
  51.         latestKeys[key] = diff[key]
  52.         allKeys.add_log(key)
  53.         curKeys[key] = diff[key]
  54.  
  55.     # Build our text from the most recent keys
  56.     for key in curKeys.keys:
  57.       latestDiff.text = block:
  58.         if not cleared:
  59.           latestDiff.text & "\r\n" & key
  60.         else:
  61.           key
  62.  
  63.     # Build our tile display
  64.     var ui = Tile(kind: Hsplit, border_color:"fff", items: @[
  65.       allKeys, latestDiff
  66.     ])
  67.  
  68.     # Render our tile display
  69.     ui.display()
  70.  
  71.     # Wait a second before getting new results
  72.     await sleepAsync 3000
  73.     set_cursor_at(0, terminal_height() - 1)
  74.  
  75.  
  76. when isMainModule:
  77.   asyncCheck renderRedis()
  78.   asyncCheck render()
  79.   runForever()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top