const std = @import("std");

/// Check if stdout is a TTY
pub fn isTTY() bool {
    return std.posix.isatty(std.posix.STDOUT_FILENO);
}

/// Get terminal width from COLUMNS env var
pub fn getWidth() ?usize {
    const allocator = std.heap.page_allocator;
    if (std.process.getEnvVarOwned(allocator, "COLUMNS")) |cols| {
        defer allocator.free(cols);
        return std.fmt.parseInt(usize, cols, 10) catch null;
    } else |_| {}
    return null;
}

/// Table formatting mode
pub const TableMode = enum { truncate, wrap, auto };

/// Get table formatting mode from env var
pub fn getTableMode() TableMode {
    const allocator = std.heap.page_allocator;
    const mode_str = std.process.getEnvVarOwned(allocator, "ML_TABLE_MODE") catch return .truncate;
    defer allocator.free(mode_str);
    if (std.mem.eql(u8, mode_str, "wrap")) return .wrap;
    if (std.mem.eql(u8, mode_str, "auto")) return .auto;
    return .truncate;
}

/// Get user's preferred pager from PAGER env var
pub fn getPager() ?[]const u8 {
    const allocator = std.heap.page_allocator;
    return std.process.getEnvVarOwned(allocator, "PAGER") catch null;
}
