browsers.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a simple proc for opening URLs with the user's
  10. ## default browser.
  11. import strutils
  12. when defined(windows):
  13. import winlean
  14. else:
  15. import os, osproc
  16. proc openDefaultBrowser*(url: string) =
  17. ## opens `url` with the user's default browser. This does not block.
  18. ##
  19. ## Under Windows, ``ShellExecute`` is used. Under Mac OS X the ``open``
  20. ## command is used. Under Unix, it is checked if ``xdg-open`` exists and
  21. ## used if it does. Otherwise the environment variable ``BROWSER`` is
  22. ## used to determine the default browser to use.
  23. when defined(windows):
  24. var o = newWideCString("open")
  25. var u = newWideCString(url)
  26. discard shellExecuteW(0'i32, o, u, nil, nil, SW_SHOWNORMAL)
  27. elif defined(macosx):
  28. discard execShellCmd("open " & quoteShell(url))
  29. else:
  30. var u = quoteShell(url)
  31. if execShellCmd("xdg-open " & u) == 0: return
  32. for b in getEnv("BROWSER").string.split(PathSep):
  33. try:
  34. # we use ``startProcess`` here because we don't want to block!
  35. discard startProcess(command=b, args=[url], options={poUsePath})
  36. return
  37. except OSError:
  38. discard