A few days ago on the microblog saw someone recommended lazygit this tool, let a person in front of a bright, when the command line can also be so robbed,, research, found it used gocui, use it can make a lot of very flashy command line tools.
There are existing hosts tools switchosts!
, but I used to find that he still has bugs, can only be used in the interface platform. Think about it and get ready to start with a command-line hsots Configuration tool, this class library can be fully satisfied.
Interface design
In the interface design, or do a copycat, plagiarism, switchosts!
divided into two columns, the left is the hosts group, the right side is currently focused on the hosts group content, control switch on the left side, as shown below. The left switch controls whether each group is turned on or off, and the current hosts of the system are the collection of all open hosts on the left.
*------*-------------------*|×name1|# hosts for name2 ||√name2|1.1.1.1 a.com ||×name3|0.0.0.0 abc.com || | || | || | |*------*-------------------*
The implementation principle of the hosts
Principle and switchosts!
similar, by modifying the system Hosts file to achieve the purpose, each change will be reflected in real-time changes in the system Hosts file, so that the purpose of controlling the hosts.
hosts 配置软件 系统hosts*------*-------------------* *-------------------------*|×name1|# hosts for name2 | |# hosts for name2 ||√name2|1.1.1.1 a.com | |1.1.1.1 a.com ||√name3|0.0.0.0 abc.com | |0.0.0.0 abc.com | | | | => |# hosts for name3 || | | |127.0.0.1 localhost || | | | |*------*-------------------* *-------------------------*
What Gocui has to offer us
Gocui is a powerful library that Go Console User Interface
provides a command-line user interface, as he says. He gave us the ability to control the refresh of terminal, such as dividing terminal into multiple view (partitions), each view can independently control the refresh, response to the keyboard events, so as long as you want, you can even make some simple mini-games.
Gohosts
So far the function of gohosts has been basically developed, the main function is to switch hosts, have the following interaction:
- When you focus on the left view, you
shift + a
create a new hosts group by combining keys.
tab
Toggle the left and right view by key.
- When you focus on the layer view, when you create a new hosts group, you enter the layer view, enter the content, and press ENTER to take effect.
- Toggles the hosts group by the up and down arrows when focusing on the left view.
- When focusing on the left view, switch the hosts group switch by the left and right arrows.
Gohosts implementation
Because to modify the hosts, it is necessary to run the program must also use root permissions, the first time, the initial operation will be automatically initialized, generate backup
groups, and the system currently has the hosts content into it.
The gohosts configuration file is placed under the folder under the user's home directory, where the .gohost
configuration file is read and displayed each time it is started. In addition, in order to be compatible, the Windows system and Linux system are differentiated, and the getUserHome
user home directory is obtained through functions. In addition, as a copycat, from switchosts!
where to learn that some Windows system disk is not installed in the C-disk, by getWinSystemDir
acquiring Windows system disk directory, to achieve as far as possible the full platform, multi-environment compatible.
func getUserHome() string { home := "" if runtime.GOOS == "windows" { home = os.Getenv("USERPROFILE") } else { home = os.Getenv("HOME") } return home}func getWinSystemDir() string { dir := "" if runtime.GOOS == "windows" { dir = os.Getenv("windir") } return dir}
In addition here 2 times to use the go process to check the syntax, start the software, start the process, every 50ms detection of the input content is not compliant, as shown below, the process is a dead loop, if non-compliant, red display.
go func() { for range time.Tick(time.Millisecond * 50) { // do something }}()
For example, when we enter the hosts configuration, we must satisfy the following 3, otherwise you will be prompted which line has errors.
"^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(?:[ ]{1,})(?:[ 0-9a-zA-Z._-]{1,}){1,}$""^[ ]*$""^#.*"
And when we entered the new hosts group, if the hosts group name already exists, or the hosts group name exceeds the length, it will also be marked red in the input layer view.
Golang's process of processing these content is unique, easy to handle, but also very simple and efficient.
Another troublesome place is to control the focus of the cursor, the left to increase the hosts group, and when the number of groups more than the maximum number of display lines, this is quite the case of the canvas, picture frame, when moving the focus, need to make the corresponding mobile canvas or mobile picture frame, haha, It is interesting to look at the logic in the code.
Gohosts Effect Show
Summarize
I have actually used a few days, but also easy to use, the more regrettable is not support Chinese, Chinese will appear incomplete problems, this is dependent on the library caused by the estimated more laborious, but for the hosts, the whole English is not a problem.
Original link:
Http://www.5aikid.com/2018/08 ...
GitHub Address:
Https://github.com/aizuyan/go ...
In addition this is the first real use of Golang to make some useful things, feel pretty good, ready to be a little busy, well study this
Dependent Library Gocui.