Implementing the pic command
This commit is contained in:
parent
b39f6c62ba
commit
1fc88a4cca
2 changed files with 60 additions and 6 deletions
|
@ -2,14 +2,60 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
_ "image/jpeg"
|
||||||
|
_ "image/png"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/nfnt/resize"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SendAsciiPic sends an image as ascii text to the client.
|
||||||
|
func SendAsciiPic(c Client, path string) error {
|
||||||
|
// From: https://github.com/stdupp/goasciiart/blob/master/goasciiart.go
|
||||||
|
var w = 80
|
||||||
|
f, err := os.Open(MakePathFromStringStack(c.Stack()) + path)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
img, _, err := image.Decode(f)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sz := img.Bounds()
|
||||||
|
h := (sz.Max.Y * w * 10) / (sz.Max.X * 16)
|
||||||
|
img = resize.Resize(uint(80), uint(h), img, resize.Lanczos3)
|
||||||
|
|
||||||
|
table := []byte("MND8OZ$7I?+=~:,..")
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
for i := 0; i < h; i++ {
|
||||||
|
for j := 0; j < w; j++ {
|
||||||
|
g := color.GrayModel.Convert(img.At(j, i))
|
||||||
|
y := reflect.ValueOf(g).FieldByName("Y").Uint()
|
||||||
|
pos := int(y * 16 / 255)
|
||||||
|
buf.WriteByte(table[pos])
|
||||||
|
}
|
||||||
|
buf.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = c.Connection().Write(buf.Bytes())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// GetFile sends the file to the client and returns true if it succeeds and false otherwise.
|
// GetFile sends the file to the client and returns true if it succeeds and false otherwise.
|
||||||
// it also returns the total number of send bytes.
|
// it also returns the total number of send bytes.
|
||||||
func GetFile(c Client, path string) (int, error) {
|
func GetFile(c Client, path string) (int, error) {
|
||||||
|
@ -38,7 +84,7 @@ func GetFile(c Client, path string) (int, error) {
|
||||||
totalSend += n
|
totalSend += n
|
||||||
_, err = c.Connection().Write(data)
|
_, err = c.Connection().Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return totalSend, err
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,13 +122,9 @@ func ListFiles(c Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.Connection().Write(buffer.Bytes())
|
_, err = c.Connection().Write(buffer.Bytes())
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearScreen cleans the client's screen by sending clear to the terminal.
|
// ClearScreen cleans the client's screen by sending clear to the terminal.
|
||||||
func ClearScreen(c Client) error {
|
func ClearScreen(c Client) error {
|
||||||
// Ansi clear: 1b 5b 48 1b 5b 4a
|
// Ansi clear: 1b 5b 48 1b 5b 4a
|
||||||
|
@ -120,6 +162,7 @@ ls - List the files in the current directory.
|
||||||
cd - Changes the directory.
|
cd - Changes the directory.
|
||||||
clear - Clear the screen.
|
clear - Clear the screen.
|
||||||
exit - Close the connection with the server.c
|
exit - Close the connection with the server.c
|
||||||
|
pic - Returns the ascii art of an image. :-)
|
||||||
`
|
`
|
||||||
_, err := c.Connection().Write([]byte(helpText))
|
_, err := c.Connection().Write([]byte(helpText))
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,17 @@ func ProcessInput(c Client, text string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &InputError{thisCommand, err}
|
return &InputError{thisCommand, err}
|
||||||
}
|
}
|
||||||
|
case "pic":
|
||||||
|
err := checkArgumentsLength(commandsLen, 2)
|
||||||
|
if err != nil {
|
||||||
|
return &InputError{thisCommand, err}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the file
|
||||||
|
err = SendAsciiPic(c, commands[1])
|
||||||
|
if err != nil {
|
||||||
|
return &InputError{thisCommand, err}
|
||||||
|
}
|
||||||
case "ls":
|
case "ls":
|
||||||
err := checkArgumentsLength(commandsLen, 1)
|
err := checkArgumentsLength(commandsLen, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue