Skip to content

Commit 1b79056

Browse files
committed
supporting longer participant names
1 parent ce99dac commit 1b79056

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

examples/main.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,34 @@ package main
33
import "github.com/mriceman/go-uml/sequence"
44

55
func main() {
6-
userStartsChatting()
7-
userBIsNotPartOfChat()
8-
UserBIsPartOfChat()
6+
userJoinsChat()
7+
}
8+
9+
func userJoinsChat() {
10+
d := sequence.NewDiagram("new_chat")
11+
userA := "userA (name could be quite longer)"
12+
userB := "userB"
13+
b := "backend"
14+
15+
d.SetTitle("User starts a chat with someone (doesn't matter if new or not)")
16+
17+
d.AddParticipants(userA, userB, b)
18+
19+
_ = d.AddDirectionalEdge(userA, b, `POST /inbox {"recipient": userID, "project": projectID, "projectType": challenge|solution}"`)
20+
_ = d.AddDirectionalEdge(b, userA, "Response Status Code: 202, Body: Inbox{id, secret, recipient, ...}")
21+
_ = d.AddDirectionalEdge(userA, b, "WS connect")
22+
_ = d.AddDirectionalEdge(userA, b, `"{"action": "JOIN", "data": {"inboxId": str, "userId": "str", "inboxSecret": str}}"`)
23+
_ = d.AddDirectionalEdge(b, b, "STORE_CONNECTION_ID(userA)")
24+
_ = d.AddDirectionalEdge(b, b, "sets unread inbox userA = 0")
25+
_ = d.AddDirectionalEdge(b, userA, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`)
26+
_ = d.AddDirectionalEdge(userA, b, `"{"action": "SEND_MESSAGE", "data": {"inboxId": str, "userId": "str", "inboxSecret": str, "content": "hi"}}"`)
27+
_ = d.AddDirectionalEdge(b, b, "check connectionID userB")
28+
_ = d.AddUndirectionalEdge(b, b, "if connected: ")
29+
_ = d.AddDirectionalEdge(b, userB, `{"event": "new_message", "messages": [{"id": str, "inboxId": str, "content": str, "userId": str, "createdAt": str}]}`)
30+
_ = d.AddUndirectionalEdge(b, b, "else: ")
31+
_ = d.AddUndirectionalEdge(b, b, "increment unread inbox userB")
932

33+
d.Render()
1034
}
1135

1236
func UserBIsPartOfChat() {
@@ -71,6 +95,7 @@ func userStartsChatting() {
7195
d.AddParticipants(backend)
7296
d.AddParticipants(db)
7397

98+
d.AddDirectionalEdge(client, backend, "GET /inbox/<TO_ID>")
7499
d.AddDirectionalEdge(client, backend, "PUT /chat/user/<TO_ID>")
75100
d.AddDirectionalEdge(backend, db, "checks or create inbox for user")
76101
d.AddDirectionalEdge(backend, db, "set all unread messages to read if existing")

sequence/diagram.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ func (d *Diagram) renderParticipants() {
7474
}
7575
}
7676
spacePerBlock := float64(d.dc.Width() / len(d.participants))
77-
startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding
78-
// startX := float64(participantsPadding + (len(d.renderedParticipants) * (participantBoxWidth + 1000/(len(d.participants)))))
79-
endX := startX + participantBoxWidth
80-
startY := 1000 * 0.1 // 10% from the top
77+
strWidth, strHeight := d.dc.MeasureString(p.Name)
78+
startX := spacePerBlock*float64(len(d.renderedParticipants)+1) - spacePerBlock/2 - participantsPadding - strWidth/2
79+
80+
endX := startX + participantBoxWidth + strWidth
81+
startY := height * 0.1 // 10% from the top
8182
endY := startY + participantBoxHeight
8283
// draw the border
8384
d.dc.SetColor(color.Black)
@@ -100,11 +101,10 @@ func (d *Diagram) renderParticipants() {
100101
d.dc.DrawRectangle(
101102
startX,
102103
startY,
103-
participantBoxWidth,
104+
participantBoxWidth+strWidth,
104105
participantBoxHeight,
105106
)
106107
d.dc.SetColor(color.Black)
107-
strWidth, strHeight := d.dc.MeasureString(p.Name)
108108
centerStrWidth := startX + ((endX - startX) / 2) - strWidth/2
109109
centerStrHeight := (endY-startY)/2 + startY + (strHeight / 2)
110110

@@ -137,11 +137,13 @@ func (d *Diagram) renderEdges() {
137137

138138
for idx := range d.edges {
139139
e := &d.edges[idx]
140+
fromStrWidth, _ := d.dc.MeasureString(e.from.Name)
141+
toStrWidth, _ := d.dc.MeasureString(e.to.Name)
140142
fromCords := d.participantsCoordMap[e.from.Name]
141143
toCords := d.participantsCoordMap[e.to.Name]
142-
startX := fromCords.X + participantBoxWidth/2 - 2.5 // 2.5 = half of stroke width
144+
startX := fromCords.X + participantBoxWidth/2 - 2.5 + fromStrWidth/2 // 2.5 = half of stroke width
143145
startY := fromCords.Y + participantBoxHeight + 2.5 + float64((1+renderedEdges)*verticalSpaceBetweenEdges)
144-
endX := toCords.X + participantBoxWidth/2 - 2.5
146+
endX := toCords.X + participantBoxWidth/2 - 2.5 + toStrWidth/2
145147
isReverseEdge := endX < startX
146148

147149
d.dc.SetDash(6)

0 commit comments

Comments
 (0)