View Categories

Command Lifecycle (Internal Execution Flow)

2 min read

What happens from the moment the user types a command until the terminal prints output

Understanding the command lifecycle helps developers:

  • override commands safely
  • extend behavior
  • hook into the right moment
  • debug advanced issues
  • build add-ons properly

This is the internal flow used by your real plugin.


πŸ”Ή 1. User types into the input field #

The user enters something like:

wallet

or:

say Hello world

This text lives inside the input element:

<input id="cointacted-social-terminal-input">

No processing happens yet.


πŸ”Ή 2. User presses Enter (Keyboard Event) #

A key listener captures it:

if (event.key === "Enter") { ... }

When pressed:

  • The input value is read β†’ this becomes input.raw
  • The input is cleared
  • The cursor resets position
  • The command is handed to the parser

πŸ”Ή 3. Input Parsing (Tokenizer) #

The text is split into:

cmd      // primary command (string)
args     // list of arguments (array)
raw      // entire text (string)

Example:

Input:

say Hello world

Parser output:

cmd: "say"
args: ["Hello", "world"]
raw: "say Hello world"

Parser rules:

  • multiple spaces collapse into one
  • leading and trailing spaces are stripped
  • quotes are not yet supported (future)

πŸ”Ή 4. Aliases Resolved #

If the user typed an alias:

cls

The system resolves it to:

clear

This is done by scanning:

commands_meta[cmd].alias

If alias matches, it maps to the real command name.


πŸ”Ή 5. Check if the command exists #

The system checks:

CointactedSocialTerminalTerminal.registry[cmd]

If no match:

  • runs the command_not_found handler
  • usually prints:
Command not found: xyz

Add-ons may override this handler.


πŸ”Ή 6. Permission Check (Guest vs Logged-In) #

If the site is in guest-restricted mode:

  • commands with public: true can run
  • others require login

If the command is private and the user is logged out:

The terminal prints:

You must be logged in to use this command.

And execution stops.


πŸ”Ή 7. Help/Data Query Before Execution #

Before running a command, the system:

  • retrieves metadata
  • checks if hidden
  • checks its group
  • prepares help-side access rules

This allows help to reflect the correct state.


πŸ”Ή 8. Command Execution (Your Function Runs) #

Now the command actually executes:

registry[cmd](input);

This is where your code lives:

registry.mycmd = function(input) {
    // your logic here
};

Execution is synchronous or async
(both types work automatically).


πŸ”Ή 9. Your Command Prints Output Using: #

cointacted_social_terminal_append_line(text, type);

This function:

  • escapes HTML
  • auto-scrolls
  • wraps into <div class="cointacted-social-terminal__line">
  • applies a type-specific class
  • inserts line into output panel

Example output DOM:

<div class="cointacted-social-terminal__line cointacted-social-terminal__line--system">
  Hello world
</div>

πŸ”Ή 10. Each Output Line Dispatches an Event #

Every time you call append_line(), the terminal fires:

cointacted_social_terminal_output

With:

event.detail = {
    text: "Hello world",
    type: "system"
};

Add-ons can listen to this to:

  • sync data
  • trigger animations
  • apply effects
  • monitor user activity
  • run analytics

This is one of the most powerful hooks.


πŸ”Ή 11. History Recording #

After execution, the command is:

  • appended to session history
  • accessible through:
!!
!3
history

History is stored only in memory (no database).


πŸ”Ή 12. Multi-Line Output Completion #

A command may produce:

  • no lines
  • one line
  • multiple lines
  • asynchronous delayed lines

All output is appended in the order printed.

Async example:

append_line("Step 1");
setTimeout(() => append_line("Step 2"), 500);

The lifecycle supports this naturally.


πŸ”Ή 13. Terminal Auto-Scroll Behavior #

After each output:

  • terminal scrolls to bottom
  • unless the user manually scrolled up

This prevents:

  • annoying forced scrolling
  • loss of scroll position

πŸ”Ή 14. Command Completion #

After execution:

  • control returns to user
  • input is ready
  • cursor reappears
  • registry stays alive for next commands

No special teardown is needed.


πŸ”Ή Lifecycle Overview Diagram #

Here is the complete lifecycle in order:

User typing
       ↓
Press Enter
       ↓
Parser β†’ (cmd, args, raw)
       ↓
Alias resolution
       ↓
Command exists? β†’ No β†’ command_not_found β†’ END
       ↓
Guest mode check (if enabled)
       ↓
Metadata loading
       ↓
Execute registry[cmd](input)
       ↓
append_line(text, type)
       ↓
Output event fired
       ↓
History updated
       ↓
Auto-scroll
       ↓
Command complete

This flow applies to:

  • built-in commands
  • add-on commands
  • overridden commands
  • custom theme commands

Everything is unified through this lifecycle.


🎯 Summary #

The terminal lifecycle includes:

βœ” input parsing #

βœ” alias resolution #

βœ” permission filtering #

βœ” metadata preparation #

βœ” command execution #

βœ” output printing #

βœ” events #

βœ” history #

βœ” scroll management #

Understanding this flow allows developers to:

  • override safely
  • add advanced features
  • hook at the right stage
  • debug deeply
  • write powerful add-ons

Leave a Reply

Your email address will not be published. Required fields are marked *