about summary refs log tree commit diff stats
path: root/src/io/window.nim
blob: 278fc3fb30cfab6249c9077ac09567a951d16ea9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import terminal

when defined(posix):
  import termios


type
  WindowAttributes* = object
    width*: int
    height*: int
    ppc*: int # cell width
    ppl*: int # cell height
    cell_ratio*: float64 # ppl / ppc
    width_px*: int
    height_px*: int

proc getWindowAttributes*(tty: File): WindowAttributes =
  when defined(posix):
    if tty.isatty():
      var win: IOctl_WinSize
      if ioctl(cint(getOsFileHandle(tty)), TIOCGWINSZ, addr win) != -1:
        var cols = win.ws_col
        var rows = win.ws_row
        if cols == 0:
          cols = 80
        if rows == 0:
          rows = 24
        # Filling the last row without raw mode breaks things. However,
        # not supporting Windows means we can always have raw mode, so we can
        # use all available columns.
        result.width = int(cols)
        result.height = int(rows)
        result.ppc = int(win.ws_xpixel) div result.width
        result.ppl = int(win.ws_ypixel) div result.height
        # some terminal emulators (aka vte) don't set ws_xpixel or ws_ypixel.
        # solution: use xterm.
        if result.ppc == 0:
          result.ppc = 9
        if result.ppl == 0:
          result.ppl = 18
        result.width_px = result.width * result.ppc
        result.height_px = result.height * result.ppl
        result.cell_ratio = result.ppl / result.ppc
        return
  # for Windows. unused.
  result.width = terminalWidth() - 1
  result.height = terminalHeight()
  if result.height == 0:
    result.height = 24
  result.ppc = 9
  result.ppl = 18
  result.cell_ratio = result.ppl / result.ppc
  result.width_px = result.ppc * result.width
  result.height_px = result.ppl * result.height