Compare commits
16 Commits
aad4da3077
...
2932f9bf43
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2932f9bf43 | ||
|
|
6c63fc7122 | ||
|
|
a545c4cab3 | ||
| 2661860245 | |||
| eaf6c974af | |||
|
|
561e2c3edd | ||
|
|
6d83e1b5bf | ||
|
|
ec6ddb4a7e | ||
|
|
431d26db8e | ||
|
|
bd13a0ebe9 | ||
|
|
84b969a98c | ||
|
|
13a510550e | ||
|
|
a014ea71d2 | ||
|
|
a785c7df3b | ||
|
|
0f95938d33 | ||
|
|
b1b9854a6d |
@ -5,6 +5,7 @@ import (
|
|||||||
"bbash/command/cd"
|
"bbash/command/cd"
|
||||||
"bbash/command/cp"
|
"bbash/command/cp"
|
||||||
"bbash/command/echo"
|
"bbash/command/echo"
|
||||||
|
"bbash/command/fritiofcommand"
|
||||||
"bbash/command/head"
|
"bbash/command/head"
|
||||||
"bbash/command/help"
|
"bbash/command/help"
|
||||||
"bbash/command/ls"
|
"bbash/command/ls"
|
||||||
@ -12,6 +13,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"
|
||||||
@ -36,6 +38,8 @@ func Run_command(in input_parser.Input, env *environment.Env) {
|
|||||||
pwd.Pwd(in, env)
|
pwd.Pwd(in, env)
|
||||||
case "echo":
|
case "echo":
|
||||||
echo.Echo(in, env)
|
echo.Echo(in, env)
|
||||||
|
case "fritiof":
|
||||||
|
fritiofcommand.Fritiof(in, env)
|
||||||
case "ls":
|
case "ls":
|
||||||
ls.Ls(in, env)
|
ls.Ls(in, env)
|
||||||
case "cd":
|
case "cd":
|
||||||
@ -56,6 +60,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))
|
||||||
|
|||||||
@ -6,12 +6,29 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Head(in input_parser.Input, env *environment.Env) {
|
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
|
||||||
|
return
|
||||||
|
}
|
||||||
|
args := in.Args
|
||||||
var file_path string
|
var file_path string
|
||||||
var lines int
|
var lines int
|
||||||
if len(args) >= 1 {
|
if len(args) >= 1 {
|
||||||
@ -36,15 +53,24 @@ func Head(in input_parser.Input, env *environment.Env) {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
scanner.Scan()
|
||||||
|
fmt.Print(fmt.Sprintf(strconv.FormatInt(int64(counter), 10) + scanner.Text()))
|
||||||
|
} else {
|
||||||
for i := 0; i < lines-1; i++ {
|
for i := 0; i < lines-1; i++ {
|
||||||
if(!scanner.Scan()) {
|
if !scanner.Scan() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(scanner.Text())
|
fmt.Println(scanner.Text())
|
||||||
}
|
}
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
fmt.Print(scanner.Text())
|
fmt.Print(scanner.Text())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -9,11 +9,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Ls(in input_parser.Input, env *environment.Env) {
|
func Ls(in input_parser.Input, env *environment.Env) {
|
||||||
flagsArray := []string {
|
flagsArray := []string{
|
||||||
"a ",
|
"a ",
|
||||||
"help ",
|
"help ",
|
||||||
}
|
}
|
||||||
flagsDictionary := map[string]string {
|
flagsDictionary := map[string]string{
|
||||||
"a": "-a shows hidden directories",
|
"a": "-a shows hidden directories",
|
||||||
"help": "-help shows this message",
|
"help": "-help shows this message",
|
||||||
}
|
}
|
||||||
@ -22,7 +22,8 @@ func Ls(in input_parser.Input, env *environment.Env) {
|
|||||||
fmt.Printf("Supported flags are:")
|
fmt.Printf("Supported flags are:")
|
||||||
for _, flag := range flagsArray {
|
for _, flag := range flagsArray {
|
||||||
fmt.Println(flag + flagsDictionary[flag])
|
fmt.Println(flag + flagsDictionary[flag])
|
||||||
} // Print flags and there description
|
} // Print flags and their description
|
||||||
|
return
|
||||||
}
|
}
|
||||||
fs, err := os.ReadDir(env.Path)
|
fs, err := os.ReadDir(env.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -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 or 0 is provided. Argument <file> <number of lines> \nTakes the flag -n in order to display the line numbers")
|
||||||
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> \nTakes the flag -n in order to display the line numbers")
|
||||||
|
default:
|
||||||
|
fmt.Println(fmt.Sprintf("No such command! (%s)", in.Instruction))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
128
command/tail/tail.go
Normal file
128
command/tail/tail.go
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
package tail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bbash/environment"
|
||||||
|
"bbash/input_parser"
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"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
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
if !scanner.Scan() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i >= (size - 10) {
|
||||||
|
fmt.Println(fmt.Sprintf(scanner.Text()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
@ -22,6 +22,3 @@ func Touch(in input_parser.Input, env *environment.Env) {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
70
input_parser/auto_complete.go
Normal file
70
input_parser/auto_complete.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package input_parser
|
||||||
|
|
||||||
|
var commandsTab []string
|
||||||
|
var prevInput string
|
||||||
|
var tabIndex int
|
||||||
|
|
||||||
|
func AutoComplete(input string) string {
|
||||||
|
|
||||||
|
inputLength := len(input)
|
||||||
|
|
||||||
|
if inputLength == 0 {
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
if tabIndex+2 > len(commandsTab) {
|
||||||
|
tabIndex = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if prevInput == input {
|
||||||
|
tabIndex = tabIndex + 1
|
||||||
|
|
||||||
|
prevInput = commandsTab[tabIndex]
|
||||||
|
return commandsTab[tabIndex]
|
||||||
|
}
|
||||||
|
|
||||||
|
prevInput = input
|
||||||
|
tabIndex = 0
|
||||||
|
|
||||||
|
currentCommands := []string{
|
||||||
|
"cat",
|
||||||
|
"cd",
|
||||||
|
"cp",
|
||||||
|
"echo",
|
||||||
|
"head",
|
||||||
|
"help",
|
||||||
|
"ls",
|
||||||
|
"man",
|
||||||
|
"mv",
|
||||||
|
"pwd",
|
||||||
|
"rm",
|
||||||
|
"tail",
|
||||||
|
"touch",
|
||||||
|
}
|
||||||
|
|
||||||
|
var commandsWorking []string
|
||||||
|
|
||||||
|
for _, currentCommand := range currentCommands {
|
||||||
|
|
||||||
|
if len(currentCommand) < inputLength {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
currentCommandWithoutEnd := currentCommand[:inputLength]
|
||||||
|
|
||||||
|
if currentCommandWithoutEnd != input {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
commandsWorking = append(commandsWorking, currentCommand)
|
||||||
|
}
|
||||||
|
|
||||||
|
commandsTab = commandsWorking
|
||||||
|
|
||||||
|
if len(commandsTab) == 0 {
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
prevInput = commandsTab[0]
|
||||||
|
return commandsTab[0]
|
||||||
|
}
|
||||||
@ -25,7 +25,7 @@ func Parse(env *environment.Env) Input {
|
|||||||
return parse(input)
|
return parse(input)
|
||||||
}
|
}
|
||||||
func parse(input string) Input {
|
func parse(input string) Input {
|
||||||
split := strings.Split(string(input), " ")
|
split := strings.Split(strings.TrimSpace(string(input)), " ")
|
||||||
instruction := strings.TrimSpace(split[0])
|
instruction := strings.TrimSpace(split[0])
|
||||||
|
|
||||||
args_raw := ""
|
args_raw := ""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user