browsers.nim 2.9 KB

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