wide/conf/user.go

163 lines
4.6 KiB
Go
Raw Permalink Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, b3log.org
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
2018-03-12 07:28:33 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package conf
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
2014-12-23 19:14:03 +03:00
"time"
)
2015-12-08 18:48:16 +03:00
// Panel represents a UI panel.
type Panel struct {
State string `json:"state"` // panel state, "min"/"max"/"normal"
Size uint16 `json:"size"` // panel size
}
// Layout represents the UI layout.
2015-01-19 05:19:38 +03:00
type Layout struct {
2015-12-08 18:48:16 +03:00
Side *Panel `json:"side"` // Side panel
SideRight *Panel `json:"sideRight"` // Right-Side panel
Bottom *Panel `json:"bottom"` // Bottom panel
2015-01-19 05:19:38 +03:00
}
// LatestSessionContent represents the latest session content.
type LatestSessionContent struct {
2015-12-08 18:48:16 +03:00
FileTree []string `json:"fileTree"` // paths of expanding nodes of file tree
Files []string `json:"files"` // paths of files of opening editor tabs
CurrentFile string `json:"currentFile"` // path of file of the current focused editor tab
Layout *Layout `json:"layout"` // UI Layout
}
// User configuration.
type User struct {
2019-05-16 20:41:04 +03:00
Id string
Name string
2019-05-16 18:17:25 +03:00
Avatar string
Workspace string // the GOPATH of this user (maybe contain several paths splitted by os.PathListSeparator)
Locale string
GoFormat string
GoBuildArgsForLinux string
GoBuildArgsForWindows string
GoBuildArgsForDarwin string
FontFamily string
FontSize string
Theme string
Keymap string // wide/vim
Created int64 // user create time in unix nano
Updated int64 // preference update time in unix nano
Lived int64 // the latest session activity in unix nano
Editor *editor
LatestSessionContent *LatestSessionContent
}
// Editor configuration of a user.
type editor struct {
FontFamily string
FontSize string
LineHeight string
Theme string
TabSize string
}
2019-05-16 18:37:04 +03:00
// Save saves the user's configurations in conf/users/{userId}.json.
func (u *User) Save() bool {
bytes, err := json.MarshalIndent(u, "", " ")
if nil != err {
logger.Error(err)
return false
}
2016-01-14 10:45:52 +03:00
if "" == string(bytes) {
2019-05-16 18:17:25 +03:00
logger.Error("Truncated user [" + u.Id + "]")
2016-01-14 10:45:52 +03:00
return false
}
2019-05-16 20:41:04 +03:00
if err = ioutil.WriteFile(filepath.Join(Wide.Data, "users", u.Id+".json"), bytes, 0644); nil != err {
logger.Error(err)
return false
}
return true
}
2019-05-16 18:17:25 +03:00
// NewUser creates a user with the specified username and workspace.
func NewUser(id, name, avatar, workspace string) *User {
now := time.Now().UnixNano()
return &User{Id: id, Name: name, Avatar: avatar, Workspace: workspace,
Locale: Wide.Locale, GoFormat: "gofmt",
2023-07-31 13:27:08 +03:00
GoBuildArgsForLinux: "", GoBuildArgsForWindows: "", GoBuildArgsForDarwin: "",
2019-05-16 18:17:25 +03:00
FontFamily: "Helvetica", FontSize: "13px", Theme: "default",
Keymap: "wide",
Created: now, Updated: now, Lived: now,
Editor: &editor{FontFamily: "Consolas, 'Courier New', monospace", FontSize: "inherit", LineHeight: "17px",
Theme: "wide", TabSize: "4"}}
}
2017-05-05 08:35:09 +03:00
// WorkspacePath gets workspace path of the user.
//
// Compared to the use of Wide.Workspace, this function will be processed as follows:
// 1. Replace {WD} variable with the actual directory path
// 2. Replace ${GOPATH} with enviorment variable GOPATH
// 3. Replace "/" with "\\" (Windows)
2017-05-05 08:35:09 +03:00
func (u *User) WorkspacePath() string {
2019-05-16 20:06:01 +03:00
w := u.Workspace
w = strings.Replace(w, "${GOPATH}", os.Getenv("GOPATH"), 1)
return filepath.FromSlash(w)
}
2017-05-05 08:35:09 +03:00
// BuildArgs get build args with the specified os.
func (u *User) BuildArgs(os string) []string {
2017-03-27 04:59:35 +03:00
var tmp string
if os == "windows" {
tmp = u.GoBuildArgsForWindows
2017-03-27 04:59:35 +03:00
}
if os == "linux" {
tmp = u.GoBuildArgsForLinux
2017-03-27 04:59:35 +03:00
}
if os == "darwin" {
tmp = u.GoBuildArgsForDarwin
2017-03-27 04:59:35 +03:00
}
2017-03-27 04:59:35 +03:00
exp := regexp.MustCompile(`[^\s"']+|"([^"]*)"|'([^']*)'`)
ret := exp.FindAllString(tmp, -1)
for idx := range ret {
ret[idx] = strings.Replace(ret[idx], "\"", "", -1)
2017-03-27 04:59:35 +03:00
}
return ret
}
2017-05-05 08:35:09 +03:00
// GetOwner gets the user the specified path belongs to. Returns "" if not found.
func GetOwner(path string) string {
for _, user := range Users {
if strings.HasPrefix(path, user.WorkspacePath()) {
2019-05-16 18:17:25 +03:00
return user.Id
2017-05-05 08:35:09 +03:00
}
}
return ""
}