Skip to content

Commit

Permalink
tools: support multiple languages #78
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jan 28, 2024
1 parent 6fcf178 commit bee0ffd
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 19 deletions.
6 changes: 6 additions & 0 deletions tssh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type sshHost struct {
}

type tsshConfig struct {
language string
configPath string
sysConfigPath string
exConfigPath string
Expand Down Expand Up @@ -126,6 +127,8 @@ func parseTsshConfig() {
continue
}
switch {
case name == "language" && userConfig.language == "":
userConfig.language = value
case name == "configpath" && userConfig.configPath == "":
userConfig.configPath = resolveHomeDir(value)
case name == "exconfigpath" && userConfig.exConfigPath == "":
Expand Down Expand Up @@ -173,6 +176,9 @@ func parseTsshConfig() {
}

func showTsshConfig() {
if userConfig.language != "" {
debug("Language = %s", userConfig.language)
}
if userConfig.configPath != "" {
debug("ConfigPath = %s", userConfig.configPath)
}
Expand Down
111 changes: 111 additions & 0 deletions tssh/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
MIT License
Copyright (c) 2023-2024 The Trzsz SSH Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package tssh

import (
"fmt"
"os"
"path/filepath"
"strings"
)

var english = map[string]string{
"tools/help": "-- Press Enter to accept the default options provided in brackets. Ctrl+C to exit.",
"newhost/title": "================================= Add New Host =================================",
"newhost/config": "-- Generally, just press Enter to use the default configuration path in brackets.",
"newhost/alias": "-- Give the server an alias as you like.",
"newhost/host": "-- Enter the IP address or domain name of the server.",
"newhost/port": "-- Enter the server port, the default is 22.",
"newhost/user": "-- Enter your login username.",
"newhost/passwd": "-- Public key authentication or no need to remember password, press Enter to skip.",
"newhost/login": "-- Added successfully, enter Y or Yes (case insensitive) to log in immediately.",
}

var chinese = map[string]string{
"tools/help": "-- 可以直接按回车键接受括号内提供的默认选项,使用 Ctrl+C 可以立即退出",
"newhost/title": "================================ 新增服务器配置 ================================",
"newhost/config": "-- SSH 配置文件路径,一般直接按回车键使用括号内的默认值即可。",
"newhost/alias": "-- 随便给服务器起个别名,如设置为 xxx 则可以使用 tssh xxx 快速登录此服务器。",
"newhost/host": "-- 请输入服务器 IP。如果是使用域名登录的,也可以输入服务器域名。",
"newhost/port": "-- 请输入服务器端口,默认是22。",
"newhost/user": "-- 请输入登录用户名。",
"newhost/passwd": "-- 使用公私钥登录,或者无需记住密码,请直接按回车跳过。",
"newhost/login": "-- 新服务器配置已成功写入,输入 Y 或 Yes( 不区分大小写 )可以立即登录。",
}

func getText(key string) string {
switch userConfig.language {
case "english":
return english[key]
case "chinese":
return chinese[key]
default:
return english[key]
}
}

func chooseLanguage() {
switch strings.ToLower(userConfig.language) {
case "english":
userConfig.language = "english"
return
case "chinese":
userConfig.language = "chinese"
return
}
if userConfig.language != "" {
warning("Language [%s] is not support yet, English will be used by default.", userConfig.language)
return
}

language := promptList("Please choose your preferred language", "", []string{"English", "简体中文"})
switch language {
case "English":
language = "English"
userConfig.language = "english"
case "简体中文":
language = "Chinese"
userConfig.language = "chinese"
}

path := filepath.Join(userHomeDir, ".tssh.conf")
if err := writeLanguage(path, language); err != nil {
warning("write language [%s] to %s failed:", language, path, err)
return
}
toolsInfo(fmt.Sprintf("Language = %s", language), "has been written to %s", path)
}

func writeLanguage(path, language string) error {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
if err != nil {
return err
}
defer file.Close()
if err := ensureNewline(file); err != nil {
return err
}
return writeAll(file, []byte(fmt.Sprintf("Language = %s\n", language)))
}
86 changes: 82 additions & 4 deletions tssh/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func toolsErrorExit(format string, a ...any) {

func printToolsHelp(title string) {
fmt.Print(lipgloss.NewStyle().Bold(true).Foreground(greenColor).Render(title) + "\r\n")
fmt.Print(lipgloss.NewStyle().Faint(true).Render(
"-- 可以直接按回车键接受括号内提供的默认选项,使用 Ctrl+C 可以立即退出") + "\r\n\r\n")
fmt.Print(lipgloss.NewStyle().Faint(true).Render(getText("tools/help")) + "\r\n\r\n")
}

type inputValidator struct {
Expand Down Expand Up @@ -186,7 +185,9 @@ func (m *textInputModel) View() string {
builder.WriteString(lipgloss.NewStyle().Foreground(magentaColor).Render(m.defaultValue))
builder.WriteByte(')')
}
builder.WriteString(m.textInput.View())
if !m.quit {
builder.WriteString(m.textInput.View())
}
builder.WriteString("\r\n")
if m.err != nil {
builder.WriteString(lipgloss.NewStyle().Foreground(redColor).Render(m.err.Error()))
Expand Down Expand Up @@ -326,7 +327,7 @@ func (m *passwordModel) View() string {
for i := 0; i < len(m.passwordInput); i++ {
builder.WriteByte('*')
}
if m.cursorVisible {
if !m.quit && m.cursorVisible {
builder.WriteRune('█')
} else {
builder.WriteRune(' ')
Expand Down Expand Up @@ -357,6 +358,83 @@ func promptPassword(promptLabel, helpMessage string, validator *inputValidator)
return ""
}

type listModel struct {
promptLabel string
helpMessage string
cursor int
items []string
done bool
quit bool
}

func (m *listModel) Init() tea.Cmd {
return nil
}

func (m *listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "q", "ctrl+c":
m.quit = true
return m, tea.Quit
case "j", "tab", "down":
m.cursor++
if m.cursor >= len(m.items) {
m.cursor = 0
}
case "k", "shift+tab", "up":
m.cursor--
if m.cursor < 0 {
m.cursor = len(m.items) - 1
}
case "enter":
m.done = true
return m, tea.Quit
}
}
return m, nil
}

func (m *listModel) View() string {
if m.done {
return ""
}
var builder strings.Builder
builder.WriteString(lipgloss.NewStyle().Foreground(cyanColor).Render(m.promptLabel+":") + "\r\n")
if m.helpMessage != "" {
builder.WriteString(lipgloss.NewStyle().Faint(true).Render(m.helpMessage) + "\r\n")
}
for i, item := range m.items {
if i == m.cursor {
builder.WriteString(lipgloss.NewStyle().Foreground(magentaColor).
Render(fmt.Sprintf("> %s", item)) + "\r\n")
} else {
builder.WriteString(lipgloss.NewStyle().Render(fmt.Sprintf(" %s", item)) + "\r\n")
}
}
builder.WriteString(lipgloss.NewStyle().Faint(true).
Render("Use ↓ ↑ j k or tab to navigate, Enter to choose.") + "\r\n")
return builder.String()
}

func promptList(promptLabel, helpMessage string, listItems []string) string {
m, err := tea.NewProgram(&listModel{
promptLabel: promptLabel,
helpMessage: helpMessage,
items: listItems,
}).Run()

if model, ok := m.(*listModel); err == nil && ok {
if model.quit {
os.Exit(0)
}
return model.items[model.cursor]
}
toolsErrorExit("input error: %v", err)
return ""
}

func isFileNotExistOrEmpty(path string) bool {
stat, err := os.Stat(path)
if os.IsNotExist(err) {
Expand Down
4 changes: 2 additions & 2 deletions tssh/tools_install_trzsz.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func getRemoteServerOS(client *ssh.Client) (string, error) {
case "linux":
return "linux", nil
default:
return "", fmt.Errorf("os [%s] does not support yet", name)
return "", fmt.Errorf("os [%s] is not support yet", name)
}
}

Expand All @@ -146,7 +146,7 @@ func getRemoteServerArch(client *ssh.Client) (string, error) {
case "i386", "i486", "i586", "i686":
return "i386", nil
default:
return "", fmt.Errorf("arch [%s] does not support yet", arch)
return "", fmt.Errorf("arch [%s] is not support yet", arch)
}
}

Expand Down
23 changes: 10 additions & 13 deletions tssh/tools_new_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ type newHostTool struct {
}

func (n *newHostTool) promptConfigPath() {
n.configPath = promptTextInput("ConfigPath", userConfig.configPath,
"-- SSH 配置文件路径,一般直接按回车键使用括号内的默认值即可。",
n.configPath = promptTextInput("ConfigPath", userConfig.configPath, getText("newhost/config"),
&inputValidator{func(path string) error {
if path == "" {
return fmt.Errorf("empty ssh config path")
Expand Down Expand Up @@ -80,8 +79,7 @@ func (n *newHostTool) promptConfigPath() {
}

func (n *newHostTool) promptHostAlias() {
n.hostAlias = promptTextInput("HostAlias", "",
"-- 随便给服务器起个别名,如设置为 xxx 则可以使用 tssh xxx 快速登录此服务器。",
n.hostAlias = promptTextInput("HostAlias", "", getText("newhost/alias"),
&inputValidator{func(alias string) error {
if alias == "" {
return fmt.Errorf("empty host alias")
Expand All @@ -106,8 +104,7 @@ func (n *newHostTool) promptHostAlias() {
}

func (n *newHostTool) promptHostName() {
n.hostName = promptTextInput("HostName/IP", "",
"-- 请输入服务器 IP。如果是使用域名登录的,也可以输入服务器域名。",
n.hostName = promptTextInput("HostName/IP", "", getText("newhost/host"),
&inputValidator{func(name string) error {
if name == "" {
return fmt.Errorf("empty host name")
Expand All @@ -120,8 +117,7 @@ func (n *newHostTool) promptHostName() {
}

func (n *newHostTool) promptHostPort() {
port, _ := strconv.ParseUint(promptTextInput("HostPort", "22",
"-- 请输入服务器端口,默认是22。",
port, _ := strconv.ParseUint(promptTextInput("HostPort", "22", getText("newhost/port"),
&inputValidator{func(port string) error {
if port == "" {
return fmt.Errorf("empty host port")
Expand All @@ -140,8 +136,7 @@ func (n *newHostTool) promptHostPort() {
}

func (n *newHostTool) promptUserName() {
n.userName = promptTextInput("UserName", "",
"-- 请输入登录用户名。",
n.userName = promptTextInput("UserName", "", getText("newhost/user"),
&inputValidator{func(name string) error {
if name == "" {
return fmt.Errorf("empty user name")
Expand All @@ -151,7 +146,7 @@ func (n *newHostTool) promptUserName() {
}

func (n *newHostTool) promptPassword() {
n.password = promptPassword("Password", "-- 使用公私钥登录,或者无需记住密码,请直接按回车跳过。",
n.password = promptPassword("Password", getText("newhost/passwd"),
&inputValidator{func(name string) error {
return nil
}})
Expand Down Expand Up @@ -185,13 +180,15 @@ Host %s

func (n *newHostTool) loginImmediately() bool {
return promptBoolInput("New host added successfully. Would you like to log in now?",
"-- 新服务器配置已成功写入,输入 Y 或 Yes( 不区分大小写 )可以立即登录。", false)
getText("newhost/login"), false)
}

func execNewHost(args *sshArgs) (int, bool) {
n := &newHostTool{}

printToolsHelp("================== Add New Host ( 新增服务器配置 ) ==================")
chooseLanguage()

printToolsHelp(getText("newhost/title"))

n.promptConfigPath()

Expand Down

0 comments on commit bee0ffd

Please sign in to comment.