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

adds cursor control ANSI escape codes #887

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

wassup05
Copy link

@wassup05 wassup05 commented Nov 12, 2024

The PR adds cursor control ANSI escape codes as a separate module stdlib_ansi_cursor

This was brought up scarcely in #229 but was not really discussed.

Functions added are

  1. move_to(x,y)
  2. move_to_column(x)
  3. move_<direction> where <direction> is any one of up, down, left, right
  4. A couple of constants for clearning the terminal, clearning a line and it's different variants

Motivation

There is already a module stdlib_ansi for ANSI escape codes of foreground, background colors and the different styles... If a person finds the use for such module it is very probable that he will also at some point want to control the cursor position too, to let's say, implement a custom loading animation or for elegant management of terminal output

Side note

I chose to make it a different module because of the difference in approach between the two, the functions in this module talk in terms of strings whereas stdlib_ansi has a whole type surrounding it and also because you won't need to add the cursor_ to all the functions to make it more readable... But I don't really see a problem in merging the two too...

Edit: This is my first attempt at contributing here and I am not sure about who I am supposed to ask for feedback.. Could you guide me regarding this @perazz @jalvesz (I figured as you were the most active right now, hence pinging you would be appropriate)

Copy link
Member

@jvdp1 jvdp1 left a comment

Choose a reason for hiding this comment

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

Thank you @wassup05 for this PR. It sounds like a good start. I left a few comments.

@@ -0,0 +1,107 @@
module stdlib_ansi_cursor
Copy link
Member

Choose a reason for hiding this comment

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

Could this be considered as a submodule of the module stdlib_ansi?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for reviewing!

It could be, but I thought separating it would be better since stdlib_ansi revolves around it's own derived type whereas this doesn't... I'm open to either way though

src/stdlib_ansi_cursor.f90 Show resolved Hide resolved

character(len=*), parameter :: esc = achar(27)
!> moves the cursor to home => `(0,0)`
character(len=*), parameter :: home = esc//"[H"
Copy link
Member

Choose a reason for hiding this comment

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

Is this the same for all OS?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, It's more of a terminal thing than an OS thing.. But almost all of the modern terminals support these ansi codes

src/stdlib_ansi_cursor.f90 Outdated Show resolved Hide resolved
contains
!> moves the cursor to `(line, column)`
!> returns an empty string if any of them is negative
pure function move_to(line, col) result(str)
Copy link
Member

Choose a reason for hiding this comment

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

Could some of these functions be elemental?

Copy link
Author

Choose a reason for hiding this comment

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

They could be made so, But I don't think it's worth it since I don't see anyone using it like that

test/terminal/test_ansi_cursor.f90 Outdated Show resolved Hide resolved
@perazz
Copy link
Contributor

perazz commented Nov 18, 2024

Nice work @wassup05, thank you. I'm no expert on ANSI terminal handling so I will add some of my thoughts here.

  • The stdlib_ansi module has a derived type for ANSI escape codes:

type :: ansi_code
private
!> Style descriptor
integer(i1) :: style = -1_i1
!> Background color descriptor
integer(i1) :: bg = -1_i1
!> Foreground color descriptor
integer(i1) :: fg = -1_i1
end type ansi_code

If the terminal handling codes provided here match the same pattern, I believe it would be better to uniform their handling with that of the module, and make them type(ansi_code) as well.

  • Testing and examples are hard with console handling. However, I think it would be very useful to have examples that show some console interaction with these functions (for example, draw a box on the terminal, or something like that).

@wassup05
Copy link
Author

Thanks for replying, @perazz !

When I started working on this, I thought of ways to integrate the two but couldn't really find any such way... As these are more action oriented whereas those of ansi_type were more of like changing the properties of the text being printed

Regarding examples... I added an example in the recent commit which basically draws a blue box on the screen using a subroutine draw_box, whose width, height and the position (line, column) were it is drawn can be changed.. I added the link to it in the spec as well

Please let me know what you think about it

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.

3 participants