Improvements and WIP wc
This commit is contained in:
parent
94c57b1da2
commit
b7e5108379
10 changed files with 60 additions and 16 deletions
2
cat.nim
2
cat.nim
|
|
@ -1,4 +1,4 @@
|
|||
import cligen, os, streams
|
||||
import cligen, os
|
||||
import common
|
||||
|
||||
proc catFile(file:string) =
|
||||
|
|
|
|||
2
cp.nim
2
cp.nim
|
|
@ -1,7 +1,7 @@
|
|||
import cligen, os
|
||||
import common
|
||||
|
||||
proc main() =
|
||||
proc main(files:seq[string]) =
|
||||
echo "asdf"
|
||||
|
||||
dispatch(main, version=("version", nimbaseVersion))
|
||||
|
|
|
|||
6
echo.nim
6
echo.nim
|
|
@ -1,11 +1,11 @@
|
|||
import cligen, sequtils
|
||||
import common
|
||||
|
||||
proc main(strings:seq[string], newline=true) =
|
||||
proc main(strings:seq[string], newline=true, nospaces=false) =
|
||||
if newline:
|
||||
writeLine(stdout, strings.foldl(a & " " & b))
|
||||
writeLine(stdout, strings.foldl(a & (if nospaces: "" else: " ") & b))
|
||||
else:
|
||||
write(stdout, strings.foldl(a & " " & b))
|
||||
write(stdout, strings.foldl(a & (if nospaces: "" else: " ") & b))
|
||||
flushFile(stdout)
|
||||
|
||||
dispatch(main, version=("version", nimbaseVersion))
|
||||
|
|
|
|||
12
head.nim
12
head.nim
|
|
@ -1,10 +1,12 @@
|
|||
import cligen, os, streams
|
||||
import cligen, os
|
||||
import common
|
||||
|
||||
proc main(files:seq[string], lines=10, bytes=0) =
|
||||
proc main(files:seq[string], lines=10, bytes=0, quiet=false) =
|
||||
# if files.len == 0:
|
||||
# files.add "-"
|
||||
for file in files:
|
||||
if files.len > 1 and not quiet:
|
||||
writeLine(stdout, "==> " & (if file == "-": "standard input" else: file) & " <==")
|
||||
var f = if file == "-": stdin
|
||||
else: open(file, fmRead)
|
||||
if bytes > 0:
|
||||
|
|
@ -14,8 +16,10 @@ proc main(files:seq[string], lines=10, bytes=0) =
|
|||
inc currentByte
|
||||
elif lines > 0:
|
||||
var currentLine = 0
|
||||
while not f.endOfFile and currentLine < lines:
|
||||
writeLine(stdout, f.readLine)
|
||||
for line in lines(f):
|
||||
if currentLine >= lines:
|
||||
break
|
||||
writeLine(stdout, line)
|
||||
inc currentLine
|
||||
|
||||
dispatch(main, version=("version", nimbaseVersion))
|
||||
|
|
|
|||
|
|
@ -8,6 +8,6 @@ requires "nim >= 0.19.0"
|
|||
requires "cligen >= 0.9.19"
|
||||
requires "stint >= 0.0.1"
|
||||
|
||||
bin = @["add", "cat", "cp", "div", "echo", "factor", "false", "head", "mkdir", "mul", "nl", "pwd", "rev", "rm", "seq", "shuf", "sleep", "sub", "tac", "tee", "touch", "true", "yes"]
|
||||
bin = @["add", "cat", "cp", "div", "echo", "factor", "false", "head", "mkdir", "mul", "nl", "pwd", "rev", "rm", "seq", "shuf", "sleep", "sub", "tac", "tee", "touch", "true", "wc", "yes"]
|
||||
|
||||
binDir = "bin"
|
||||
|
|
|
|||
2
rev.nim
2
rev.nim
|
|
@ -1,4 +1,4 @@
|
|||
import cligen, os, streams, unicode
|
||||
import cligen, os, unicode
|
||||
import common
|
||||
|
||||
proc catFile(file:string) =
|
||||
|
|
|
|||
14
seq.nim
14
seq.nim
|
|
@ -1,7 +1,19 @@
|
|||
import cligen
|
||||
import common
|
||||
|
||||
proc main(last:int, first=1, increment=1, separator="\n") =
|
||||
proc main(numbers:seq[int], separator="\n") =
|
||||
if numbers.len < 1:
|
||||
err "seq: missing operand"
|
||||
quit 1
|
||||
if numbers.len > 3:
|
||||
err "seq: extra operand"
|
||||
quit 1
|
||||
let
|
||||
last = numbers[numbers.high]
|
||||
first = if numbers.len > 1: numbers[0]
|
||||
else: 1
|
||||
increment = if numbers.len == 3: numbers[1]
|
||||
else: 1
|
||||
var i = first
|
||||
while i <= last:
|
||||
write(stdout, i)
|
||||
|
|
|
|||
2
tac.nim
2
tac.nim
|
|
@ -1,4 +1,4 @@
|
|||
import cligen, os, streams
|
||||
import cligen, os
|
||||
import common
|
||||
|
||||
proc catFile(file:string) =
|
||||
|
|
|
|||
5
tee.nim
5
tee.nim
|
|
@ -4,11 +4,10 @@ import common
|
|||
proc main(files:seq[string]) =
|
||||
var
|
||||
fileStreams:seq[Stream]
|
||||
line = ""
|
||||
for file in files:
|
||||
fileStreams.add(newFileStream(file, fmWrite))
|
||||
while stdin.readLine(line):
|
||||
echo line
|
||||
for line in lines(stdin):
|
||||
writeLine(stdout, line)
|
||||
for fStream in fileStreams:
|
||||
writeLine(fStream, line)
|
||||
|
||||
|
|
|
|||
29
wc.nim
Normal file
29
wc.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import cligen, os, strutils
|
||||
import common
|
||||
|
||||
proc main(files:seq[string]) =
|
||||
# if files.len == 0:
|
||||
# files.add "-"
|
||||
var
|
||||
totalLines = 0
|
||||
totalWords = 0
|
||||
totalChars = 0
|
||||
for file in files:
|
||||
var f = if file == "-": stdin
|
||||
else: open(file, fmRead)
|
||||
var
|
||||
fileLines = 0
|
||||
fileWords = 0
|
||||
fileChars = 0
|
||||
for line in lines(f):
|
||||
inc fileLines
|
||||
fileWords += line.split.len
|
||||
fileChars += line.len
|
||||
totalLines += fileLines
|
||||
totalWords += fileWords
|
||||
totalChars += fileChars
|
||||
writeLine(stdout, $(fileLines) & $(fileWords) & $(fileChars) & file)
|
||||
if files.len > 1:
|
||||
writeLine(stdout, $(totalLines) & $(totalWords) & $(totalChars) & "total")
|
||||
|
||||
dispatch(main, version=("version", nimbaseVersion))
|
||||
Loading…
Add table
Add a link
Reference in a new issue