checkResize :: Quake ()
checkResize = do
    vidDefWidth <- use $ globals.gVidDef.vdWidth

    let w = (vidDefWidth `shiftR` 3) - 2
        width = if w > Constants.maxCmdLine
                  then Constants.maxCmdLine
                  else w

    lineWidth <- use $ globals.gCon.cLineWidth

    unless (width == lineWidth) $ do
      if width < 1 -- video hasn't been initialized yet
        then do
          zoom (globals.gCon) $ do
            cLineWidth .= 38
            cTotalLines .= Constants.conTextSize `div` 38

          use (globals.gCon.cText) >>= \text ->
            io $ text `MSV.set` ' '

        else do
          oldWidth <- use $ globals.gCon.cLineWidth
          globals.gCon.cLineWidth .= width

          let totalLines = Constants.conTextSize `div` width
          oldTotalLines <- use $ globals.gCon.cTotalLines
          globals.gCon.cTotalLines .= totalLines

          let numLines = if totalLines < oldTotalLines
                           then totalLines
                           else oldTotalLines

          let numChars = if width < oldWidth
                           then width
                           else oldWidth

          currentLine <- use $ globals.gCon.cCurrent
          use (globals.gCon.cText) >>= \text -> do
            tbuf <- io $ MSV.clone text
            fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines width 0 0 numLines numChars

          clearNotify

      totalLines <- use $ globals.gCon.cTotalLines
      globals.gCon.cCurrent .= totalLines - 1
      globals.gCon.cDisplay .= totalLines - 1

  where fillInBuf :: MSV.IOVector Char -> MSV.IOVector Char -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Quake ()
        fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth i j maxI maxJ
          | i >= maxI = return ()
          | j >= maxJ = fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth (i + 1) 0 maxI maxJ
          | otherwise = do
              let idx = (totalLines - 1 - i) * lineWidth + j
                  idx2 = ((currentLine - i + oldTotalLines) `mod` oldTotalLines) * oldWidth + j
              io $ tbuf `MSV.read` idx2 >>= MSV.write text idx
              fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth i (j + 1) maxI maxJ
checkResize :: Quake ()
checkResize = do
    vidDefWidth <- use $ globals.gVidDef.vdWidth

    let w = (vidDefWidth `shiftR` 3) - 2
        width = if w > Constants.maxCmdLine
                  then Constants.maxCmdLine
                  else w

    lineWidth <- use $ globals.gCon.cLineWidth

    unless (width == lineWidth) $ do
      if width < 1 -- video hasn't been initialized yet
        then do
          zoom (globals.gCon) $ do
            cLineWidth .= 38
            cTotalLines .= Constants.conTextSize `div` 38

          use (globals.gCon.cText) >>= \text ->
            io $ text `MSV.set` ' '

        else do
          oldWidth <- use $ globals.gCon.cLineWidth
          globals.gCon.cLineWidth .= width

          let totalLines = Constants.conTextSize `div` width
          oldTotalLines <- use $ globals.gCon.cTotalLines
          globals.gCon.cTotalLines .= totalLines

          let numLines = if totalLines < oldTotalLines
                           then totalLines
                           else oldTotalLines

          let numChars = if width < oldWidth
                           then width
                           else oldWidth

          currentLine <- use $ globals.gCon.cCurrent
          use (globals.gCon.cText) >>= \text -> do
            tbuf <- io $ MSV.clone text
            fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines width 0 0 numLines numChars

          clearNotify

      totalLines <- use $ globals.gCon.cTotalLines
      globals.gCon.cCurrent .= totalLines - 1
      globals.gCon.cDisplay .= totalLines - 1

  where fillInBuf :: MSV.IOVector Char -> MSV.IOVector Char -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Quake ()
        fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth i j maxI maxJ
          | i >= maxI = return ()
          | j >= maxJ = fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth (i + 1) 0 maxI maxJ
          | otherwise = do
              let idx = (totalLines - 1 - i) * lineWidth + j
                  idx2 = ((currentLine - i + oldTotalLines) `mod` oldTotalLines) * oldWidth + j
              io $ tbuf `MSV.read` idx2 >>= MSV.write text idx
              fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth i (j + 1) maxI maxJ
checkResize = do
    vidDefWidth <- use $ globals.gVidDef.vdWidth

    let w = (vidDefWidth `shiftR` 3) - 2
        width = if w > Constants.maxCmdLine
                  then Constants.maxCmdLine
                  else w

    lineWidth <- use $ globals.gCon.cLineWidth

    unless (width == lineWidth) $ do
      if width < 1 -- video hasn't been initialized yet
        then do
          zoom (globals.gCon) $ do
            cLineWidth .= 38
            cTotalLines .= Constants.conTextSize `div` 38

          use (globals.gCon.cText) >>= \text ->
            io $ text `MSV.set` ' '

        else do
          oldWidth <- use $ globals.gCon.cLineWidth
          globals.gCon.cLineWidth .= width

          let totalLines = Constants.conTextSize `div` width
          oldTotalLines <- use $ globals.gCon.cTotalLines
          globals.gCon.cTotalLines .= totalLines

          let numLines = if totalLines < oldTotalLines
                           then totalLines
                           else oldTotalLines

          let numChars = if width < oldWidth
                           then width
                           else oldWidth

          currentLine <- use $ globals.gCon.cCurrent
          use (globals.gCon.cText) >>= \text -> do
            tbuf <- io $ MSV.clone text
            fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines width 0 0 numLines numChars

          clearNotify

      totalLines <- use $ globals.gCon.cTotalLines
      globals.gCon.cCurrent .= totalLines - 1
      globals.gCon.cDisplay .= totalLines - 1

  where fillInBuf :: MSV.IOVector Char -> MSV.IOVector Char -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Quake ()
        fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth i j maxI maxJ
          | i >= maxI = return ()
          | j >= maxJ = fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth (i + 1) 0 maxI maxJ
          | otherwise = do
              let idx = (totalLines - 1 - i) * lineWidth + j
                  idx2 = ((currentLine - i + oldTotalLines) `mod` oldTotalLines) * oldWidth + j
              io $ tbuf `MSV.read` idx2 >>= MSV.write text idx
              fillInBuf text tbuf oldTotalLines oldWidth currentLine totalLines lineWidth i (j + 1) maxI maxJ