From 952eb7df1520028a104ec921bfd3f3db3de832cf Mon Sep 17 00:00:00 2001 From: John Date: Thu, 5 Sep 2019 20:00:01 +0800 Subject: [PATCH] add Scan/Scanf functions for gcmd; improve gfile.IsEmpty --- .example/frame/main/model/model1.go | 2 -- .example/other/test.go | 18 +++--------------- os/gcmd/gcmd_scan.go | 26 ++++++++++++++++++++++++++ os/gfile/gfile.go | 8 +++++--- 4 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 os/gcmd/gcmd_scan.go diff --git a/.example/frame/main/model/model1.go b/.example/frame/main/model/model1.go index 651976d41..8b7bf2264 100644 --- a/.example/frame/main/model/model1.go +++ b/.example/frame/main/model/model1.go @@ -10,6 +10,4 @@ func main() { user, err := test.ModelUser().One() g.Dump(err) g.Dump(user) - user.Password = "1" - g.Dump(user.Update()) } diff --git a/.example/other/test.go b/.example/other/test.go index efc86bd9a..4bdb33247 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,23 +1,11 @@ package main import ( - "os" + "fmt" - "github.com/olekukonko/tablewriter" + "github.com/gogf/gf/os/gcmd" ) func main() { - data := [][]string{ - []string{"1/1/2014", "Domain name", "2233", "$10.98"}, - []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, - []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, - []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, - } - - table := tablewriter.NewWriter(os.Stdout) - table.SetBorder(false) - table.SetRowLine(false) - table.SetColumnSeparator("") - table.AppendBulk(data) - table.Render() + fmt.Println(gcmd.Scan("input:")) } diff --git a/os/gcmd/gcmd_scan.go b/os/gcmd/gcmd_scan.go new file mode 100644 index 000000000..a27681819 --- /dev/null +++ b/os/gcmd/gcmd_scan.go @@ -0,0 +1,26 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. +// + +package gcmd + +import "fmt" + +// Scan prints to stdout, reads and returns user input, which stops by '\n'. +func Scan(info ...interface{}) string { + var s string + fmt.Print(info...) + fmt.Scan(&s) + return s +} + +// Scanf prints to stdout with , reads and returns user input, which stops by '\n'. +func Scanf(format string, info ...interface{}) string { + var s string + fmt.Printf(format, info...) + fmt.Scan(&s) + return s +} diff --git a/os/gfile/gfile.go b/os/gfile/gfile.go index 73a645e3b..0f98622ca 100644 --- a/os/gfile/gfile.go +++ b/os/gfile/gfile.go @@ -407,20 +407,22 @@ func Dir(path string) string { // IsEmpty checks whether the given is empty. // If is a folder, it checks if there's any file under it. // If is a file, it checks if the file size is zero. +// +// Note that it returns true if does not exist. func IsEmpty(path string) bool { stat, err := Stat(path) if err != nil { - return false + return true } if stat.IsDir() { file, err := os.Open(path) if err != nil { - return false + return true } defer file.Close() names, err := file.Readdirnames(-1) if err != nil { - return false + return true } return len(names) == 0 } else {