refactor: Improve tmux API consistency and formatting

- Refactor `logs_get_new` to use `LogsGetArgs` struct
- Return window as reference from `window_new`
- Standardize indentation and spacing
- Remove excessive blank lines
- Comment out initial example usage
This commit is contained in:
Mahmoud-Emad
2025-08-24 16:31:04 +03:00
parent 810cbda176
commit 117c9ac67c
4 changed files with 199 additions and 214 deletions

View File

@@ -1,20 +1,20 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.osal.tmux
mut t := tmux.new()!
if !t.is_running()! {
t.start()!
}
if t.session_exist('main') {
t.session_delete('main')!
}
// Create session first, then create window
mut session := t.session_create(name: 'main')!
session.window_new(name: 'test', cmd: 'mc', reset: true)!
// Or use the convenience method
// t.window_new(session_name: 'main', name: 'test', cmd: 'mc', reset: true)!
// if !t.is_running()! {
// t.start()!
// }
// if t.session_exist('main') {
// t.session_delete('main')!
// }
// // Create session first, then create window
// mut session := t.session_create(name: 'main')!
// session.window_new(name: 'test', cmd: 'mc', reset: true)!
// // Or use the convenience method
// // t.window_new(session_name: 'main', name: 'test', cmd: 'mc', reset: true)!
println(t)

View File

@@ -14,7 +14,6 @@ pub mut:
sessionid string // unique link to job
}
// get session (session has windows) .
// returns none if not found
pub fn (mut t Tmux) session_get(name_ string) !&Session {
@@ -56,8 +55,6 @@ pub mut:
reset bool
}
// create session, if reset will re-create
pub fn (mut t Tmux) session_create(args SessionCreateArgs) !&Session {
name := texttools.name_fix(args.name)
@@ -83,7 +80,6 @@ pub fn (mut t Tmux) session_create(args SessionCreateArgs) !&Session {
return s
}
@[params]
pub struct TmuxNewArgs {
sessionid string
@@ -126,7 +122,6 @@ pub fn (mut t Tmux) window_new(args WindowNewArgs) !&Window {
)!
}
pub fn (mut t Tmux) stop() ! {
$if debug {
console.print_debug('Stopping tmux...')
@@ -156,7 +151,6 @@ pub fn (mut t Tmux) start() ! {
t.scan()!
}
// print list of tmux sessions
pub fn (mut t Tmux) list_print() {
// os.log('TMUX - Start listing ....')

View File

@@ -20,7 +20,6 @@ pub mut:
last_output_offset int // for tracking new logs
}
pub fn (mut p Pane) stats() !ProcessStats {
if p.pid == 0 {
return ProcessStats{}
@@ -48,7 +47,6 @@ pub fn (mut p Pane) stats() !ProcessStats {
}
}
pub struct TMuxLogEntry {
pub mut:
content string
@@ -56,16 +54,19 @@ pub mut:
offset int
}
pub fn (mut p Pane) logs_get_new(reset bool) ![]TMuxLogEntry {
pub struct LogsGetArgs {
pub mut:
reset bool
}
if reset{
// get new logs since last call
pub fn (mut p Pane) logs_get_new(args LogsGetArgs) ![]TMuxLogEntry {
if args.reset {
p.last_output_offset = 0
}
// Capture pane content with line numbers
cmd := 'tmux capture-pane -t ${p.window.session.name}:@${p.window.id}.%${p.id} -S ${p.last_output_offset} -p'
result := osal.execute_silent(cmd) or {
return error('Cannot capture pane output: ${err}')
}
result := osal.execute_silent(cmd) or { return error('Cannot capture pane output: ${err}') }
lines := result.split_into_lines()
mut entries := []TMuxLogEntry{}
@@ -106,9 +107,7 @@ pub fn (mut p Pane) exit_status() !ProcessStatus {
pub fn (mut p Pane) logs_all() !string {
cmd := 'tmux capture-pane -t ${p.window.session.name}:@${p.window.id}.%${p.id} -S -2000 -p'
return osal.execute_silent(cmd) or {
error('Cannot capture pane output: ${err}')
}
return osal.execute_silent(cmd) or { error('Cannot capture pane output: ${err}') }
}
// Fix the output_wait method to use correct method name

View File

@@ -21,6 +21,7 @@ pub mut:
env map[string]string
reset bool
}
@[params]
pub struct WindowGetArgs {
pub mut:
@@ -28,10 +29,9 @@ pub mut:
id int
}
pub fn (mut s Session) create() ! {
// Check if session already exists
cmd_check := "tmux has-session -t ${s.name}"
cmd_check := 'tmux has-session -t ${s.name}'
check_result := osal.exec(cmd: cmd_check, stdout: false, ignore_error: true) or {
// Session doesn't exist, this is expected
osal.Job{}
@@ -42,7 +42,7 @@ pub fn (mut s Session) create() ! {
}
// Create new session
cmd := "tmux new-session -d -s ${s.name}"
cmd := 'tmux new-session -d -s ${s.name}'
osal.exec(cmd: cmd, stdout: false, name: 'tmux_session_create') or {
return error("Can't create session ${s.name}: ${err}")
}
@@ -54,7 +54,7 @@ pub fn (mut s Session) scan() ! {
cmd := "tmux list-windows -t ${s.name} -F '#{window_name}|#{window_id}|#{window_active}'"
result := osal.execute_silent(cmd) or {
if err.msg().contains('session not found') {
return // Session doesn't exist anymore
return
}
return error('Cannot list windows for session ${s.name}: ${err}')
}
@@ -102,7 +102,6 @@ pub fn (mut s Session) scan() ! {
s.windows = s.windows.filter(current_windows[it.name] == true)
}
// window_name is the name of the window in session main (will always be called session main)
// cmd to execute e.g. bash file
// environment arguments to use
@@ -116,7 +115,7 @@ pub fn (mut s Session) scan() ! {
// reset bool
// }
// ```
pub fn (mut s Session) window_new(args WindowArgs) !Window {
pub fn (mut s Session) window_new(args WindowArgs) !&Window {
$if debug {
console.print_header(' start window: \n${args}')
}
@@ -128,7 +127,7 @@ pub fn (mut s Session) window_new(args WindowArgs) !Window {
return error('cannot create new window it already exists, window ${namel} in session:${s.name}')
}
}
mut w := Window{
mut w := &Window{
session: &s
name: namel
panes: []&Pane{}
@@ -143,10 +142,6 @@ pub fn (mut s Session) window_new(args WindowArgs) !Window {
return w
}
// get all windows as found in a session
pub fn (mut s Session) windows_get() []&Window {
mut res := []&Window{}
@@ -208,8 +203,6 @@ pub fn (mut s Session) stats() !ProcessStats {
// }
// }
fn (mut s Session) window_exist(args_ WindowGetArgs) bool {
mut args := args_
s.window_get(args) or { return false }
@@ -249,7 +242,6 @@ pub fn (mut s Session) window_delete(args_ WindowGetArgs) ! {
s.windows.delete(i) // i is now the one in the list which needs to be removed
}
pub fn (mut s Session) restart() ! {
s.stop()!
s.create()!