update: bouncing ball exercise only uses the screen package
This commit is contained in:
@ -18,41 +18,25 @@ import (
|
|||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
// EXERCISE: Adjust the width and height automatically
|
// EXERCISE: Adjust the width and height automatically
|
||||||
//
|
//
|
||||||
// In this exercise, your goal is simple:
|
|
||||||
//
|
|
||||||
// Instead of setting the width and height manually,
|
// Instead of setting the width and height manually,
|
||||||
// you need to get the width and height of the terminal
|
// you need to get the width and height of the terminal
|
||||||
// screen from your operating system.
|
// screen from your operating system.
|
||||||
//
|
//
|
||||||
// Don't worry, it is easier than it sounds. You just
|
// 1. Update your program to use my screen package.
|
||||||
// need to read a few documentation and install a
|
// It offers an easy way to get the width and height.
|
||||||
// Go package.
|
|
||||||
//
|
|
||||||
// 1. Go here: https://godoc.org/golang.org/x/crypto/ssh/terminal
|
|
||||||
//
|
|
||||||
// Download the package:
|
|
||||||
// go get -u golang.org/x/crypto/ssh/terminal
|
|
||||||
//
|
|
||||||
// 2. Find the function that gives you the width and height
|
|
||||||
// of the terminal.
|
|
||||||
//
|
|
||||||
// 3. Call that function from your program and get the
|
|
||||||
// width and height.
|
|
||||||
//
|
|
||||||
// 4. When an error occurs while retrieving the width
|
|
||||||
// and height, report it.
|
|
||||||
//
|
|
||||||
// 5. Set the width and height of the board.
|
|
||||||
//
|
|
||||||
// 6. After solving the above steps, update your program
|
|
||||||
// to use my screen package instead. It offers an
|
|
||||||
// easier way to get the width and height of a
|
|
||||||
// terminal.
|
|
||||||
//
|
//
|
||||||
// go get -u https://github.com/inancgumus/screen
|
// go get -u https://github.com/inancgumus/screen
|
||||||
//
|
//
|
||||||
|
// 2. Read the package's documentation and find a way to
|
||||||
|
// get the screen size: width and height.
|
||||||
//
|
//
|
||||||
// BONUS
|
// The documentation is here:
|
||||||
|
// https://godoc.org/github.com/inancgumus/screen
|
||||||
|
//
|
||||||
|
// 3. Use it to set the board's dimensions.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// OPTIONAL EXERCISE
|
||||||
//
|
//
|
||||||
// 1. When you set the width, you may see that the ball
|
// 1. When you set the width, you may see that the ball
|
||||||
// goes beyond the left and right borders. This happens
|
// goes beyond the left and right borders. This happens
|
||||||
@ -83,19 +67,6 @@ import (
|
|||||||
// wasn't matter whether your terminal was bigger or
|
// wasn't matter whether your terminal was bigger or
|
||||||
// smaller, but now it will be!
|
// smaller, but now it will be!
|
||||||
//
|
//
|
||||||
//
|
|
||||||
// HINT
|
|
||||||
//
|
|
||||||
// Please take a look at this if you get stuck.
|
|
||||||
//
|
|
||||||
// You need to pass the Standard Out file handler
|
|
||||||
// to the function that returns you the dimensions.
|
|
||||||
//
|
|
||||||
// Check out my screen package to find out how I'm
|
|
||||||
// passing the Standard Out file handler.
|
|
||||||
//
|
|
||||||
// https://github.com/inancgumus/screen/blob/master/dimensions.go
|
|
||||||
//
|
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -10,13 +10,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattn/go-runewidth"
|
"github.com/mattn/go-runewidth"
|
||||||
|
|
||||||
"github.com/inancgumus/screen"
|
"github.com/inancgumus/screen"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -35,15 +33,8 @@ func main() {
|
|||||||
cell rune // current cell (for caching)
|
cell rune // current cell (for caching)
|
||||||
)
|
)
|
||||||
|
|
||||||
// get the width and height
|
|
||||||
width, height, err := terminal.GetSize(int(os.Stdout.Fd()))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// you can get the width and height using the screen package easily:
|
// you can get the width and height using the screen package easily:
|
||||||
// width, height := screen.Size()
|
width, height := screen.Size()
|
||||||
|
|
||||||
// get the rune width of the ball emoji
|
// get the rune width of the ball emoji
|
||||||
ballWidth := runewidth.RuneWidth(cellBall)
|
ballWidth := runewidth.RuneWidth(cellBall)
|
||||||
|
Reference in New Issue
Block a user