Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bar component for scripted text #252

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

BashkaMen
Copy link

No description provided.

GlazeWM.Bar/Components/ScriptedTextComponentViewModel.cs Outdated Show resolved Hide resolved
GlazeWM.Domain/UserConfigs/ScriptedTextConfig.cs Outdated Show resolved Hide resolved
var output = outSb.ToString();
var json = Regex.Matches(output).Last(); // extract json block
var result = JsonSerializer.Deserialize<Dictionary<string, object>>(json.Value);
FormattedText = result.Aggregate(_baseConfig.Format, (state, item) => state.Replace($"{{{item.Key}}}", item.Value.ToString()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be users who have scripts which just return strings instead of JSON.

to solve this, would it be possible to do label: "{output.coin} {output.price}" for scripts where the output is JSON, and label: BTC price {output} for scripts that simply output a string?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that returning json and doing formatting on the application side will be more convenient than parsing lables inside a script
I just want a few values from the script to use in the output
otherwise, you will have to return the finished string from the script and the format is not needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% sure what to do about this. posted about it in the #contributing channel on discord

Copy link
Author

@BashkaMen BashkaMen Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you want to return multiple value from cmd script?

echo value1 value2?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in polybar, users would write a .sh script and whatever it outputs is shown in the bar. so to remake your BTC example in polybar, a script would have to output eg "BTC: 22000". There is no support for multiple values.

A lot of glazewm users come from polybar, and a lot cli scripts out there don't output as JSON. So ideally we can figure out a solution that supports showing regular text output, as well as JSON.

It's hard to explain the idea i'm proposing, so i'll show a few examples:

  1. user has a script that outputs "BTC: 22000", he can define a config label: "{output}"
  2. user has a script that outputs "22000", he can define a config label: "BTC: {output}"
  3. user has a script that outputs { "price": "22000" }, he can define a config label: "BTC: {output.price}"
  4. user has a script that outputs { "price": "22000", "token: "BTC" }, he can define a config label: "{output.token}: {output.price}"

^ with this way, users can use scripts that output regular text, as well as JSON

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added output type

@lars-berger lars-berger changed the title new bar component feat: bar component for scripted text Feb 14, 2023
@BashkaMen
Copy link
Author

BashkaMen commented Feb 14, 2023

new config

- type: "scripted text"
      script_path: dotnet
      script_args: fsi "C:\Users\sbmba\.glaze-wm\run.fsx" bitcoin
      label: "BTC: {price}"
      interval_ms: 60000
      margin: 5px
      padding: 5px
      background: "#f7931a"

@BashkaMen
Copy link
Author

BashkaMen commented Feb 14, 2023

and test this with compiled dotnet app

- type: "scripted text"
      script_path: C:\Users\sbmba\.glaze-wm\crypto-price\run.exe
      script_args: bitcoin
      label: "BTC: {price}"
      interval_ms: 60000
      margin: 5px
      padding: 5px
      background: "#f7931a"

{
var updateInterval = TimeSpan.FromMilliseconds(_baseConfig.IntervalMs);
TimerSub = Observable.Interval(updateInterval).Subscribe( _ => RunScript());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor can be simplified a bit by changing Init:

    private void Init()
    {
      _ = RunScript();
      var updateInterval = TimeSpan.FromMilliseconds(_baseConfig.IntervalMs);
      Observable.Interval(updateInterval)
        .TakeUntil(_bus.Events.OfType<UserConfigReloadedEvent>())
        .Subscribe(_ => RunScript());
    }

var output = outSb.ToString();
var json = Regex.Matches(output).Last(); // extract json block
var result = JsonSerializer.Deserialize<Dictionary<string, object>>(json.Value);
FormattedText = result.Aggregate(_baseConfig.Format, (state, item) => state.Replace($"{{{item.Key}}}", item.Value.ToString()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% sure what to do about this. posted about it in the #contributing channel on discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants