@@ -75,36 +75,79 @@ type TmuxSession struct {
7575
7676func format () string {
7777 variables := []string {
78- "session_activity" ,
79- "session_alerts" ,
80- "session_attached" ,
81- "session_attached_list" ,
82- "session_created" ,
83- "session_format" ,
84- "session_group" ,
85- "session_group_attached" ,
86- "session_group_attached_list" ,
87- "session_group_list" ,
88- "session_group_many_attached" ,
89- "session_group_size" ,
90- "session_grouped" ,
91- "session_id" ,
92- "session_last_attached" ,
93- "session_many_attached" ,
94- "session_marked" ,
95- "session_name" ,
96- "session_path" ,
97- "session_stack" ,
98- "session_windows" ,
78+ "#{ session_activity} " ,
79+ "#{ session_alerts} " ,
80+ "#{ session_attached} " ,
81+ "#{ session_attached_list} " ,
82+ "#{ session_created} " ,
83+ "#{ session_format} " ,
84+ "#{ session_group} " ,
85+ "#{ session_group_attached} " ,
86+ "#{ session_group_attached_list} " ,
87+ "#{ session_group_list} " ,
88+ "#{ session_group_many_attached} " ,
89+ "#{ session_group_size} " ,
90+ "#{ session_grouped} " ,
91+ "#{ session_id} " ,
92+ "#{ session_last_attached} " ,
93+ "#{ session_many_attached} " ,
94+ "#{ session_marked} " ,
95+ "#{ session_name} " ,
96+ "#{ session_path} " ,
97+ "#{ session_stack} " ,
98+ "#{ session_windows} " ,
9999 }
100- variablesStr := ""
101- for i , variable := range variables {
102- variablesStr += "#{" + variable + "}"
103- if i != len (variables )- 1 {
104- variablesStr += " "
100+
101+ return strings .Join (variables , " " )
102+ }
103+
104+ func processSessions (sessionList []string ) []* TmuxSession {
105+ sessions := make ([]* TmuxSession , 0 , len (sessionList ))
106+ for _ , line := range sessionList {
107+ fields := strings .Split (line , " " ) // Strings split by single space
108+
109+ if len (fields ) != 21 {
110+ continue
111+ }
112+ if fields [2 ] == "1" {
113+ continue
114+ }
115+
116+ session := & TmuxSession {
117+ Activity : convert .StringToTime (fields [0 ]),
118+ Alerts : convert .StringToIntSlice (fields [1 ]),
119+ Attached : convert .StringToInt (fields [2 ]),
120+ AttachedList : strings .Split (fields [3 ], "," ),
121+ Created : convert .StringToTime (fields [4 ]),
122+ Format : convert .StringToBool (fields [5 ]),
123+ Group : fields [6 ],
124+ GroupAttached : convert .StringToInt (fields [7 ]),
125+ GroupAttachedList : strings .Split (fields [8 ], "," ),
126+ GroupList : strings .Split (fields [9 ], "," ),
127+ GroupManyAttached : convert .StringToBool (fields [10 ]),
128+ GroupSize : convert .StringToInt (fields [11 ]),
129+ Grouped : convert .StringToBool (fields [12 ]),
130+ ID : fields [13 ],
131+ LastAttached : convert .StringToTime (fields [14 ]),
132+ ManyAttached : convert .StringToBool (fields [15 ]),
133+ Marked : convert .StringToBool (fields [16 ]),
134+ Name : fields [17 ],
135+ Path : fields [18 ],
136+ Stack : convert .StringToIntSlice (fields [19 ]),
137+ Windows : convert .StringToInt (fields [20 ]),
105138 }
139+ sessions = append (sessions , session )
106140 }
107- return variablesStr
141+
142+ return sessions
143+ }
144+
145+ func sortSessions (sessions []* TmuxSession ) []* TmuxSession {
146+ sort .Slice (sessions , func (i , j int ) bool {
147+ return sessions [j ].LastAttached .Before (* sessions [i ].LastAttached )
148+ })
149+
150+ return sessions
108151}
109152
110153func List () ([]* TmuxSession , error ) {
@@ -116,46 +159,7 @@ func List() ([]*TmuxSession, error) {
116159 }
117160 sessionList := strings .TrimSpace (string (output ))
118161 lines := strings .Split (sessionList , "\n " )
119- sessions := make ([]* TmuxSession , 0 , len (lines ))
120- for _ , line := range lines {
121- fields := strings .Split (line , " " ) // Strings split by single space
122- if len (fields ) == 21 {
123- session := & TmuxSession {
124- Activity : convert .StringToTime (fields [0 ]),
125- Alerts : convert .StringToIntSlice (fields [1 ]),
126- Attached : convert .StringToInt (fields [2 ]),
127- AttachedList : strings .Split (fields [3 ], "," ),
128- Created : convert .StringToTime (fields [4 ]),
129- Format : convert .StringToBool (fields [5 ]),
130- Group : fields [6 ],
131- GroupAttached : convert .StringToInt (fields [7 ]),
132- GroupAttachedList : strings .Split (fields [8 ], "," ),
133- GroupList : strings .Split (fields [9 ], "," ),
134- GroupManyAttached : convert .StringToBool (fields [10 ]),
135- GroupSize : convert .StringToInt (fields [11 ]),
136- Grouped : convert .StringToBool (fields [12 ]),
137- ID : fields [13 ],
138- LastAttached : convert .StringToTime (fields [14 ]),
139- ManyAttached : convert .StringToBool (fields [15 ]),
140- Marked : convert .StringToBool (fields [16 ]),
141- Name : fields [17 ],
142- Path : fields [18 ],
143- Stack : convert .StringToIntSlice (fields [19 ]),
144- Windows : convert .StringToInt (fields [20 ]),
145- }
146- if session .Attached != 1 {
147- sessions = append (sessions , session )
148- }
149- sort .Slice (sessions , func (i , j int ) bool {
150- if sessions [i ].LastAttached == nil {
151- return false
152- }
153- if sessions [j ].LastAttached == nil {
154- return true
155- }
156- return sessions [j ].LastAttached .Before (* sessions [i ].LastAttached )
157- })
158- }
159- }
160- return sessions , nil
162+ sessions := processSessions (lines )
163+
164+ return sortSessions (sessions ), nil
161165}
0 commit comments