diff --git a/README.md b/README.md index 7277871..967fac0 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ nyat is cat clone. Written in nim. (=^・・^=) -n Display line number --b number nonempty output lines +-b Number nonempty output lines --s suppress repeated empty output lines +-s Suppress repeated empty output lines ## Installation ```sh diff --git a/src/nyat.nim b/src/nyat.nim index f95bb0a..5de46e0 100644 --- a/src/nyat.nim +++ b/src/nyat.nim @@ -1,4 +1,4 @@ -import os, unicode, sequtils, posix +import os, unicode, sequtils, posix, strutils type FileNameAndOption = tuple[fileNameList: seq[string], options: seq[char]] @@ -69,12 +69,17 @@ proc setBufferList(fileNameList: seq[string]): seq[seq[string]] = for i in 0 ..< fileNameList.len: result.add(openFile(fileNameList[i])) +proc setLineNumber(digit, currentNumber: int): string = + return " ".repeat(2) & $currentNumber & " ".repeat(digit - ($(currentNumber)).len + 1) + proc displayBuffer(buffer: seq[string], option: OptionList) = var lineNumber = 1 writeLine = "" ignoreLine = false + let lineNumberDigit = ($buffer.len).len + for i in 0 ..< buffer.len: if i < buffer.high and option.squeezeBlank: if buffer[i] == "" and buffer[i + 1] == "": @@ -82,19 +87,19 @@ proc displayBuffer(buffer: seq[string], option: OptionList) = if option.numberNoBlank: if buffer[i] != "": - writeLine = " " & $lineNumber & " " + writeLine = setLineNumber(lineNumberDigit, lineNumber) lineNumber.inc else: writeLine = "" - - if option.setLineNumber: - writeLine = " " & $lineNumber & " " + elif option.setLineNumber: + writeLine = setLineNumber(lineNumberDigit, lineNumber) lineNumber.inc if ignoreLine == false: - echo writeLine & buffer[i] + echo $writeLine & buffer[i] ignoreLine = false + writeLine = "" when isMainModule: if commandLineParams().len == 0: