github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/management/history.go (about)

     1  package management
     2  
     3  import (
     4  	"github.com/lmorg/murex/lang"
     5  	"github.com/lmorg/murex/lang/types"
     6  	"github.com/lmorg/murex/shell"
     7  	"github.com/lmorg/murex/shell/history"
     8  	"github.com/lmorg/murex/utils/json"
     9  )
    10  
    11  func init() {
    12  	lang.DefineFunction("history", cmdHistory, types.JsonLines)
    13  }
    14  
    15  func cmdHistory(p *lang.Process) (err error) {
    16  	//if !shell.Interactive {
    17  	//	return errors.New("This is only designed to be run when the shell is in interactive mode")
    18  	//}
    19  
    20  	list := shell.Prompt.History.Dump().([]history.Item)
    21  
    22  	// If outputting to the terminal then lets just do pure JSON for readability
    23  	if p.Stdout.IsTTY() {
    24  		p.Stdout.SetDataType(types.Json)
    25  		b, err := json.Marshal(list, p.Stdout.IsTTY())
    26  		if err != nil {
    27  			return err
    28  		}
    29  
    30  		_, err = p.Stdout.Writeln(b)
    31  		return err
    32  	}
    33  
    34  	// if not outputting to the terminal, then use jsonlines instead for easier
    35  	// grepping et al
    36  
    37  	p.Stdout.SetDataType(types.JsonLines)
    38  
    39  	aw, err := p.Stdout.WriteArray(types.JsonLines)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	for i := range list {
    45  		b, err := json.Marshal(list[i], p.Stdout.IsTTY())
    46  		if err != nil {
    47  			return err
    48  		}
    49  
    50  		err = aw.Write(b)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  /*func cmdHistPipe(p *lang.Process) error {
    60  	if !shell.Interactive {
    61  		return errors.New("This is only designed to be run when the shell is in interactive mode.")
    62  	}
    63  
    64  	p.Stdout.SetDataType(types.Null)
    65  
    66  	name, err := p.Parameters.String(0)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if lang.GlobalPipes.Dump()[name] == "" {
    72  		return errors.New("No pipe exists named: " + name)
    73  	}
    74  
    75  	pipe, err := lang.GlobalPipes.Get(name)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	shell.History.Writer = pipe
    81  
    82  	return nil
    83  }*/