browsers.nim 3.1 KB

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