browsers.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ##
  12. ## Unstable API.
  13. import strutils
  14. include "system/inclrtl"
  15. when defined(windows):
  16. import winlean
  17. else:
  18. import os, osproc
  19. const osOpenCmd* =
  20. when defined(macos) or defined(macosx) or defined(windows): "open" else: "xdg-open" ## \
  21. ## Alias for the operating system specific *"open"* command,
  22. ## ``"open"`` on OSX, MacOS and Windows, ``"xdg-open"`` on Linux, BSD, etc.
  23. template openDefaultBrowserImpl(url: string) =
  24. when defined(windows):
  25. var o = newWideCString(osOpenCmd)
  26. var u = newWideCString(url)
  27. discard shellExecuteW(0'i32, o, u, nil, nil, SW_SHOWNORMAL)
  28. elif defined(macosx):
  29. discard execShellCmd(osOpenCmd & " " & quoteShell(url))
  30. else:
  31. var u = quoteShell(url)
  32. if execShellCmd(osOpenCmd & " " & u) == 0: return
  33. for b in getEnv("BROWSER").string.split(PathSep):
  34. try:
  35. # we use ``startProcess`` here because we don't want to block!
  36. discard startProcess(command = b, args = [url], options = {poUsePath})
  37. return
  38. except OSError:
  39. discard
  40. proc openDefaultBrowser*(url: string) =
  41. ## Opens `url` with the user's default browser. This does not block.
  42. ## The URL must not be empty string, to open on a blank page see `openDefaultBrowser()`.
  43. ##
  44. ## Under Windows, ``ShellExecute`` is used. Under Mac OS X the ``open``
  45. ## command is used. Under Unix, it is checked if ``xdg-open`` exists and
  46. ## used if it does. Otherwise the environment variable ``BROWSER`` is
  47. ## used to determine the default browser to use.
  48. ##
  49. ## This proc doesn't raise an exception on error, beware.
  50. ##
  51. ## .. code-block:: nim
  52. ## block: openDefaultBrowser("https://nim-lang.org")
  53. doAssert url.len > 0, "URL must not be empty string"
  54. openDefaultBrowserImpl(url)
  55. proc openDefaultBrowser*() {.since: (1, 1).} =
  56. ## Opens the user's default browser without any `url` (blank page). This does not block.
  57. ## Implements IETF RFC-6694 Section 3, "about:blank" must be reserved for a blank page.
  58. ##
  59. ## Under Windows, ``ShellExecute`` is used. Under Mac OS X the ``open``
  60. ## command is used. Under Unix, it is checked if ``xdg-open`` exists and
  61. ## used if it does. Otherwise the environment variable ``BROWSER`` is
  62. ## used to determine the default browser to use.
  63. ##
  64. ## This proc doesn't raise an exception on error, beware.
  65. ##
  66. ## **See also:**
  67. ##
  68. ## * https://tools.ietf.org/html/rfc6694#section-3
  69. ##
  70. ## .. code-block:: nim
  71. ## block: openDefaultBrowser()
  72. openDefaultBrowserImpl("http:about:blank") # See IETF RFC-6694 Section 3.