mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-01 08:01:00 +03:00
host: Add timestamps into prompt when enabled
This commit is contained in:
parent
dca2cec67f
commit
4188a3bdac
37
host.go
37
host.go
@ -25,6 +25,10 @@ func GetPrompt(user *message.User) string {
|
|||||||
if cfg.Theme != nil {
|
if cfg.Theme != nil {
|
||||||
name = cfg.Theme.ColorName(user)
|
name = cfg.Theme.ColorName(user)
|
||||||
}
|
}
|
||||||
|
if cfg.Timestamp && cfg.Theme != nil {
|
||||||
|
ts := cfg.Theme.Timestamp(time.Now())
|
||||||
|
return fmt.Sprintf("%s [%s] ", ts, name)
|
||||||
|
}
|
||||||
return fmt.Sprintf("[%s] ", name)
|
return fmt.Sprintf("[%s] ", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,6 +132,12 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
term.AutoCompleteCallback = h.AutoCompleteFunction(user)
|
term.AutoCompleteCallback = h.AutoCompleteFunction(user)
|
||||||
user.SetHighlight(user.Name())
|
user.SetHighlight(user.Name())
|
||||||
|
|
||||||
|
// Update prompt once per minute
|
||||||
|
stopUpdater := make(chan struct{}, 1)
|
||||||
|
defer func() { stopUpdater <- struct{}{} }()
|
||||||
|
// FIXME: Would be nice to unify this into a single goroutine rather than one per user.
|
||||||
|
go h.promptUpdater(term, user, stopUpdater)
|
||||||
|
|
||||||
// Should the user be op'd on join?
|
// Should the user be op'd on join?
|
||||||
if h.isOp(term.Conn) {
|
if h.isOp(term.Conn) {
|
||||||
member.IsOp = true
|
member.IsOp = true
|
||||||
@ -166,7 +176,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
h.HandleMsg(m)
|
h.HandleMsg(m)
|
||||||
|
|
||||||
cmd := m.Command()
|
cmd := m.Command()
|
||||||
if cmd == "/nick" || cmd == "/theme" {
|
if cmd == "/nick" || cmd == "/theme" || cmd == "/timestamp" {
|
||||||
// Hijack /nick command to update terminal synchronously. Wouldn't
|
// Hijack /nick command to update terminal synchronously. Wouldn't
|
||||||
// work if we use h.room.Send(m) above.
|
// work if we use h.room.Send(m) above.
|
||||||
//
|
//
|
||||||
@ -185,6 +195,31 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
logger.Debugf("[%s] Leaving: %s", term.Conn.RemoteAddr(), user.Name())
|
logger.Debugf("[%s] Leaving: %s", term.Conn.RemoteAddr(), user.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Host) promptUpdater(term *sshd.Terminal, user *message.User, stopper <-chan struct{}) {
|
||||||
|
now := time.Now()
|
||||||
|
interval := time.Second * 60
|
||||||
|
nextMinute := time.Duration(60-now.Second()) * time.Second
|
||||||
|
setupWait := time.After(nextMinute)
|
||||||
|
timer := time.NewTimer(interval)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-setupWait:
|
||||||
|
// Wait until we're at :00 seconds so that minute-resolution
|
||||||
|
// timestamps happen on the minute change. This only happens once.
|
||||||
|
timer.Reset(interval)
|
||||||
|
term.SetPrompt(GetPrompt(user))
|
||||||
|
term.Write([]byte{}) // Empty write to re-render the prompt
|
||||||
|
case <-timer.C:
|
||||||
|
term.SetPrompt(GetPrompt(user))
|
||||||
|
term.Write([]byte{}) // Empty write to re-render the prompt
|
||||||
|
case <-stopper:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Serve our chat room onto the listener
|
// Serve our chat room onto the listener
|
||||||
func (h *Host) Serve() {
|
func (h *Host) Serve() {
|
||||||
h.listener.HandlerFunc = h.Connect
|
h.listener.HandlerFunc = h.Connect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user