Fas ahora acepta un argumento de línea de comandos para abrir o crear un archivo al inicio, igual que Vim.
# Abrir archivo existente
swift run Fas archivo.txt
# Crear archivo nuevo
swift run Fas nuevo.txt
# Iniciar sin archivo
swift run Fasswift run Fas README.mdResultado:
- ✅ Abre el archivo README.md
- ✅ Carga el contenido en el buffer
- ✅ Asigna
filePathal buffer - ✅ Mensaje:
"Opened: README.md" - ✅ Puedes editar inmediatamente
- ✅
:wguarda sin pedir nombre
swift run Fas nuevo.txtResultado:
- ✅ Crea un buffer vacío
- ✅ Asigna
filePath = "nuevo.txt"al buffer - ✅ Mensaje:
"nuevo.txt" [New File] - ✅ Puedes empezar a escribir inmediatamente (
i) - ✅
:wguarda en "nuevo.txt" sin pedir nombre - ✅ El archivo se crea cuando guardas por primera vez
swift run FasResultado:
- ✅ Inicia con buffer vacío
- ✅ No hay
filePathasignado - ✅ Mensaje:
"Welcome to Fas! Press 'i' to insert, ':' for commands, ':q' to quit" - ✅
:wpide nombre de archivo - ✅
:w archivo.txtasigna nombre y guarda
@main struct EntryPoint {
static func main() async throws {
// Obtener argumentos de línea de comandos
let arguments = CommandLine.arguments
// arguments[0] es el nombre del programa, arguments[1] es el primer argumento
let filePath: String? = arguments.count > 1 ? arguments[1] : nil
try await Fas.run(withFile: filePath)
}
}public static func run(withFile filePath: String? = nil) async throws {
var editor = Fas()
try await editor.start(withFile: filePath)
}private mutating func start(withFile filePath: String?) async throws {
// ... setup de terminal ...
// Si se proporcionó un archivo, intentar abrirlo o crear uno nuevo
if let path = filePath {
try openOrCreateFile(path)
}
// Mensaje de bienvenida solo si no hay archivo
if filePath == nil {
state.setMessage("Welcome to Fas! Press 'i' to insert, ':' for commands, ':q' to quit")
}
// ... event loop ...
}private mutating func openOrCreateFile(_ path: String) throws {
let expandedPath = (path as NSString).expandingTildeInPath
// Verificar si el archivo existe
if FileManager.default.fileExists(atPath: expandedPath) {
// Si existe, abrirlo normalmente
try openFile(path)
} else {
// Si no existe, crear un buffer nuevo con ese nombre
let fileName = (expandedPath as NSString).lastPathComponent
var newBuffer = api.buffers.currentBuffer
newBuffer.filePath = expandedPath
newBuffer.fileType = detectFileType(path: expandedPath)
newBuffer.isModified = false // Nuevo archivo, todavía no modificado
// Actualizar el buffer
api.buffers.currentBuffer = newBuffer
cursor = Cursor()
state.cursor = cursor.position
state.setMessage("\"\(fileName)\" [New File]")
}
}# Crear archivo de prueba
echo "Hello World" > test.txt
# Abrir con Fas
swift run Fas test.txt
# En el editor:
# - El contenido "Hello World" está visible
# - Mensaje: "Opened: test.txt"
# - Presiona 'i' para editar
# - Agrega texto
# - ESC
# - :w (guarda sin pedir nombre)
# - :q# Abrir archivo que no existe
swift run Fas nuevo.txt
# En el editor:
# - Buffer vacío
# - Mensaje: "nuevo.txt" [New File]
# - Presiona 'i'
# - Escribe contenido
# - ESC
# - :w (crea el archivo)
# - :q
# Verificar
cat nuevo.txt # Muestra el contenido# Editar archivo Swift
swift run Fas Sources/FasCore/Fas.swift
# En el editor:
# - Archivo se abre correctamente
# - Navegar con hjkl
# - Hacer cambios (i → editar → ESC)
# - :w (guarda cambios)
# - :q# Crear archivo con espacios
swift run Fas "Mi Archivo.txt"
# Funciona correctamente:
# - Mensaje: "Mi Archivo.txt" [New File]
# - :w guarda en "Mi Archivo.txt"# Ruta relativa
swift run Fas ../test.txt
# Ruta absoluta
swift run Fas /tmp/test.txt
# Home directory
swift run Fas ~/Documents/test.txt
# Todas funcionan correctamente con expandTildeInPath| Comando | Vim | Fas | Estado |
|---|---|---|---|
vim archivo.txt |
Abre/crea | Abre/crea | ✅ Igual |
vim |
Buffer vacío | Buffer vacío | ✅ Igual |
:w después de abrir |
Guarda sin pedir | Guarda sin pedir | ✅ Igual |
:w en archivo nuevo |
Crea archivo | Crea archivo | ✅ Igual |
| Mensaje [New File] | Sí | Sí | ✅ Igual |
echo "test content" > /tmp/test.txt
swift run Fas /tmp/test.txt
# Verificar:
# - Contenido visible
# - Mensaje "Opened: /tmp/test.txt"
# - :w funciona sin pedir nombrerm -f /tmp/nuevo.txt # Asegurar que no existe
swift run Fas /tmp/nuevo.txt
# Verificar:
# - Buffer vacío
# - Mensaje "nuevo.txt" [New File]
# - i → escribir → ESC → :w → archivo se creaswift run Fas
# Verificar:
# - Buffer vacío
# - Mensaje de bienvenida
# - :w pide nombreswift run Fas archivo1.txt archivo2.txt
# Solo abre archivo1.txt (comportamiento esperado)✅ Feature completada ✅ Build exitoso (2.68s) ✅ 0 errores, 0 warnings de Swift
Sources/Fas/App.swift- Parseo de argumentosSources/FasCore/Fas.swift- run(), start(), openOrCreateFile()
- ✅ Compatible con rutas relativas
- ✅ Compatible con rutas absolutas
- ✅ Compatible con
~(home directory) - ✅ Compatible con espacios en nombres
- ✅ Retrocompatible (sin argumentos sigue funcionando)
- Soportar múltiples archivos:
fas file1.txt file2.txt - Flags:
fas -R archivo.txt(read-only) - Posicionamiento:
fas +10 archivo.txt(ir a línea 10) - Tabs:
fas -p file1.txt file2.txt(abrir en tabs)