Compare commits
10 Commits
0f95938d33
...
a014ea71d2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a014ea71d2 | ||
|
|
a785c7df3b | ||
|
|
06544fa371 | ||
|
|
fb9007df49 | ||
|
|
c53b5a80f2 | ||
|
|
9815c10040 | ||
|
|
a6a2359568 | ||
|
|
7cec7f8604 | ||
|
|
3583351551 | ||
|
|
84f321389a |
@ -12,6 +12,7 @@ import (
|
|||||||
"bbash/command/mv"
|
"bbash/command/mv"
|
||||||
"bbash/command/pwd"
|
"bbash/command/pwd"
|
||||||
"bbash/command/rm"
|
"bbash/command/rm"
|
||||||
|
"bbash/command/tail"
|
||||||
"bbash/command/touch"
|
"bbash/command/touch"
|
||||||
"bbash/environment"
|
"bbash/environment"
|
||||||
"bbash/input_parser"
|
"bbash/input_parser"
|
||||||
@ -56,6 +57,8 @@ func Run_command(in input_parser.Input, env *environment.Env) {
|
|||||||
mv.Mv(in, env)
|
mv.Mv(in, env)
|
||||||
case "help":
|
case "help":
|
||||||
help.Help(in, env)
|
help.Help(in, env)
|
||||||
|
case "tail":
|
||||||
|
tail.Tail(in, env)
|
||||||
default:
|
default:
|
||||||
if !run_by_path(in, env) {
|
if !run_by_path(in, env) {
|
||||||
fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction))
|
fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction))
|
||||||
|
|||||||
@ -21,7 +21,7 @@ func Man(in input_parser.Input, env *environment.Env) {
|
|||||||
case "cat":
|
case "cat":
|
||||||
fmt.Print("Outputs the contents of the given file")
|
fmt.Print("Outputs the contents of the given file")
|
||||||
case "head":
|
case "head":
|
||||||
fmt.Print("Outputs first lines of a given file. 10 if non provided. Argument <file> <number of lines>")
|
fmt.Print("Outputs first lines of a given file. 10 if none provided. Argument <file> <number of lines>")
|
||||||
case "touch":
|
case "touch":
|
||||||
fmt.Print("Creates the given file")
|
fmt.Print("Creates the given file")
|
||||||
case "rm":
|
case "rm":
|
||||||
@ -30,8 +30,9 @@ func Man(in input_parser.Input, env *environment.Env) {
|
|||||||
fmt.Print("Copies the given file. Argument: <source> <destination>")
|
fmt.Print("Copies the given file. Argument: <source> <destination>")
|
||||||
case "mv":
|
case "mv":
|
||||||
fmt.Print("Moves a given file. Argument: <source> <destination>")
|
fmt.Print("Moves a given file. Argument: <source> <destination>")
|
||||||
default: fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction))
|
case "tail":
|
||||||
|
fmt.Print("Outputs last lines of a given file. 10 if none or 0 is provided. Argument <file> <number of lines>")
|
||||||
|
default:
|
||||||
|
fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
84
command/tail/tail.go
Normal file
84
command/tail/tail.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package tail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bbash/environment"
|
||||||
|
"bbash/input_parser"
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Tail(in input_parser.Input, env *environment.Env) {
|
||||||
|
args := in.Args
|
||||||
|
var file_path string
|
||||||
|
var lines int
|
||||||
|
if len(args) >= 1 {
|
||||||
|
file_path = strings.TrimSpace(args[0])
|
||||||
|
} else {
|
||||||
|
fmt.Print(fmt.Sprintf("No arguments provided"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(args) == 2 {
|
||||||
|
lines1, err := strconv.Atoi(strings.TrimSpace(args[1]))
|
||||||
|
lines = lines1
|
||||||
|
if err != nil {
|
||||||
|
fmt.Print(fmt.Sprintf("Second argument must be a integer"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file, err := os.Open(file_path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(fmt.Sprintf("Error opening file: %s", err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
size, err := CountLines(file)
|
||||||
|
file.Seek(0, io.SeekStart)
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
if lines == 0 {
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
if !scanner.Scan() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i >= (size - 10) {
|
||||||
|
fmt.Println(fmt.Sprintf(scanner.Text()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
if !scanner.Scan() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i >= (size - lines) {
|
||||||
|
fmt.Println(fmt.Sprintf(scanner.Text()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func CountLines(r io.Reader) (int, error) {
|
||||||
|
var count int
|
||||||
|
var read int
|
||||||
|
var err error
|
||||||
|
var target []byte = []byte("\n")
|
||||||
|
|
||||||
|
buffer := make([]byte, 32*1024)
|
||||||
|
|
||||||
|
for {
|
||||||
|
read, err = r.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
count += bytes.Count(buffer[:read], target)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == io.EOF {
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, err
|
||||||
|
}
|
||||||
@ -36,8 +36,14 @@ func parse(input string) Input {
|
|||||||
var flags []string
|
var flags []string
|
||||||
var args []string
|
var args []string
|
||||||
for _, arg := range split[1:] {
|
for _, arg := range split[1:] {
|
||||||
if len(arg) == 1 && arg[0] == '-' {
|
if len(arg) == 0 {
|
||||||
continue
|
continue
|
||||||
|
} else if len(arg) == 1 {
|
||||||
|
if arg[0] == '-' {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
args = append(args, strings.TrimSpace(arg))
|
||||||
|
}
|
||||||
} else if arg[0:2] == "--" {
|
} else if arg[0:2] == "--" {
|
||||||
var result = strings.TrimSpace(arg)
|
var result = strings.TrimSpace(arg)
|
||||||
flags = append(flags, string(result[2:]))
|
flags = append(flags, string(result[2:]))
|
||||||
@ -100,7 +106,7 @@ func input_str(env *environment.Env) string {
|
|||||||
history_index++
|
history_index++
|
||||||
input = env.History[history_index]
|
input = env.History[history_index]
|
||||||
} else {
|
} else {
|
||||||
history_index = len(env.History)-1
|
history_index = len(env.History) - 1
|
||||||
input = ""
|
input = ""
|
||||||
}
|
}
|
||||||
case 67: // LEFT
|
case 67: // LEFT
|
||||||
@ -120,7 +126,7 @@ func input_str(env *environment.Env) string {
|
|||||||
fmt.Print(" > ")
|
fmt.Print(" > ")
|
||||||
fmt.Print(input)
|
fmt.Print(input)
|
||||||
}
|
}
|
||||||
loop_exit:
|
loop_exit:
|
||||||
term.Restore(int(std_fd), term_restore)
|
term.Restore(int(std_fd), term_restore)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return input
|
return input
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user