feat: "command window" logging on startup
This commit is contained in:
parent
e9ce1a94f4
commit
2355d6a053
79
main.go
79
main.go
@ -32,13 +32,25 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
chatViewportStyle = lipgloss.NewStyle().
|
||||
centerViewportStyle = lipgloss.NewStyle().
|
||||
BorderStyle(lipgloss.NormalBorder()).
|
||||
BorderForeground(lipgloss.Color("63")).
|
||||
Padding(2)
|
||||
profileViewportStyle = chatViewportStyle
|
||||
leftViewportStyle = centerViewportStyle
|
||||
)
|
||||
|
||||
type cmdWindow struct {
|
||||
messages []string
|
||||
}
|
||||
|
||||
func (c *cmdWindow) log(msg string) {
|
||||
c.messages = append(c.messages, fmt.Sprintf("> %s", msg))
|
||||
}
|
||||
|
||||
func (c *cmdWindow) view() string {
|
||||
return strings.Join(c.messages, "\n")
|
||||
}
|
||||
|
||||
// model offers the core of the state for the entire UI.
|
||||
type model struct {
|
||||
username string // Name of user
|
||||
@ -50,9 +62,11 @@ type model struct {
|
||||
|
||||
appInitialised bool
|
||||
|
||||
chatViewport viewport.Model
|
||||
profileViewport viewport.Model
|
||||
input textinput.Model
|
||||
leftViewport viewport.Model
|
||||
centerViewport viewport.Model
|
||||
input textinput.Model
|
||||
|
||||
cmdWindow *cmdWindow
|
||||
}
|
||||
|
||||
// initialModel constucts an initial state for the UI.
|
||||
@ -60,15 +74,20 @@ func initialModel(username, homeDir string, width, height int) model {
|
||||
input := textinput.New()
|
||||
input.Focus()
|
||||
|
||||
chatViewport := viewport.New(width, height/2)
|
||||
profileViewport := viewport.New(20, height/2)
|
||||
leftViewport := viewport.New(20, height/2)
|
||||
centerViewport := viewport.New(width, height/2)
|
||||
|
||||
cmdWindow := cmdWindow{}
|
||||
|
||||
return model{
|
||||
username: username,
|
||||
userDir: path.Join(homeDir, "/.cairde/"),
|
||||
chatViewport: chatViewport,
|
||||
profileViewport: profileViewport,
|
||||
input: input,
|
||||
username: username,
|
||||
userDir: path.Join(homeDir, "/.cairde/"),
|
||||
|
||||
leftViewport: leftViewport,
|
||||
centerViewport: centerViewport,
|
||||
input: input,
|
||||
|
||||
cmdWindow: &cmdWindow,
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,6 +106,8 @@ type appInitialisedMsg struct {
|
||||
|
||||
// initApp initialises Cwtch (e.g. Tor, profiles, etc.).
|
||||
func initApp(m model) tea.Msg {
|
||||
m.cmdWindow.log("attempting to initialise tor")
|
||||
|
||||
mrand.Seed(int64(time.Now().Nanosecond()))
|
||||
port := mrand.Intn(1000) + 9600
|
||||
controlPort := port + 1
|
||||
@ -127,10 +148,12 @@ func initApp(m model) tea.Msg {
|
||||
return uiErrMsg{Err: fmt.Errorf("unable to bootstrap tor: %s", err)}
|
||||
}
|
||||
|
||||
m.cmdWindow.log("waiting for ACN to bootstrap...")
|
||||
if err := acn.WaitTillBootstrapped(); err != nil {
|
||||
return uiErrMsg{Err: fmt.Errorf("unable to initialise tor: %s", err)}
|
||||
}
|
||||
|
||||
m.cmdWindow.log("loading cwtch global settings")
|
||||
settingsFile, err := settings.InitGlobalSettingsFile(m.userDir, "")
|
||||
if err != nil {
|
||||
return uiErrMsg{Err: fmt.Errorf("unable to initialise settings: %s", err)}
|
||||
@ -142,6 +165,8 @@ func initApp(m model) tea.Msg {
|
||||
|
||||
app := app.NewApp(acn, m.userDir, settingsFile)
|
||||
app.InstallEngineHooks(m.engineHooks)
|
||||
|
||||
m.cmdWindow.log("loading unencrypted cwtch profiles")
|
||||
app.LoadProfiles("")
|
||||
|
||||
return appInitialisedMsg{
|
||||
@ -165,15 +190,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
cmds []tea.Cmd
|
||||
)
|
||||
|
||||
m.leftViewport, cmd = m.leftViewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
m.centerViewport, cmd = m.centerViewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
m.input, cmd = m.input.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
m.chatViewport, cmd = m.chatViewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
m.profileViewport, cmd = m.profileViewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case uiErrMsg: // TODO
|
||||
|
||||
@ -183,15 +208,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.app = msg.app
|
||||
m.acn = msg.acn
|
||||
m.appInitialised = true
|
||||
m.cmdWindow.log("tor successfully initialised")
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
if !m.appInitialised {
|
||||
break // init before clean up
|
||||
m.cmdWindow.log("still initialising... just a sec")
|
||||
break
|
||||
}
|
||||
|
||||
m.app.Shutdown()
|
||||
m.acn.Close()
|
||||
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
@ -203,19 +232,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
func (m model) View() string {
|
||||
body := strings.Builder{}
|
||||
|
||||
if !m.appInitialised {
|
||||
m.chatViewport.SetContent("initialising...")
|
||||
} else {
|
||||
m.chatViewport.SetContent("initialised!")
|
||||
}
|
||||
m.centerViewport.SetContent(m.cmdWindow.view())
|
||||
|
||||
panes := lipgloss.JoinHorizontal(
|
||||
lipgloss.Left,
|
||||
profileViewportStyle.Render(m.profileViewport.View()),
|
||||
chatViewportStyle.Render(m.chatViewport.View()),
|
||||
leftViewportStyle.Render(m.leftViewport.View()),
|
||||
centerViewportStyle.Render(m.centerViewport.View()),
|
||||
)
|
||||
|
||||
body.WriteString(panes)
|
||||
|
||||
body.WriteString("\n" + m.input.View())
|
||||
|
||||
return body.String()
|
||||
|
Reference in New Issue
Block a user