Skip to content

Instantly share code, notes, and snippets.

View enthus1ast's full-sized avatar

David Krause enthus1ast

View GitHub Profile
dumpTree:
var dis: set[Distribution] = {}
if detectOs(Windows):
dis.incl Windows
## more detect os
import distros
import macros
macro whichOs(): untyped =
result = newStmtList()
for dist in Distribution:
result.add newCall("echo", newLit dist , newLit " ", newCall("detectOs", newIdentNode($dist)))
whichOs()
import distros
for dist in Distribution:
echo dist, detectOs(dist)
distrost.nim(53, 22) template/generic instantiation of `detectOs` from here
distros.nim(184, 28) Error: undeclared field: 'dist'
found 'dist' of kind 'forvar'
import asyncdispatch, asyncnet
type
Server = ref object
socket: AsyncSocket
proc newServer(): Server =
Server(socket: newAsyncSocket())
proc loop(server: Server, port = 7920) {.async.} =
server.socket.bindAddr(Port(port), "0.0.0.0")
# test with: `while true; do curl 127.0.0.1:8080; done`
# import threadpool
import locks
import os
import asyncdispatch
import asynchttpserver, asyncdispatch
var L: Lock
type Context = ref object {.pure.}
type ServiceMain = proc(gSvcStatus: SERVICE_STATUS)
proc wrapServiceMain(mainProc: ServiceMain): LPSERVICE_MAIN_FUNCTION {.closure.} =
## wraps a nim proc in a LPSERVICE_MAIN_FUNCTION
return proc(dwArgc: DWORD, lpszArgv: LPTSTR) {.stdcall.} =
gSvcStatusHandle = RegisterServiceCtrlHandler(
SERVICE_NAME,
svcCtrlHandler
)
gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS
gSvcStatus.dwServiceSpecificExitCode = 0
## Store multiple "bool" values in an integer
# http://blog.millermedeiros.com/using-integers-to-store-multiple-boolean-values/
type foos = enum
foo = 1.shl 0, # 1
baa = 1.shl 1, # 2
baz = 1.shl 2 # 4
echo foo.int
echo baa.int
echo baz.int
################
import webview
import strutils
import os
const indexHTML = """
<!doctype html>
<html>
<head>
import httpclient, asyncdispatch
var client = newAsyncHttpClient()
proc onProgressChanged(total, progress, speed: BiggestInt) {.async.} =
echo("Downloaded ", progress, " of ", total)
echo("Current rate: ", speed div 1000, "kb/s")
client.onProgressChanged = onProgressChanged
echo waitFor client.getContent("http://speedtest-ams2.digitalocean.com/100mb.test")
import times
import options
type
Entry = object of RootObj
emergeDate: Option[DateTime]
publishDate: Option[DateTime]
proc newEntry(): Entry =
# Gives: Error: fields not initialized: emergeDate, publishDate.