browsers.nim 3.1 KB

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