added -n and --help to tail and head
This commit is contained in:
parent
a014ea71d2
commit
13a510550e
@ -6,12 +6,28 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Head(in input_parser.Input, env *environment.Env) {
|
||||
args := strings.SplitN(in.Args_raw, " ", 2)
|
||||
flagsArray := []string{
|
||||
"n ",
|
||||
"help ",
|
||||
}
|
||||
flagsDictionary := map[string]string{
|
||||
"n": "-n displays the line numbers",
|
||||
"help": "-help shows this message",
|
||||
}
|
||||
if slices.Contains(in.Flags, "help") {
|
||||
fmt.Printf("Lists Sources in the current working directory")
|
||||
fmt.Printf("Supported flags are:")
|
||||
for _, flag := range flagsArray {
|
||||
fmt.Println(flag + flagsDictionary[flag])
|
||||
} // Print flags and there description
|
||||
}
|
||||
args := in.Args
|
||||
var file_path string
|
||||
var lines int
|
||||
if len(args) >= 1 {
|
||||
@ -20,7 +36,7 @@ func Head(in input_parser.Input, env *environment.Env) {
|
||||
} else {
|
||||
fmt.Print(fmt.Sprintf("No arguments provided"))
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(args) == 2 {
|
||||
lines1, err := strconv.Atoi(strings.TrimSpace(args[1]))
|
||||
lines = lines1
|
||||
@ -28,7 +44,7 @@ func Head(in input_parser.Input, env *environment.Env) {
|
||||
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()))
|
||||
@ -36,15 +52,24 @@ func Head(in input_parser.Input, env *environment.Env) {
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
for i := 0; i < lines-1; i++ {
|
||||
if(!scanner.Scan()) {
|
||||
return
|
||||
if slices.Contains(in.Flags, "n") {
|
||||
var counter int
|
||||
for counter = 0; counter < lines-1; counter++ {
|
||||
if !scanner.Scan() {
|
||||
return
|
||||
}
|
||||
fmt.Println(fmt.Sprintf(strconv.FormatInt(int64(counter), 10) + scanner.Text()))
|
||||
}
|
||||
fmt.Println(scanner.Text())
|
||||
scanner.Scan()
|
||||
fmt.Print(fmt.Sprintf(strconv.FormatInt(int64(counter), 10) + scanner.Text()))
|
||||
} else {
|
||||
for i := 0; i < lines-1; i++ {
|
||||
if !scanner.Scan() {
|
||||
return
|
||||
}
|
||||
fmt.Println(scanner.Text())
|
||||
}
|
||||
scanner.Scan()
|
||||
fmt.Print(scanner.Text())
|
||||
}
|
||||
scanner.Scan()
|
||||
fmt.Print(scanner.Text())
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -9,12 +9,12 @@ import (
|
||||
)
|
||||
|
||||
func Ls(in input_parser.Input, env *environment.Env) {
|
||||
flagsArray := []string {
|
||||
"a ",
|
||||
flagsArray := []string{
|
||||
"a ",
|
||||
"help ",
|
||||
}
|
||||
flagsDictionary := map[string]string {
|
||||
"a": "-a shows hidden directories",
|
||||
flagsDictionary := map[string]string{
|
||||
"a": "-a shows hidden directories",
|
||||
"help": "-help shows this message",
|
||||
}
|
||||
if slices.Contains(in.Flags, "help") {
|
||||
@ -22,7 +22,7 @@ func Ls(in input_parser.Input, env *environment.Env) {
|
||||
fmt.Printf("Supported flags are:")
|
||||
for _, flag := range flagsArray {
|
||||
fmt.Println(flag + flagsDictionary[flag])
|
||||
} // Print flags and there description
|
||||
} // Print flags and their description
|
||||
}
|
||||
fs, err := os.ReadDir(env.Path)
|
||||
if err != nil {
|
||||
|
||||
@ -21,7 +21,7 @@ func Man(in input_parser.Input, env *environment.Env) {
|
||||
case "cat":
|
||||
fmt.Print("Outputs the contents of the given file")
|
||||
case "head":
|
||||
fmt.Print("Outputs first lines of a given file. 10 if none provided. Argument <file> <number of lines>")
|
||||
fmt.Print("Outputs first lines of a given file. 10 if none provided. Argument <file> <number of lines> \nTakes the flag -n in order to display the line numbers")
|
||||
case "touch":
|
||||
fmt.Print("Creates the given file")
|
||||
case "rm":
|
||||
@ -31,7 +31,7 @@ func Man(in input_parser.Input, env *environment.Env) {
|
||||
case "mv":
|
||||
fmt.Print("Moves a given file. Argument: <source> <destination>")
|
||||
case "tail":
|
||||
fmt.Print("Outputs last lines of a given file. 10 if none or 0 is provided. Argument <file> <number of lines>")
|
||||
fmt.Print("Outputs last lines of a given file. 10 if none or 0 is provided. Argument <file> <number of lines> \nTakes the flag -n in order to display the line numbers")
|
||||
default:
|
||||
fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction))
|
||||
}
|
||||
|
||||
@ -8,11 +8,27 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Tail(in input_parser.Input, env *environment.Env) {
|
||||
flagsArray := []string{
|
||||
"n ",
|
||||
"help ",
|
||||
}
|
||||
flagsDictionary := map[string]string{
|
||||
"n": "-n displays the line numbers",
|
||||
"help": "-help shows this message",
|
||||
}
|
||||
if slices.Contains(in.Flags, "help") {
|
||||
fmt.Printf("Lists Sources in the current working directory")
|
||||
fmt.Printf("Supported flags are:")
|
||||
for _, flag := range flagsArray {
|
||||
fmt.Println(flag + flagsDictionary[flag])
|
||||
} // Print flags and there description
|
||||
}
|
||||
args := in.Args
|
||||
var file_path string
|
||||
var lines int
|
||||
@ -40,22 +56,49 @@ func Tail(in input_parser.Input, env *environment.Env) {
|
||||
file.Seek(0, io.SeekStart)
|
||||
scanner := bufio.NewScanner(file)
|
||||
if lines == 0 {
|
||||
for i := 0; i < size; i++ {
|
||||
if !scanner.Scan() {
|
||||
return
|
||||
if slices.Contains(in.Flags, "n") {
|
||||
for i := 0; i < size; i++ {
|
||||
if !scanner.Scan() {
|
||||
return
|
||||
}
|
||||
if i >= (size - 10) {
|
||||
fmt.Println(fmt.Sprintf(strconv.FormatInt(int64(i), 10) + scanner.Text()))
|
||||
}
|
||||
}
|
||||
if i >= (size - 10) {
|
||||
fmt.Println(fmt.Sprintf(scanner.Text()))
|
||||
} else {
|
||||
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 slices.Contains(in.Flags, "n") {
|
||||
var counter int
|
||||
for counter := 0; counter < size-1; counter++ {
|
||||
if !scanner.Scan() {
|
||||
return
|
||||
}
|
||||
if counter >= (size - lines) {
|
||||
fmt.Println(fmt.Sprintf(strconv.FormatInt(int64(counter), 10) + scanner.Text()))
|
||||
}
|
||||
}
|
||||
if i >= (size - lines) {
|
||||
fmt.Println(fmt.Sprintf(scanner.Text()))
|
||||
scanner.Scan()
|
||||
fmt.Print(fmt.Sprintf(strconv.FormatInt(int64(counter), 10) + scanner.Text()))
|
||||
} else {
|
||||
for i := 0; i < size-1; i++ {
|
||||
if !scanner.Scan() {
|
||||
return
|
||||
}
|
||||
if i >= (size - lines) {
|
||||
fmt.Println(fmt.Sprintf(scanner.Text()))
|
||||
}
|
||||
}
|
||||
scanner.Scan()
|
||||
fmt.Print(fmt.Sprintf(scanner.Text()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user