diff options
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/buffer.nim | 1285 | ||||
| -rw-r--r-- | src/io/cell.nim | 305 | ||||
| -rw-r--r-- | src/io/lineedit.nim | 396 | ||||
| -rw-r--r-- | src/io/loader.nim | 7 | ||||
| -rw-r--r-- | src/io/serialize.nim | 79 | ||||
| -rw-r--r-- | src/io/term.nim | 1 |
6 files changed, 275 insertions, 1798 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim deleted file mode 100644 index f4cce8c4..00000000 --- a/src/io/buffer.nim +++ /dev/null @@ -1,1285 +0,0 @@ -import algorithm -import options -import os -import streams -import tables -import terminal -import unicode - -import css/cascade -import css/cssparser -import css/mediaquery -import css/sheet -import css/stylednode -import config/config -import html/dom -import html/tags -import html/htmlparser -import io/cell -import io/lineedit -import io/loader -import io/request -import io/term -import js/regex -import layout/box -import render/renderdocument -import render/rendertext -import types/color -import types/url -import utils/twtstr - -type - CursorPosition* = object - cursorx*: int - cursory*: int - xend*: int - fromx*: int - fromy*: int - - BufferMatch* = object - success*: bool - x*: int - y*: int - str*: string - - Buffer* = ref object - contenttype*: string - title*: string - lines*: FlexibleGrid - display*: FixedGrid - prevdisplay*: FixedGrid - statusmsg*: FixedGrid - hovertext*: string - width*: int - height*: int - cpos*: CursorPosition - attrs*: TermAttributes - document*: Document - viewport*: Viewport - prevstyled*: StyledNode - redraw*: bool - reshape*: bool - nostatus*: bool - location*: Url - ispipe*: bool - istream*: Stream - streamclosed*: bool - source*: string - prevnode*: StyledNode - userstyle*: CSSStylesheet - loader*: FileLoader - marks*: seq[Mark] - config*: Config - tty: File - -proc newBuffer*(config: Config, tty: File): Buffer = - new(result) - result.attrs = getTermAttributes(stdout) - result.width = result.attrs.width - result.height = result.attrs.height - 1 - result.config = config - result.loader = newFileLoader() - - result.display = newFixedGrid(result.width, result.height) - result.prevdisplay = newFixedGrid(result.width, result.height) - result.statusmsg = newFixedGrid(result.width) - -func cursorx*(buffer: Buffer): int {.inline.} = buffer.cpos.cursorx -func cursory*(buffer: Buffer): int {.inline.} = buffer.cpos.cursory -func fromx*(buffer: Buffer): int {.inline.} = buffer.cpos.fromx -func fromy*(buffer: Buffer): int {.inline.} = buffer.cpos.fromy -func xend*(buffer: Buffer): int {.inline.} = buffer.cpos.xend - -func generateFullOutput*(buffer: Buffer): string = - var x = 0 - var w = 0 - var format = newFormat() - result &= HVP(1, 1) - - for cell in buffer.display: - if x >= buffer.width: - result &= EL() - result &= "\r\n" - x = 0 - w = 0 - - result &= format.processFormat(cell.format) - result &= cell.str - - w += cell.width() - inc x - - result &= EL() - result &= "\r\n" - -# generate a sequence of instructions to replace the previous frame with the -# current one. ideally should be used when small changes are made (e.g. hover -# changes underlining) -func generateSwapOutput(buffer: Buffer): string = - var format = newFormat() - let curr = buffer.display - let prev = buffer.prevdisplay - var i = 0 - var x = 0 - var y = 0 - var line = "" - var lr = false - while i < curr.len: - if x >= buffer.width: - if lr: - result &= HVP(y + 1, 1) - result &= EL() - result &= line - lr = false - x = 0 - inc y - line = "" - lr = lr or (curr[i] != prev[i]) - line &= format.processFormat(curr[i].format) - line &= curr[i].str - inc i - inc x - if lr: - result &= HVP(y + 1, 1) - result &= EL() - result &= line - lr = false - - #TODO maybe fix this - #var x = 0 - #var y = 0 - #var cx = -1 - #var cy = -1 - #var i = 0 - #var text = "" - #while i < max: - # if x >= buffer.width: - # x = 0 - # inc y - - # if curr[i] != prev[i]: - # let currwidth = curr[i].runes.width() - # let prevwidth = prev[i].runes.width() - # if (curr[i].runes.len > 0 or currwidth < prevwidth) and (x != cx or y != cy): - # if text.len > 0: - # result &= text - # text = "" - # result &= HVP(y + 1, x + 1) - # cx = x - # cy = y - - # text &= format.processFormat(curr[i].format) - - # text &= $curr[i].runes - # if currwidth < prevwidth: - # var j = 0 - # while j < prevwidth - currwidth: - # text &= ' ' - # inc j - # if text.len > 0: - # inc cx - - # inc x - # inc i - #if text.len > 0: - # result &= $text - -func generateStatusMessage*(buffer: Buffer): string = - var format = newFormat() - var w = 0 - for cell in buffer.statusmsg: - result &= format.processFormat(cell.format) - result &= cell.str - w += cell.width() - if w < buffer.width: - result &= EL() - -func numLines(buffer: Buffer): int = buffer.lines.len - -func lastVisibleLine(buffer: Buffer): int = min(buffer.fromy + buffer.height, buffer.numLines) - -func currentLineWidth(buffer: Buffer): int = - return buffer.lines[buffer.cursory].width() - -func maxfromy(buffer: Buffer): int = max(buffer.numLines - buffer.height, 0) - -func maxfromx(buffer: Buffer): int = max(buffer.currentLineWidth() - buffer.width, 0) - -func acursorx(buffer: Buffer): int = - return max(0, buffer.cursorx - buffer.fromx) - -func acursory(buffer: Buffer): int = - return buffer.cursory - buffer.fromy - -func cellOrigin(buffer: Buffer, x, y: int): int = - let row = y * buffer.width - var ox = x - while ox > 0 and buffer.display[row + ox].str.len == 0: - dec ox - return ox - -func currentCellOrigin(buffer: Buffer): int = - return buffer.cellOrigin(buffer.acursorx, buffer.acursory) - -func currentDisplayCell(buffer: Buffer): FixedCell = - let row = (buffer.cursory - buffer.fromy) * buffer.width - return buffer.display[row + buffer.currentCellOrigin()] - -func getLink(node: StyledNode): HTMLAnchorElement = - if node == nil: - return nil - if node.t == STYLED_ELEMENT and node.node != nil and Element(node.node).tagType == TAG_A: - return HTMLAnchorElement(node.node) - if node.node != nil: - return HTMLAnchorElement(node.node.findAncestor({TAG_A})) - #TODO ::before links? - -const ClickableElements = { - TAG_A, TAG_INPUT, TAG_OPTION -} - -func getClickable(styledNode: StyledNode): Element = - if styledNode == nil or styledNode.node == nil: - return nil - if styledNode.t == STYLED_ELEMENT: - let element = Element(styledNode.node) - if element.tagType in ClickableElements: - return element - styledNode.node.findAncestor(ClickableElements) - -func getCursorClickable(buffer: Buffer): Element = - return buffer.currentDisplayCell().node.getClickable() - -func currentLine(buffer: Buffer): string = - return buffer.lines[buffer.cursory].str - -func cursorBytes(buffer: Buffer, y: int, cc = buffer.fromx + buffer.cursorx): int = - assert y < buffer.lines.len - let line = buffer.lines[y].str - var w = 0 - var i = 0 - while i < line.len and w < cc: - var r: Rune - fastRuneAt(line, i, r) - w += r.width() - return i - -func currentCursorBytes(buffer: Buffer, cc = buffer.fromx + buffer.cursorx): int = - return buffer.cursorBytes(buffer.cursory, cc) - -func currentWidth(buffer: Buffer): int = - let line = buffer.currentLine - if line.len == 0: return 0 - var w = 0 - var i = 0 - let cc = buffer.fromx + buffer.cursorx - var r: Rune - fastRuneAt(line, i, r) - while i < line.len and w < cc: - fastRuneAt(line, i, r) - w += r.width() - return r.width() - -func prevWidth(buffer: Buffer): int = - let line = buffer.currentLine - if line.len == 0: return 0 - var w = 0 - var i = 0 - let cc = buffer.fromx + buffer.cursorx - var pr: Rune - var r: Rune - fastRuneAt(line, i, r) - while i < line.len and w < cc: - pr = r - fastRuneAt(line, i, r) - w += r.width() - return pr.width() - -func maxScreenWidth(buffer: Buffer): int = - for line in buffer.lines[buffer.fromy..buffer.lastVisibleLine - 1]: - result = max(line.width(), result) - -func atPercentOf(buffer: Buffer): int = - if buffer.lines.len == 0: return 100 - return (100 * (buffer.cursory + 1)) div buffer.numLines - -func hasAnchor*(buffer: Buffer, anchor: string): bool = - return buffer.document.getElementById(anchor) != nil - -func getTitle(buffer: Buffer): string = - if buffer.document != nil: - result = buffer.document.title - if result != "": return result - if buffer.ispipe: - return "*pipe*" - return buffer.location.serialize(excludepassword = true) - -proc clearDisplay(buffer: Buffer) = - buffer.prevdisplay = buffer.display - buffer.display = newFixedGrid(buffer.width, buffer.height) - -proc refreshDisplay(buffer: Buffer) = - var r: Rune - var y = 0 # y position on screen - buffer.clearDisplay() - - for by in buffer.fromy..buffer.lastVisibleLine - 1: - let line = buffer.lines[by] # by: y position in lines - var w = 0 # width of the row so far - var i = 0 # byte in line.str - - # Skip cells till buffer.fromx. - while w < buffer.fromx and i < line.str.len: - fastRuneAt(line.str, i, r) - w += r.width() - - let dls = y * buffer.width # starting position of row in display - - # Fill in the gap in case we skipped more cells than fromx mandates (i.e. - # we encountered a double-width character.) - var k = 0 - if w > buffer.fromx: - while k < w - buffer.fromx: - buffer.display[dls + k].str &= ' ' - inc k - - var cf = line.findFormat(w) - var nf = line.findNextFormat(w) - - let startw = w # save this for later - - # Now fill in the visible part of the row. - while i < line.str.len: - let pw = w - fastRuneAt(line.str, i, r) - w += r.width() - if w > buffer.fromx + buffer.width: - break # die on exceeding the width limit - if nf.pos != -1 and nf.pos <= pw: - cf = nf - nf = line.findNextFormat(pw) - buffer.display[dls + k].str &= r - if cf.pos != -1: - buffer.display[dls + k].format = cf.format - buffer.display[dls + k].node = cf.node - let tk = k + r.width() - while k < tk and k < buffer.width - 1: - inc k - - # Then, for each cell that has a mark, override its formatting with that - # specified by the mark. - var l = 0 - while l < buffer.marks.len and buffer.marks[l].y < by: - inc l # linear search to find the first applicable mark - let aw = buffer.width - (startw - buffer.fromx) # actual width - while l < buffer.marks.len and buffer.marks[l].y == by: - let mark = buffer.marks[l] - inc l - if mark.x >= startw + aw or mark.x + mark.width < startw: continue - for i in max(mark.x, startw)..<min(mark.x + mark.width, startw + aw): - buffer.display[dls + i - startw].format = mark.format - - inc y - -proc setCursorX(buffer: Buffer, x: int, refresh = true, save = true) = - if (not refresh) or (buffer.fromx <= x and x < buffer.fromx + buffer.width): - buffer.cpos.cursorx = x - else: - if refresh and buffer.fromx > buffer.cursorx: - buffer.cpos.fromx = max(buffer.currentLineWidth() - 1, 0) - buffer.cpos.cursorx = buffer.fromx - elif x > buffer.cursorx: - buffer.cpos.fromx = max(x - buffer.width + 1, 0) - buffer.cpos.cursorx = x - elif x < buffer.cursorx: - buffer.cpos.fromx = x - buffer.cpos.cursorx = x - buffer.redraw = true - if save: - buffer.cpos.xend = buffer.cursorx - -proc restoreCursorX(buffer: Buffer) = - buffer.setCursorX(max(min(buffer.currentLineWidth() - 1, buffer.xend), 0), false, false) - -proc setCursorY(buffer: Buffer, y: int) = - if buffer.cursory == y: - return - if y - buffer.fromy >= 0 and y - buffer.height < buffer.fromy: - buffer.cpos.cursory = y - else: - if y > buffer.cursory: - buffer.cpos.fromy = max(y - buffer.height + 1, 0) - else: - buffer.cpos.fromy = min(y, buffer.maxfromy) - buffer.cpos.cursory = y - buffer.redraw = true - buffer.restoreCursorX() - -proc centerLine*(buffer: Buffer) = - let ny = max(min(buffer.cursory - buffer.height div 2, buffer.numLines - buffer.height), 0) - if ny != buffer.fromy: - buffer.cpos.fromy = ny - buffer.redraw = true - -proc setCursorXY*(buffer: Buffer, x, y: int) = - let fy = buffer.fromy - buffer.setCursorY(max(min(y, buffer.numLines - 1), 0)) - buffer.setCursorX(max(min(buffer.currentLineWidth(), x), 0)) - if fy != buffer.fromy: - buffer.centerLine() - -proc setFromXY*(buffer: Buffer, x, y: int) = - buffer.cpos.fromy = max(min(y, buffer.maxfromy), 0) - buffer.cpos.fromx = max(min(x, buffer.maxfromx), 0) - -proc cursorDown*(buffer: Buffer) = - if buffer.cursory < buffer.numLines - 1: - buffer.setCursorY(buffer.cursory + 1) - -proc cursorUp*(buffer: Buffer) = - if buffer.cursory > 0: - buffer.setCursorY(buffer.cursory - 1) - -proc cursorRight*(buffer: Buffer) = - let cellwidth = buffer.currentWidth() - if buffer.cursorx + cellwidth < buffer.currentLineWidth(): - buffer.setCursorX(buffer.cursorx + cellwidth) - -proc cursorLeft*(buffer: Buffer) = - buffer.setCursorX(max(buffer.cursorx - buffer.prevWidth(), 0)) - -proc cursorLineBegin*(buffer: Buffer) = - buffer.setCursorX(0) - -proc cursorLineEnd*(buffer: Buffer) = - buffer.setCursorX(max(buffer.currentLineWidth() - 1, 0)) - -proc cursorNextWord*(buffer: Buffer) = - var r: Rune - var b = buffer.currentCursorBytes() - var x = buffer.cursorx - while b < buffer.currentLine.len: - let pb = b - fastRuneAt(buffer.currentLine, b, r) - if r.breaksWord(): - b = pb - break - x += r.width() - - while b < buffer.currentLine.len: - let pb = b - fastRuneAt(buffer.currentLine, b, r) - if not r.breaksWord(): - b = pb - break - x += r.width() - - if b < buffer.currentLine.len: - buffer.setCursorX(x) - else: - if buffer.cursory < buffer.numLines - 1: - buffer.cursorDown() - buffer.cursorLineBegin() - else: - buffer.cursorLineEnd() - -proc cursorPrevWord*(buffer: Buffer) = - var b = buffer.currentCursorBytes() - var x = buffer.cursorx - if buffer.currentLine.len > 0: - b = min(b, buffer.currentLine.len - 1) - while b >= 0: - let (r, o) = lastRune(buffer.currentLine, b) - if r.breaksWord(): - break - b -= o - x -= r.width() - - while b >= 0: - let (r, o) = lastRune(buffer.currentLine, b) - if not r.breaksWord(): - break - b -= o - x -= r.width() - else: - b = -1 - - if b >= 0: - buffer.setCursorX(x) - else: - if buffer.cursory > 0: - buffer.cursorUp() - buffer.cursorLineEnd() - else: - buffer.cursorLineBegin() - -proc cursorNextLink*(buffer: Buffer) = - let line = buffer.lines[buffer.cursory] - var i = line.findFormatN(buffer.cursorx) - 1 - var link: Element = nil - if i >= 0: - link = line.formats[i].node.getClickable() - inc i - - while i < line.formats.len: - let format = line.formats[i] - let fl = format.node.getClickable() - if fl != nil and fl != link: - buffer.setCursorX(format.pos) - return - inc i - - for y in (buffer.cursory + 1)..(buffer.numLines - 1): - let line = buffer.lines[y] - i = 0 - while i < line.formats.len: - let format = line.formats[i] - let fl = format.node.getClickable() - if fl != nil and fl != link: - buffer.setCursorXY(format.pos, y) - return - inc i - -proc cursorPrevLink*(buffer: Buffer) = - let line = buffer.lines[buffer.cursory] - var i = line.findFormatN(buffer.cursorx) - 1 - var link: Element = nil - if i >= 0: - link = line.formats[i].node.getClickable() - dec i - - var ly = 0 #last y - var lx = 0 #last x - template link_beginning() = - #go to beginning of link - ly = y #last y - lx = format.pos #last x - - #on the current line - let line = buffer.lines[y] - while i >= 0: - let format = line.formats[i] - let nl = format.node.getClickable() - if nl == fl: - lx = format.pos - dec i - - #on previous lines - for iy in countdown(ly - 1, 0): - let line = buffer.lines[iy] - i = line.formats.len - 1 - while i >= 0: - let format = line.formats[i] - let nl = format.node.getClickable() - if nl == fl: - ly = iy - lx = format.pos - dec i - - while i >= 0: - let format = line.formats[i] - let fl = format.node.getClickable() - if fl != nil and fl != link: - let y = buffer.cursory - link_beginning - buffer.setCursorXY(lx, ly) - return - dec i - - for y in countdown(buffer.cursory - 1, 0): - let line = buffer.lines[y] - i = line.formats.len - 1 - while i >= 0: - let format = line.formats[i] - let fl = format.node.getClickable() - if fl != nil and fl != link: - link_beginning - buffer.setCursorXY(lx, ly) - return - dec i - -proc cursorFirstLine*(buffer: Buffer) = - buffer.setCursorY(0) - -proc cursorLastLine*(buffer: Buffer) = - buffer.setCursorY(buffer.numLines - 1) - -proc cursorTop*(buffer: Buffer) = - buffer.setCursorY(buffer.fromy) - -proc cursorMiddle*(buffer: Buffer) = - buffer.setCursorY(min(buffer.fromy + (buffer.height - 2) div 2, buffer.numLines - 1)) - -proc cursorBottom*(buffer: Buffer) = - buffer.setCursorY(min(buffer.fromy + buffer.height - 1, buffer.numLines - 1)) - -proc cursorLeftEdge*(buffer: Buffer) = - buffer.setCursorX(buffer.fromx) - -proc cursorVertMiddle*(buffer: Buffer) = - buffer.setCursorX(min(buffer.fromx + (buffer.width - 2) div 2, buffer.currentLineWidth)) - -proc cursorRightEdge*(buffer: Buffer) = - buffer.setCursorX(min(buffer.fromx + buffer.width - 1, buffer.currentLineWidth)) - -proc halfPageUp*(buffer: Buffer) = - buffer.cpos.cursory = max(buffer.cursory - buffer.height div 2 + 1, 0) - let nfy = max(0, buffer.fromy - buffer.height div 2 + 1) - if nfy != buffer.fromy: - buffer.cpos.fromy = nfy - buffer.redraw = true - buffer.restoreCursorX() - -proc halfPageDown*(buffer: Buffer) = - buffer.cpos.cursory = min(buffer.cursory + buffer.height div 2 - 1, buffer.numLines - 1) - let nfy = min(max(buffer.numLines - buffer.height, 0), buffer.fromy + buffer.height div 2 - 1) - if nfy != buffer.fromy: - buffer.cpos.fromy = nfy - buffer.redraw = true - buffer.restoreCursorX() - -proc pageUp*(buffer: Buffer) = - buffer.cpos.cursory = max(buffer.cursory - buffer.height, 0) - let nfy = max(0, buffer.fromy - buffer.height) - if nfy != buffer.fromy: - buffer.cpos.fromy = nfy - buffer.redraw = true - buffer.restoreCursorX() - -proc pageDown*(buffer: Buffer) = - buffer.cpos.cursory = min(buffer.cursory + buffer.height, buffer.numLines - 1) - let nfy = min(buffer.fromy + buffer.height, max(buffer.numLines - buffer.height, 0)) - if nfy != buffer.fromy: - buffer.cpos.fromy = nfy - buffer.redraw = true - buffer.restoreCursorX() - -proc pageLeft*(buffer: Buffer) = - buffer.cpos.cursorx = max(buffer.cursorx - buffer.width, 0) - let nfx = max(0, buffer.fromx - buffer.width) - if nfx != buffer.fromx: - buffer.cpos.fromx = nfx - buffer.redraw = true - -proc pageRight*(buffer: Buffer) = - buffer.cpos.cursorx = min(buffer.fromx, buffer.currentLineWidth()) - let nfx = min(max(buffer.maxScreenWidth() - buffer.width, 0), buffer.fromx + buffer.width) - if nfx != buffer.fromx: - buffer.cpos.fromx = nfx - buffer.redraw = true - -proc scrollDown*(buffer: Buffer) = - if buffer.fromy + buffer.height < buffer.numLines: - inc buffer.cpos.fromy - if buffer.fromy > buffer.cursory: - buffer.cursorDown() - buffer.redraw = true - else: - buffer.cursorDown() - -proc scrollUp*(buffer: Buffer) = - if buffer.fromy > 0: - dec buffer.cpos.fromy - if buffer.fromy + buffer.height <= buffer.cursory: - buffer.cursorUp() - buffer.redraw = true - else: - buffer.cursorUp() - -proc scrollRight*(buffer: Buffer) = - if buffer.fromx + buffer.width < buffer.maxScreenWidth(): - inc buffer.cpos.fromx - buffer.redraw = true - -proc scrollLeft*(buffer: Buffer) = - if buffer.fromx > 0: - dec buffer.cpos.fromx - if buffer.cursorx < buffer.fromx: - buffer.setCursorX(max(buffer.currentLineWidth() - 1, 0)) - buffer.redraw = true - -proc gotoAnchor*(buffer: Buffer) = - if buffer.document == nil: return - let anchor = buffer.document.getElementById(buffer.location.anchor) - if anchor == nil: return - for y in 0..<buffer.numLines: - let line = buffer.lines[y] - var i = 0 - while i < line.formats.len: - let format = line.formats[i] - if format.node != nil and anchor in format.node.node: - buffer.setCursorY(y) - buffer.centerLine() - buffer.setCursorX(format.pos) - return - inc i - -proc addMark*(buffer: Buffer, x, y, width: int): Mark = - assert y < buffer.lines.len - var format = newFormat() - format.bgcolor = buffer.config.markcolor - result = Mark(x: x, y: y, width: width, format: format) - let previ = upperBound(buffer.marks, y, (proc(a: Mark, b: int): int = cmp(a.y, b))) - buffer.marks.insert(result, previ) - -proc removeMark*(buffer: Buffer, mark: Mark) = - let i = buffer.marks.find(mark) - if i != -1: - buffer.marks.delete(i) - -proc cursorNextMatch(buffer: Buffer, regex: Regex, sy, ey: int, wrap = false): BufferMatch = - for y in sy..ey: - let s = if y == buffer.cursory and not wrap: - buffer.currentCursorBytes(buffer.fromx + buffer.cursorx + 1) - else: - 0 - let res = regex.exec(buffer.lines[y].str, s) - if res.success and res.captures.len > 0: - let cap = res.captures[0] - let x = buffer.lines[y].str.width(cap.s) - buffer.setCursorXY(x, y) - result.success = true - result.y = y - result.x = x - result.str = buffer.lines[y].str.substr(cap.s, cap.e - 1) - return - -proc cursorNextMatch*(buffer: Buffer, regex: Regex, wrap = true): BufferMatch = - let s = buffer.currentCursorBytes(buffer.fromx + buffer.cursorx + 1) - var low = buffer.cursory - if s == buffer.lines.len: - low += 1 - if low > buffer.lines.high: - low = 0 - let ret = buffer.cursorNextMatch(regex, low, buffer.lines.high) - if ret.success: - return ret - if wrap: - return buffer.cursorNextMatch(regex, 0, low, true) - -proc cursorPrevMatch*(buffer: Buffer, regex: Regex, sy, ey: int, wrap = false): BufferMatch = - for y in countdown(sy, ey): - let e = if y == buffer.cursory and not wrap: - buffer.currentCursorBytes() - else: - buffer.lines[y].str.len + 1 - let res = regex.exec(buffer.lines[y].str) - if res.success: - for i in countdown(res.captures.high, 0): - let cap = res.captures[i] - if cap.s < e: - let x = buffer.lines[y].str.width(cap.s) - buffer.setCursorXY(x, y) - result.success = true - result.y = y - result.x = x - result.str = buffer.lines[y].str.substr(cap.s, cap.e - 1) - return - -proc cursorPrevMatch*(buffer: Buffer, regex: Regex, wrap = true): BufferMatch = - var high = buffer.cursory - if buffer.fromx + buffer.cursorx - 1 < 0: - high -= 1 - if high < 0: - high = buffer.lines.high - let ret = buffer.cursorPrevMatch(regex, high, 0) - if ret.success: - return ret - if wrap: - return buffer.cursorPrevMatch(regex, buffer.lines.high, high) - -proc refreshTermAttrs*(buffer: Buffer): bool = - let newAttrs = getTermAttributes(stdout) - if newAttrs != buffer.attrs: - buffer.attrs = newAttrs - buffer.width = newAttrs.width - buffer.height = newAttrs.height - 1 - return true - return false - -proc updateCursor(buffer: Buffer) = - if buffer.fromy > buffer.lastVisibleLine - 1: - buffer.cpos.fromy = 0 - buffer.cpos.cursory = buffer.lastVisibleLine - 1 - - if buffer.cursory >= buffer.lines.len: - buffer.cpos.cursory = max(0, buffer.lines.len - 1) - - if buffer.lines.len == 0: - buffer.cpos.cursory = 0 - -proc updateHover(buffer: Buffer) = - let thisnode = buffer.currentDisplayCell().node - let prevnode = buffer.prevnode - - if thisnode != prevnode and (thisnode == nil or prevnode == nil or thisnode.node != prevnode.node): - for styledNode in thisnode.branch: - if styledNode.t == STYLED_ELEMENT and styledNode.node != nil: - let elem = Element(styledNode.node) - if not elem.hover: - elem.hover = true - buffer.reshape = true - - let link = thisnode.getLink() - if link != nil: - buffer.hovertext = link.href - else: - buffer.hovertext = "" - - for styledNode in prevnode.branch: - if styledNode.t == STYLED_ELEMENT and styledNode.node != nil: - let elem = Element(styledNode.node) - if elem.hover: - elem.hover = false - buffer.reshape = true - - buffer.prevnode = thisnode - -proc loadResource(buffer: Buffer, document: Document, elem: HTMLLinkElement) = - let url = parseUrl(elem.href, document.location.some) - if url.isSome: - let url = url.get - if url.scheme == buffer.location.scheme: - let media = elem.media - if media != "": - let media = parseMediaQueryList(parseListOfComponentValues(newStringStream(media))) - if not media.applies(): return - let fs = buffer.loader.doRequest(newRequest(url)) - if fs.body != nil and fs.contenttype == "text/css": - elem.sheet = parseStylesheet(fs.body) - -proc loadResources(buffer: Buffer, document: Document) = - var stack: seq[Element] - if document.html != nil: - stack.add(document.html) - while stack.len > 0: - let elem = stack.pop() - - if elem.tagType == TAG_LINK: - let elem = HTMLLinkElement(elem) - if elem.rel == "stylesheet": - buffer.loadResource(document, elem) - - for child in elem.children_rev: - stack.add(child) - -proc load*(buffer: Buffer) = - case buffer.contenttype - of "text/html": - if not buffer.streamclosed: - buffer.source = buffer.istream.readAll() - buffer.istream.close() - buffer.istream = newStringStream(buffer.source) - buffer.document = parseHTML5(buffer.istream) - buffer.streamclosed = true - else: - buffer.document = parseHTML5(newStringStream(buffer.source)) - buffer.document.location = buffer.location - buffer.loadResources(buffer.document) - else: - if not buffer.streamclosed: - buffer.source = buffer.istream.readAll() - buffer.istream.close() - buffer.streamclosed = true - buffer.lines = renderPlainText(buffer.source) - -proc render*(buffer: Buffer) = - case buffer.contenttype - of "text/html": - if buffer.viewport == nil: - buffer.viewport = Viewport(term: buffer.attrs) - if buffer.userstyle == nil: - buffer.userstyle = buffer.config.stylesheet.parseStylesheet() - let ret = renderDocument(buffer.document, buffer.attrs, buffer.userstyle, buffer.viewport, buffer.prevstyled) - buffer.lines = ret[0] - buffer.prevstyled = ret[1] - else: discard - buffer.updateCursor() - -proc cursorBufferPos(buffer: Buffer) = - let x = buffer.acursorx - let y = buffer.acursory - print(HVP(y + 1, x + 1)) - -proc clearStatusMessage(buffer: Buffer) = - buffer.statusmsg = newFixedGrid(buffer.width) - -proc writeStatusMessage(buffer: Buffer, str: string, format: Format = Format()) = - buffer.clearStatusMessage() - var i = 0 - for r in str.runes: - i += r.width() - if i >= buffer.statusmsg.len: - buffer.statusmsg[^1].str = "$" - break - buffer.statusmsg[i].str &= r - buffer.statusmsg[i].format = format - -proc statusMsgForBuffer(buffer: Buffer) = - var msg = $(buffer.cursory + 1) & "/" & $buffer.numLines & " (" & - $buffer.atPercentOf() & "%) " & "<" & buffer.title & ">" - if buffer.hovertext.len > 0: - msg &= " " & buffer.hovertext - var format: Format - format.reverse = true - buffer.writeStatusMessage(msg, format) - -proc setStatusMessage*(buffer: Buffer, str: string) = - buffer.writeStatusMessage(str) - buffer.nostatus = true - -proc lineInfo*(buffer: Buffer) = - buffer.setStatusMessage("line " & $(buffer.cursory + 1) & "/" & $buffer.numLines & " col " & $(buffer.cursorx + 1) & "/" & $buffer.currentLineWidth() & " x: " & $buffer.currentCursorBytes()) - -proc displayBufferSwapOutput(buffer: Buffer) = - print(buffer.generateSwapOutput()) - -proc displayBuffer(buffer: Buffer) = - print(buffer.generateFullOutput()) - -proc displayStatusMessage*(buffer: Buffer) = - print(HVP(buffer.height + 1, 1)) - print(SGR()) - print(buffer.generateStatusMessage()) - print(SGR()) - -# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set -proc constructEntryList(form: HTMLFormElement, submitter: Element = nil, encoding: string = ""): seq[tuple[name, value: string]] = - if form.constructingentrylist: - return - form.constructingentrylist = true - - var entrylist: seq[tuple[name, value: string]] - for field in form.controls: - if field.findAncestor({TAG_DATALIST}) != nil or - field.attrb("disabled") or - field.isButton() and Element(field) != submitter: - continue - - if field.tagType == TAG_INPUT: - let field = HTMLInputElement(field) - if field.inputType == INPUT_IMAGE: - let name = if field.attr("name") != "": - field.attr("name") & '.' - else: - "" - entrylist.add((name & 'x', $field.xcoord)) - entrylist.add((name & 'y', $field.ycoord)) - continue - - #TODO custom elements - - let name = field.attr("name") - - if name == "": - continue - - if field.tagType == TAG_SELECT: - let field = HTMLSelectElement(field) - for option in field.options: - if option.selected or option.disabled: - entrylist.add((name, option.value)) - elif field.tagType == TAG_INPUT and HTMLInputElement(field).inputType in {INPUT_CHECKBOX, INPUT_RADIO}: - let value = if field.attr("value") != "": - field.attr("value") - else: - "on" - entrylist.add((name, value)) - elif field.tagType == TAG_INPUT and HTMLInputElement(field).inputType == INPUT_FILE: - #TODO file - discard - elif field.tagType == TAG_INPUT and HTMLInputElement(field).inputType == INPUT_HIDDEN and name.equalsIgnoreCase("_charset_"): - let charset = if encoding != "": - encoding - else: - "UTF-8" - entrylist.add((name, charset)) - else: - if field.tagType == TAG_INPUT: - entrylist.add((name, HTMLInputElement(field).value)) - else: - assert false - if field.tagType == TAG_TEXTAREA or - field.tagType == TAG_INPUT and HTMLInputElement(field).inputType in {INPUT_TEXT, INPUT_SEARCH}: - if field.attr("dirname") != "": - let dirname = field.attr("dirname") - let dir = "ltr" #TODO bidi - entrylist.add((dirname, dir)) - - form.constructingentrylist = false - return entrylist - -#https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm -proc makeCRLF(s: string): string = - result = newStringOfCap(s.len) - var i = 0 - while i < s.len - 1: - if s[i] == '\r' and s[i + 1] != '\n': - result &= '\r' - result &= '\n' - elif s[i] != '\r' and s[i + 1] == '\n': - result &= s[i] - result &= '\r' - result &= '\n' - inc i - else: - result &= s[i] - inc i - -proc serializeMultipartFormData(kvs: seq[(string, string)]): MimeData = - for it in kvs: - let name = makeCRLF(it[0]) - let value = makeCRLF(it[1]) - result[name] = value - -proc serializePlainTextFormData(kvs: seq[(string, string)]): string = - for it in kvs: - let (name, value) = it - result &= name - result &= '=' - result &= value - result &= "\r\n" - -proc submitForm(form: HTMLFormElement, submitter: Element): Option[Request] = - let entrylist = form.constructEntryList(submitter) - - let action = if submitter.action() == "": - $form.document.location - else: - submitter.action() - - let url = parseUrl(action, submitter.document.baseUrl.some) - if url.isnone: - return none(Request) - - var parsedaction = url.get - let scheme = parsedaction.scheme - let enctype = submitter.enctype() - let formmethod = submitter.formmethod() - if formmethod == FORM_METHOD_DIALOG: - #TODO - return none(Request) - let httpmethod = if formmethod == FORM_METHOD_GET: - HTTP_GET - else: - assert formmethod == FORM_METHOD_POST - HTTP_POST - - #let target = if submitter.isSubmitButton() and submitter.attrb("formtarget"): - # submitter.attr("formtarget") - #else: - # submitter.target() - #let noopener = true #TODO - - template mutateActionUrl() = - let query = serializeApplicationXWWWFormUrlEncoded(entrylist) - parsedaction.query = query.some - return newRequest(parsedaction, httpmethod).some - - template submitAsEntityBody() = - var mimetype: string - var body = none(string) - var multipart = none(MimeData) - case enctype - of FORM_ENCODING_TYPE_URLENCODED: - body = serializeApplicationXWWWFormUrlEncoded(entrylist).some - mimeType = $enctype - of FORM_ENCODING_TYPE_MULTIPART: - multipart = serializeMultipartFormData(entrylist).some - mimetype = $enctype - of FORM_ENCODING_TYPE_TEXT_PLAIN: - body = serializePlainTextFormData(entrylist).some - mimetype = $enctype - return newRequest(parsedaction, httpmethod, {"Content-Type": mimetype}, body, multipart).some - - template getActionUrl() = - return newRequest(parsedaction).some - - case scheme - of "http", "https": - if formmethod == FORM_METHOD_GET: - mutateActionUrl - else: - assert formmethod == FORM_METHOD_POST - submitAsEntityBody - of "ftp": - getActionUrl - of "data": - if formmethod == FORM_METHOD_GET: - mutateActionUrl - else: - assert formmethod == FORM_METHOD_POST - getActionUrl - -proc click*(buffer: Buffer): Option[Request] = - let clickable = buffer.getCursorClickable() - if clickable != nil: - template set_focus(e: Element) = - if buffer.document.focus != e: - buffer.document.focus = e - buffer.reshape = true - template restore_focus = - if buffer.document.focus != nil: - buffer.document.focus = nil - buffer.reshape = true - case clickable.tagType - of TAG_SELECT: - set_focus clickable - of TAG_A: - restore_focus - let url = parseUrl(HTMLAnchorElement(clickable).href, clickable.document.baseUrl.some) - if url.issome: - return newRequest(url.get, HTTP_GET).some - of TAG_OPTION: - let option = HTMLOptionElement(clickable) - let select = option.select - if select != nil: - if buffer.document.focus == select: - # select option - if not select.attrb("multiple"): - for option in select.options: - option.selected = false - option.selected = true - restore_focus - else: - # focus on select - set_focus select - of TAG_INPUT: - restore_focus - let input = HTMLInputElement(clickable) - case input.inputType - of INPUT_SEARCH: - var value = input.value - print(HVP(buffer.height + 1, 1)) - print(EL()) - let status = readLine("SEARCH: ", value, buffer.width, {'\r', '\n'}, config = buffer.config, tty = buffer.tty) - if status: - input.value = value - input.invalid = true - buffer.reshape = true - if input.form != nil: - let submitaction = submitForm(input.form, input) - return submitaction - of INPUT_TEXT, INPUT_PASSWORD: - var value = input.value - print(HVP(buffer.height + 1, 1)) - print(EL()) - let status = readLine("TEXT: ", value, buffer.width, {'\r', '\n'}, input.inputType == INPUT_PASSWORD, config = buffer.config, tty = buffer.tty) - if status: - input.value = value - input.invalid = true - buffer.reshape = true - of INPUT_FILE: - var path = if input.file.issome: - input.file.get.path.serialize_unicode() - else: - "" - print(HVP(buffer.height + 1, 1)) - print(EL()) - let status = readLine("Filename: ", path, buffer.width, {'\r', '\n'}, config = buffer.config, tty = buffer.tty) - if status: - let cdir = parseUrl("file://" & getCurrentDir() & DirSep) - let path = parseUrl(path, cdir) - if path.issome: - input.file = path - input.invalid = true - buffer.reshape = true - of INPUT_CHECKBOX: - input.checked = not input.checked - input.invalid = true - buffer.reshape = true - of INPUT_RADIO: - for radio in input.radiogroup: - radio.checked = false - radio.invalid = true - input.checked = true - input.invalid = true - buffer.reshape = true - of INPUT_RESET: - if input.form != nil: - input.form.reset() - buffer.reshape = true - of INPUT_SUBMIT, INPUT_BUTTON: - if input.form != nil: - let submitaction = submitForm(input.form, input) - return submitaction - else: - restore_focus - else: - restore_focus - -proc setupBuffer*(buffer: Buffer) = - buffer.load() - buffer.render() - buffer.gotoAnchor() - buffer.redraw = true - -proc dupeBuffer*(buffer: Buffer, location = none(URL)): Buffer = - let clone = newBuffer(buffer.config, buffer.tty) - clone.contenttype = buffer.contenttype - clone.ispipe = buffer.ispipe - if location.isSome: - clone.location = location.get - else: - clone.location = buffer.location - clone.istream = newStringStream(buffer.source) - clone.setupBuffer() - return clone - -proc drawBuffer*(buffer: Buffer) = - var format = newFormat() - for line in buffer.lines: - if line.formats.len == 0: - print(line.str & "\n") - else: - var x = 0 - var i = 0 - for f in line.formats: - var outstr = "" - #assert f.pos < line.str.width(), "fpos " & $f.pos & "\nstr" & line.str & "\n" - while x < f.pos: - var r: Rune - fastRuneAt(line.str, i, r) - outstr &= r - x += r.width() - print(outstr) - print(format.processFormat(f.format)) - print(line.str.substr(i)) - print(format.processFormat(newFormat())) - print("\n") - -proc refreshTitle*(buffer: Buffer) = - buffer.title = buffer.getTitle() - -proc refreshBuffer*(buffer: Buffer, peek = false): bool {.discardable.} = - buffer.refreshTitle() - stdout.hideCursor() - - if buffer.refreshTermAttrs(): - buffer.redraw = true - buffer.reshape = true - - if buffer.redraw: - buffer.refreshDisplay() - buffer.displayBuffer() - #result = true - buffer.redraw = false - - if not peek: - buffer.updateHover() - - if buffer.reshape: - buffer.render() - buffer.reshape = false - buffer.refreshDisplay() - buffer.displayBufferSwapOutput() - #result = true - - if not peek: - if not buffer.nostatus: - buffer.statusMsgForBuffer() - else: - buffer.nostatus = false - buffer.displayStatusMessage() - buffer.cursorBufferPos() - stdout.showCursor() diff --git a/src/io/cell.nim b/src/io/cell.nim deleted file mode 100644 index 8c5333b1..00000000 --- a/src/io/cell.nim +++ /dev/null @@ -1,305 +0,0 @@ -import sequtils -import streams -import strutils -import sugar - -import css/stylednode -import layout/box -import types/color -import utils/twtstr - -type - FormatFlags* = enum - FLAG_BOLD - FLAG_ITALIC - FLAG_UNDERLINE - FLAG_REVERSE - FLAG_STRIKE - FLAG_OVERLINE - FLAG_BLINK - - Format* = object - fgcolor*: CellColor - bgcolor*: CellColor - flags: set[FormatFlags] - - Cell* = object of RootObj - format*: Format - node*: StyledNode - - # A FormatCell *starts* a new terminal formatting context. - # If no FormatCell exists before a given cell, the default formatting is used. - FormatCell* = object of Cell - pos*: int - computed*: ComputedFormat - - FlexibleLine* = object - str*: string - formats*: seq[FormatCell] - - Mark* = ref object - x*: int - y*: int - width*: int - format*: Format - - FlexibleGrid* = seq[FlexibleLine] - - FixedCell* = object of Cell - str*: string - - FixedGrid* = seq[FixedCell] - -const FormatCodes: array[FormatFlags, tuple[s: int, e: int]] = [ - FLAG_BOLD: (1, 22), - FLAG_ITALIC: (3, 23), - FLAG_UNDERLINE: (4, 24), - FLAG_REVERSE: (7, 27), - FLAG_STRIKE: (9, 29), - FLAG_OVERLINE: (53, 55), - FLAG_BLINK: (5, 25), -] - -template flag_template(format: Format, val: bool, flag: FormatFlags) = - if val: format.flags.incl(flag) - else: format.flags.excl(flag) - -template `italic=`*(f: var Format, b: bool) = flag_template f, b, FLAG_ITALIC -template `bold=`*(f: var Format, b: bool) = flag_template f, b, FLAG_BOLD -template `underline=`*(f: var Format, b: bool) = flag_template f, b, FLAG_UNDERLINE -template `reverse=`*(f: var Format, b: bool) = flag_template f, b, FLAG_REVERSE -template `strike=`*(f: var Format, b: bool) = flag_template f, b, FLAG_STRIKE -template `overline=`*(f: var Format, b: bool) = flag_template f, b, FLAG_OVERLINE -template `blink=`*(f: var Format, b: bool) = flag_template f, b, FLAG_BLINK - -func `==`*(a: FixedCell, b: FixedCell): bool = - return a.format == b.format and - a.str == b.str and - a.node == b.node - -func newFixedGrid*(w: int, h: int = 1): FixedGrid = - return newSeq[FixedCell](w * h) - -func width*(line: FlexibleLine): int = - return line.str.width() - -func width*(cell: FixedCell): int = - return cell.str.width() - -func newFormat*(): Format = - return Format(fgcolor: defaultColor, bgcolor: defaultColor) - -# Get the first format cell after pos, if any. -func findFormatN*(line: FlexibleLine, pos: int): int = - var i = 0 - while i < line.formats.len: - if line.formats[i].pos > pos: - break - inc i - return i - -func findFormat*(line: FlexibleLine, pos: int): FormatCell = - let i = line.findFormatN(pos) - 1 - if i != -1: - result = line.formats[i] - else: - result.pos = -1 - -func findNextFormat*(line: FlexibleLine, pos: int): FormatCell = - let i = line.findFormatN(pos) - if i < line.formats.len: - result = line.formats[i] - else: - result.pos = -1 - -proc addLine*(grid: var FlexibleGrid) = - grid.add(FlexibleLine()) - -proc insertFormat*(line: var FlexibleLine, pos, i: int, format: Format, computed: ComputedFormat = nil) = - if computed == nil: - line.formats.insert(FormatCell(format: format, pos: pos), i) - else: - line.formats.insert(FormatCell(format: format, computed: computed, node: computed.node, pos: pos), i) - -proc addFormat*(line: var FlexibleLine, pos: int, format: Format) = - line.formats.add(FormatCell(format: format, pos: pos)) - -proc addFormat*(line: var FlexibleLine, pos: int, format: Format, computed: ComputedFormat) = - #if computed != nil and line.formats.len > 0 and line.formats[^1].computed == computed and line.formats[^1].format.bgcolor != format.bgcolor: - # return - if computed == nil: - line.formats.add(FormatCell(format: format, pos: pos)) - else: - line.formats.add(FormatCell(format: format, computed: computed, node: computed.node, pos: pos)) - -template inc_check(i: int) = - inc i - if i >= buf.len: - return i - -proc handleAnsiCode(format: var Format, final: char, params: string) = - case final - of 'm': - if params.len == 0: - format = newFormat() - else: - let sparams = params.split(';') - try: - let ip = sparams.map((x) => parseInt(x)) - var pi = 0 - while pi < ip.len: - case ip[pi] - of 0: - format = newFormat() - of 1: format.bold = true - of 3: format.italic = true - of 4: format.underline = true - of 5: format.blink = true - of 7: format.reverse = true - of 9: format.strike = true - of 22: format.bold = false - of 23: format.italic = false - of 25: format.blink = false - of 27: format.reverse = false - of 29: format.strike = false - of 30..37: format.fgcolor = CellColor(rgb: false, color: uint8(ip[pi])) - of 38: - inc pi - if pi < ip.len: - if ip[pi] == 2: - inc pi - if pi + 2 < ip.len: - let r = ip[pi] - inc pi - let g = ip[pi] - inc pi - let b = ip[pi] - format.fgcolor = CellColor(rgb: true, rgbcolor: rgb(r, g, b)) - else: - #TODO - inc pi - continue - else: - break - of 39: - format.fgcolor = defaultColor - of 40..47: - format.bgcolor = CellColor(rgb: false, color: uint8(ip[0])) - of 48: - inc pi - if pi < ip.len: - if ip[pi] == 2: - inc pi - if pi + 2 < ip.len: - let r = ip[pi] - inc pi - let g = ip[pi] - inc pi - let b = ip[pi] - format.bgcolor = CellColor(rgb: true, rgbcolor: rgb(r, g, b)) - else: - #TODO - inc pi - continue - else: - break - of 49: format.bgcolor = defaultColor - of 53: format.overline = true - of 55: format.overline = false - else: discard - inc pi - except ValueError: discard - else: discard - -proc parseAnsiCode*(format: var Format, buf: string, fi: int): int = - var i = fi - if buf[i] != '\e': - return i - - inc_check i - if 0x40 <= int(buf[i]) and int(buf[i]) <= 0x5F: - if buf[i] != '[': - #C1, TODO? - return - inc_check i - - let sp = i - #parameter bytes - while 0x30 <= int(buf[i]) and int(buf[i]) <= 0x3F: - inc_check i - let params = buf.substr(sp, i - 1) - - #let si = i - #intermediate bytes - while 0x20 <= int(buf[i]) and int(buf[i]) <= 0x2F: - inc_check i - #let interm = buf.substr(si, i) - - let final = buf[i] - #final byte - if 0x40 <= int(buf[i]) and int(buf[i]) <= 0x7E: - format.handleAnsiCode(final, params) - - return i - -proc parseAnsiCode*(format: var Format, stream: Stream) = - if stream.atEnd(): return - var c = stream.readChar() - if 0x40 <= int(c) and int(c) <= 0x5F: - if c != '[': - #C1, TODO? - return - if stream.atEnd(): return - c = stream.readChar() - - var params = $c - #parameter bytes - while 0x30 <= int(c) and int(c) <= 0x3F: - params &= c - if stream.atEnd(): return - c = stream.readChar() - - #intermediate bytes - #var interm = $c - while 0x20 <= int(c) and int(c) <= 0x2F: - #interm &= c - if stream.atEnd(): return - c = stream.readChar() - - #final byte - if 0x40 <= int(c) and int(c) <= 0x7E: - let final = c - format.handleAnsiCode(final, params) - -proc processFormat*(format: var Format, cellf: Format): string = - for flag in FormatFlags: - if flag in format.flags and flag notin cellf.flags: - result &= SGR(FormatCodes[flag].e) - - if cellf.fgcolor != format.fgcolor: - var color = cellf.fgcolor - if color.rgb: - let rgb = color.rgbcolor - result &= SGR(38, 2, rgb.r, rgb.g, rgb.b) - elif color == defaultColor: - result &= SGR() - format = newFormat() - else: - result &= SGR(color.color) - - if cellf.bgcolor != format.bgcolor: - var color = cellf.bgcolor - if color.rgb: - let rgb = color.rgbcolor - result &= SGR(48, 2, rgb.r, rgb.g, rgb.b) - elif color == defaultColor: - result &= SGR() - format = newFormat() - else: - result &= SGR(color.color) - - for flag in FormatFlags: - if flag notin format.flags and flag in cellf.flags: - result &= SGR(FormatCodes[flag].s) - - format = cellf diff --git a/src/io/lineedit.nim b/src/io/lineedit.nim index 3336d7ac..2d7da512 100644 --- a/src/io/lineedit.nim +++ b/src/io/lineedit.nim @@ -4,27 +4,30 @@ import strutils import sequtils import sugar +import bindings/quickjs import config/config -import io/term +import js/javascript import utils/twtstr -type LineState* = object - news*: seq[Rune] - prompt*: string - current: string - s: string - feedNext: bool - escNext: bool - cursor: int - shift: int - minlen: int - maxlen: int - displen: int - disallowed: set[char] - hide: bool - config: Config #TODO get rid of this - tty: File - callback: proc(state: var LineState): bool +type + LineEditState* = enum + EDIT, FINISH, CANCEL + + LineEdit* = ref object + news*: seq[Rune] + prompt*: string + current: string + state*: LineEditState + escNext*: bool + cursor: int + shift: int + minlen: int + maxlen: int + displen: int + disallowed: set[char] + hide: bool + config: Config #TODO get rid of this + tty: File func lwidth(r: Rune): int = if r.isControlChar(): @@ -52,57 +55,55 @@ func lwidth(s: seq[Rune], min: int): int = result += lwidth(s[i]) inc i -template kill(state: LineState, i: int) = - state.space(i) - state.backward(i) +template kill0(edit: LineEdit, i: int) = + edit.space(i) + edit.backward0(i) -template kill(state: LineState) = - let w = min(state.news.lwidth(state.cursor), state.displen) - state.kill(w) +template kill0(edit: LineEdit) = + let w = min(edit.news.lwidth(edit.cursor), edit.displen) + edit.kill0(w) -proc backward(state: LineState, i: int) = +proc backward0(state: LineEdit, i: int) = if i > 0: if i == 1: print('\b') else: cursorBackward(i) -proc forward(state: LineState, i: int) = +proc forward0(state: LineEdit, i: int) = if i > 0: cursorForward(i) -proc begin(state: LineState) = +proc begin0(state: LineEdit) = print('\r') - state.forward(state.minlen) + state.forward0(state.minlen) -proc space(state: LineState, i: int) = +proc space(edit: LineEdit, i: int) = print(' '.repeat(i)) -proc redraw(state: var LineState) = +proc redraw(state: LineEdit) = var dispw = state.news.lwidth(state.shift, state.shift + state.displen) if state.shift + state.displen > state.news.len: state.displen = state.news.len - state.shift while dispw > state.maxlen - 1: dispw -= state.news[state.shift + state.displen - 1].lwidth() dec state.displen - - state.begin() + state.begin0() let os = state.news.substr(state.shift, state.shift + state.displen) if state.hide: printesc('*'.repeat(os.lwidth())) else: printesc($os) state.space(max(state.maxlen - state.minlen - os.lwidth(), 0)) + state.begin0() + state.forward0(state.news.lwidth(state.shift, state.cursor)) - state.begin() - state.forward(state.news.lwidth(state.shift, state.cursor)) - -proc zeroShiftRedraw(state: var LineState) = +proc zeroShiftRedraw(state: LineEdit) = state.shift = 0 state.displen = state.maxlen - 1 state.redraw() -proc fullRedraw(state: var LineState) = +proc fullRedraw*(state: LineEdit) = state.displen = state.maxlen - 1 if state.cursor > state.shift: var shiftw = state.news.lwidth(state.shift, state.cursor) @@ -111,10 +112,9 @@ proc fullRedraw(state: var LineState) = shiftw -= state.news[state.shift].lwidth() else: state.shift = max(state.cursor - 1, 0) - state.redraw() -proc insertCharseq(state: var LineState, cs: var seq[Rune], disallowed: set[char]) = +proc insertCharseq(state: LineEdit, cs: var seq[Rune], disallowed: set[char]) = let escNext = state.escNext cs.keepIf((r) => (escNext or not r.isControlChar) and not (r.isAscii and char(r) in disallowed)) state.escNext = false @@ -133,169 +133,165 @@ proc insertCharseq(state: var LineState, cs: var seq[Rune], disallowed: set[char state.cursor += cs.len state.fullRedraw() -proc readLine(state: var LineState): bool = - printesc(state.prompt) - if state.hide: - printesc('*'.repeat(state.current.lwidth())) - else: - printesc(state.current) +proc cancel*(edit: LineEdit) {.jsfunc.} = + edit.state = CANCEL - while true: - if not state.feedNext: - state.s = "" - else: - state.feedNext = false - - restoreStdin(state.tty.getFileHandle()) - let c = state.tty.readChar() - state.s &= c - - var action = getLinedAction(state.config, state.s) - if state.escNext: - action = NO_ACTION - case action - of ACTION_LINED_CANCEL: - return false - of ACTION_LINED_SUBMIT: - return true - of ACTION_LINED_BACKSPACE: - if state.cursor > 0: - let w = state.news[state.cursor - 1].lwidth() - state.news.delete(state.cursor - 1..state.cursor - 1) - dec state.cursor - if state.cursor == state.news.len and state.shift == 0: - state.backward(w) - state.kill(w) - else: - state.fullRedraw() - of ACTION_LINED_DELETE: - if state.cursor >= 0 and state.cursor < state.news.len: - let w = state.news[state.cursor].lwidth() - state.news.delete(state.cursor..state.cursor) - if state.cursor == state.news.len and state.shift == 0: - state.kill(w) - else: - state.fullRedraw() - of ACTION_LINED_ESC: - state.escNext = true - of ACTION_LINED_CLEAR: - if state.cursor > 0: - state.news.delete(0..state.cursor - 1) - state.cursor = 0 - state.zeroShiftRedraw() - of ACTION_LINED_KILL: - if state.cursor < state.news.len: - state.kill() - state.news.setLen(state.cursor) - of ACTION_LINED_BACK: - if state.cursor > 0: - dec state.cursor - if state.cursor > state.shift or state.shift == 0: - state.backward(state.news[state.cursor].lwidth()) - else: - state.fullRedraw() - of ACTION_LINED_FORWARD: - if state.cursor < state.news.len: - inc state.cursor - if state.news.lwidth(state.shift, state.cursor) < state.displen: - var n = 1 - if state.news.len > state.cursor: - n = state.news[state.cursor].lwidth() - state.forward(n) - else: - state.fullRedraw() - of ACTION_LINED_PREV_WORD: - let oc = state.cursor - while state.cursor > 0: - dec state.cursor - if state.news[state.cursor].breaksWord(): - break - if state.cursor != oc: - if state.cursor > state.shift or state.shift == 0: - state.backward(state.news.lwidth(state.cursor, oc)) - else: - state.fullRedraw() - of ACTION_LINED_NEXT_WORD: - let oc = state.cursor - while state.cursor < state.news.len: - inc state.cursor - if state.cursor < state.news.len: - if state.news[state.cursor].breaksWord(): - break - - if state.cursor != oc: - let dw = state.news.lwidth(oc, state.cursor) - if oc + dw - state.shift < state.displen: - state.forward(dw) - else: - state.fullRedraw() - of ACTION_LINED_KILL_WORD: - var chars = 0 - if state.cursor > chars: - inc chars - - while state.cursor > chars: - inc chars - if state.news[state.cursor - chars].breaksWord(): - dec chars - break - if chars > 0: - let w = state.news.lwidth(state.cursor - chars, state.cursor) - state.news.delete(state.cursor - chars..state.cursor - 1) - state.cursor -= chars - if state.cursor == state.news.len and state.shift == 0: - state.backward(w) - state.kill(w) - else: - state.fullRedraw() - of ACTION_LINED_BEGIN: - if state.cursor > 0: - if state.shift == 0: - state.backward(state.news.lwidth(0, state.cursor)) - else: - state.fullRedraw() - state.cursor = 0 - of ACTION_LINED_END: - if state.cursor < state.news.len: - if state.news.lwidth(state.shift, state.news.len) < state.maxlen: - state.forward(state.news.lwidth(state.cursor, state.news.len)) - else: - state.fullRedraw() - state.cursor = state.news.len - of ACTION_FEED_NEXT: - state.feedNext = true - elif validateUtf8(state.s) == -1: - var cs = state.s.toRunes() - state.insertCharseq(cs, state.disallowed) - if state.callback(state): - state.fullRedraw() +proc submit*(edit: LineEdit) {.jsfunc.} = + edit.state = FINISH + +proc backspace*(edit: LineEdit) {.jsfunc.} = + if edit.cursor > 0: + let w = edit.news[edit.cursor - 1].lwidth() + edit.news.delete(edit.cursor - 1..edit.cursor - 1) + dec edit.cursor + if edit.cursor == edit.news.len and edit.shift == 0: + edit.backward0(w) + edit.kill0(w) else: - state.feedNext = true - -proc readLine*(prompt: string, current: var string, termwidth: int, - disallowed: set[char], hide: bool, config: Config, - tty: File, callback: proc(state: var LineState): bool): bool = - var state: LineState - - state.prompt = prompt - state.current = current - state.news = current.toRunes() - state.cursor = state.news.len - state.minlen = prompt.lwidth() - state.maxlen = termwidth - prompt.len - state.displen = state.maxlen - 1 - state.disallowed = disallowed - state.callback = callback - state.hide = hide - state.config = config - state.tty = tty - - if state.readLine(): - current = $state.news + edit.fullRedraw() + +proc write*(edit: LineEdit, s: string): bool {.jsfunc.} = + if validateUtf8(s) == -1: + var cs = s.toRunes() + edit.insertCharseq(cs, edit.disallowed) return true - return false -proc readLine*(prompt: string, current: var string, termwidth: int, +proc delete*(edit: LineEdit) {.jsfunc.} = + if edit.cursor >= 0 and edit.cursor < edit.news.len: + let w = edit.news[edit.cursor].lwidth() + edit.news.delete(edit.cursor..edit.cursor) + if edit.cursor == edit.news.len and edit.shift == 0: + edit.kill0(w) + else: + edit.fullRedraw() + +proc escape*(edit: LineEdit) {.jsfunc.} = + edit.escNext = true + +proc clear*(edit: LineEdit) {.jsfunc.} = + if edit.cursor > 0: + edit.news.delete(0..edit.cursor - 1) + edit.cursor = 0 + edit.zeroShiftRedraw() + +proc kill*(edit: LineEdit) {.jsfunc.} = + if edit.cursor < edit.news.len: + edit.kill0() + edit.news.setLen(edit.cursor) + +proc backward*(edit: LineEdit) {.jsfunc.} = + if edit.cursor > 0: + dec edit.cursor + if edit.cursor > edit.shift or edit.shift == 0: + edit.backward0(edit.news[edit.cursor].lwidth()) + else: + edit.fullRedraw() + +proc forward*(edit: LineEdit) {.jsfunc.} = + if edit.cursor < edit.news.len: + inc edit.cursor + if edit.news.lwidth(edit.shift, edit.cursor) < edit.displen: + var n = 1 + if edit.news.len > edit.cursor: + n = edit.news[edit.cursor].lwidth() + edit.forward0(n) + else: + edit.fullRedraw() + +proc prevWord*(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} = + let oc = edit.cursor + while edit.cursor > 0: + dec edit.cursor + if edit.news[edit.cursor].breaksWord(check): + break + if edit.cursor != oc: + if edit.cursor > edit.shift or edit.shift == 0: + edit.backward0(edit.news.lwidth(edit.cursor, oc)) + else: + edit.fullRedraw() + +proc nextWord*(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} = + let oc = edit.cursor + while edit.cursor < edit.news.len: + inc edit.cursor + if edit.cursor < edit.news.len: + if edit.news[edit.cursor].breaksWord(check): + break + if edit.cursor != oc: + let dw = edit.news.lwidth(oc, edit.cursor) + if oc + dw - edit.shift < edit.displen: + edit.forward0(dw) + else: + edit.fullRedraw() + +proc clearWord*(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} = + var i = edit.cursor + if i > 0: + # point to the previous character + dec i + while i > 0: + dec i + if edit.news[i].breaksWord(check): + inc i + break + if i != edit.cursor: + edit.news.delete(i..<edit.cursor) + edit.cursor = i + edit.fullRedraw() + +proc killWord*(edit: LineEdit, check = none(BoundaryFunction)) {.jsfunc.} = + var i = edit.cursor + if i < edit.news.len and edit.news[i].breaksWord(check): + inc i + while i < edit.news.len: + if edit.news[i].breaksWord(check): + break + inc i + if i != edit.cursor: + edit.news.delete(edit.cursor..<i) + edit.fullRedraw() + +proc begin*(edit: LineEdit) {.jsfunc.} = + if edit.cursor > 0: + if edit.shift == 0: + edit.backward0(edit.news.lwidth(0, edit.cursor)) + else: + edit.fullRedraw() + edit.cursor = 0 + +proc `end`*(edit: LineEdit) {.jsfunc.} = + if edit.cursor < edit.news.len: + if edit.news.lwidth(edit.shift, edit.news.len) < edit.maxlen: + edit.forward0(edit.news.lwidth(edit.cursor, edit.news.len)) + else: + edit.fullRedraw() + edit.cursor = edit.news.len + +proc writePrompt*(lineedit: LineEdit) = + printesc(lineedit.prompt) + +proc writeStart*(lineedit: LineEdit) = + lineedit.writePrompt() + if lineedit.hide: + printesc('*'.repeat(lineedit.current.lwidth())) + else: + printesc(lineedit.current) + +proc readLine*(prompt: string, termwidth: int, current = "", disallowed: set[char] = {}, hide = false, config: Config, - tty: File): bool = - readLine(prompt, current, termwidth, disallowed, hide, config, tty, (proc(state: var LineState): bool = false)) + tty: File): LineEdit = + new(result) + result.prompt = prompt + result.current = current + result.news = current.toRunes() + result.cursor = result.news.len + result.minlen = prompt.lwidth() + result.maxlen = termwidth - prompt.len + result.displen = result.maxlen - 1 + result.disallowed = disallowed + result.hide = hide + result.config = config + result.tty = tty + +proc addLineEditModule*(ctx: JSContext) = + ctx.registerType(LineEdit) diff --git a/src/io/loader.nim b/src/io/loader.nim index 7e6faab4..a8d0b969 100644 --- a/src/io/loader.nim +++ b/src/io/loader.nim @@ -76,10 +76,11 @@ proc loadResource(loader: FileLoader, request: Request, ostream: Stream) = ostream.swrite(-1) # error ostream.flush() +var ssock: ServerSocket proc runFileLoader(loader: FileLoader, fd: cint) = if curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK: raise newException(Defect, "Failed to initialize libcurl.") - let ssock = initServerSocket(getpid()) + ssock = initServerSocket(getpid()) # The server has been initialized, so the main process can resume execution. var writef: File if not open(writef, FileHandle(fd), fmWrite): @@ -88,6 +89,10 @@ proc runFileLoader(loader: FileLoader, fd: cint) = writef.flushFile() close(writef) discard close(fd) + onSignal SIGTERM, SIGINT: + curl_global_cleanup() + ssock.close() + quit(1) while true: let stream = ssock.acceptSocketStream() try: diff --git a/src/io/serialize.nim b/src/io/serialize.nim index 932b82df..2dde0649 100644 --- a/src/io/serialize.nim +++ b/src/io/serialize.nim @@ -4,7 +4,10 @@ import options import streams import tables +import buffer/cell import io/request +import js/regex +import types/color import types/url template swrite*[T](stream: Stream, o: T) = @@ -47,11 +50,9 @@ proc swrite*[T](stream: Stream, s: seq[T]) = stream.swrite(m) proc swrite*[T](stream: Stream, o: Option[T]) = + stream.swrite(o.issome) if o.issome: - stream.swrite(1u8) stream.swrite(o.get) - else: - stream.swrite(0u8) proc swrite*(stream: Stream, request: Request) = stream.swrite(request.httpmethod) @@ -60,6 +61,39 @@ proc swrite*(stream: Stream, request: Request) = stream.swrite(request.body) stream.swrite(request.multipart) +proc swrite*(stream: Stream, color: CellColor) = + stream.swrite(color.rgb) + if color.rgb: + stream.swrite(color.rgbcolor) + else: + stream.swrite(color.color) + +proc swrite*(stream: Stream, format: Format) = + stream.swrite(format.fgcolor) + stream.swrite(format.bgcolor) + stream.swrite(format.flags) + +proc swrite*(stream: Stream, cell: SimpleFormatCell) = + stream.swrite(cell.format) + stream.swrite(cell.pos) + +proc swrite*(stream: Stream, line: SimpleFlexibleLine) = + stream.swrite(line.str) + stream.swrite(line.formats) + +proc swrite*(stream: Stream, cell: FormatCell) = + stream.swrite(cell.format) + stream.swrite(cell.pos) + +proc swrite*(stream: Stream, line: FlexibleLine) = + stream.swrite(line.str) + stream.swrite(line.formats) + +proc swrite*(stream: Stream, regex: Regex) = + stream.swrite(regex.plen) + stream.writeData(regex.bytecode, regex.plen) + stream.swrite(regex.buf) + template sread*[T](stream: Stream, o: T) = stream.read(o) @@ -119,13 +153,13 @@ proc sread*[T](stream: Stream, s: var seq[T]) = stream.sread(s[i]) proc sread*[T](stream: Stream, o: var Option[T]) = - let c = uint8(stream.readChar()) - if c == 1u8: + var x: bool + stream.sread(x) + if x: var m: T stream.sread(m) o = some(m) else: - assert c == 0u8 o = none(T) proc sread*(stream: Stream, req: var RequestObj) = @@ -135,6 +169,39 @@ proc sread*(stream: Stream, req: var RequestObj) = stream.sread(req.body) stream.sread(req.multipart) +proc sread*(stream: Stream, color: var CellColor) = + var rgb: bool + stream.sread(rgb) + if rgb: + color = CellColor(rgb: true) + stream.sread(color.rgbcolor) + else: + color = CellColor(rgb: false) + stream.sread(color.color) + +proc sread*(stream: Stream, format: var Format) = + stream.sread(format.fgcolor) + stream.sread(format.bgcolor) + stream.sread(format.flags) + +proc sread*(stream: Stream, cell: var SimpleFormatCell) = + stream.sread(cell.format) + stream.sread(cell.pos) + +proc sread*(stream: Stream, line: var SimpleFlexibleLine) = + stream.sread(line.str) + stream.sread(line.formats) + +proc sread*(stream: Stream, regex: var Regex) = + assert regex.bytecode == nil + stream.sread(regex.plen) + regex.bytecode = cast[ptr uint8](alloc(regex.plen)) + regex.clone = true + let l = stream.readData(regex.bytecode, regex.plen) + stream.sread(regex.buf) + if l != regex.plen: + `=destroy`(regex) + proc readRequest*(stream: Stream): Request = new(result) stream.sread(result[]) diff --git a/src/io/term.nim b/src/io/term.nim index bc0f2ae9..1eacd237 100644 --- a/src/io/term.nim +++ b/src/io/term.nim @@ -23,7 +23,6 @@ when defined(posix): discard tcSetAttr(stdin_fileno, TCSAFLUSH, addr orig_termios) proc enableRawMode*(fileno: FileHandle) = - eprint "raw mode" stdin_fileno = fileno addExitProc(disableRawMode) discard tcGetAttr(fileno, addr orig_termios) |